| |
@@ -17,121 +17,86 @@
|
| |
# Authors:
|
| |
# Josef Skladanka <jskladan@redhat.com>
|
| |
|
| |
- #import os
|
| |
- #import sys
|
| |
- #from optparse import OptionParser
|
| |
- #
|
| |
- #from alembic.config import Config
|
| |
- #from alembic import command as al_command
|
| |
- #from alembic.migration import MigrationContext
|
| |
- #
|
| |
- #from oraculum import db
|
| |
- #from oraculum.models.user import User
|
| |
- #
|
| |
- #
|
| |
- #def get_alembic_config():
|
| |
- # # the location of the alembic ini file and alembic scripts changes when
|
| |
- # # installed via package
|
| |
- # if os.path.exists("./alembic.ini"):
|
| |
- # alembic_cfg = Config("./alembic.ini")
|
| |
- # else:
|
| |
- # alembic_cfg = Config("/usr/share/oraculum/alembic.ini",
|
| |
- # ini_section='alembic-packaged')
|
| |
- # return alembic_cfg
|
| |
- #
|
| |
- #
|
| |
- #def upgrade_db(*args):
|
| |
- # alembic_cfg = get_alembic_config()
|
| |
- #
|
| |
- # context = MigrationContext.configure(db.engine.connect())
|
| |
- # current_rev = context.get_current_revision()
|
| |
- # print "Upgrading Database to `head` from `%s`" % current_rev
|
| |
- #
|
| |
- # al_command.upgrade(alembic_cfg, "head")
|
| |
- #
|
| |
- #
|
| |
- #def init_alembic(*args):
|
| |
- # alembic_cfg = get_alembic_config()
|
| |
- #
|
| |
- # # check to see if the db has already been initialized by checking for an
|
| |
- # # alembic revision
|
| |
- # context = MigrationContext.configure(db.engine.connect())
|
| |
- # current_rev = context.get_current_revision()
|
| |
- #
|
| |
- # if not current_rev:
|
| |
- # print "Initializing alembic"
|
| |
- # print " - Setting the version to the first revision"
|
| |
- # al_command.stamp(alembic_cfg, "15f5eeb9f635")
|
| |
- # else:
|
| |
- # print "Alembic already initialized"
|
| |
- #
|
| |
- #
|
| |
- #def initialize_db(destructive):
|
| |
- # alembic_cfg = get_alembic_config()
|
| |
- #
|
| |
- # print "Initializing database"
|
| |
- # if destructive:
|
| |
- # print " - Dropping all tables"
|
| |
- # db.drop_all()
|
| |
- # print " - Creating tables"
|
| |
- # db.create_all()
|
| |
- # print " - Stamping alembic's current version to 'head'"
|
| |
- # al_command.stamp(alembic_cfg, "head")
|
| |
- #
|
| |
- # init_alembic()
|
| |
- # upgrade_db()
|
| |
- #
|
| |
- #
|
| |
- #def mock_data(destructive):
|
| |
- # print "Populating tables with mock-data"
|
| |
- #
|
| |
- # if destructive or not db.session.query(User).count():
|
| |
- # print " - User"
|
| |
- # data_users = [('admin', 'admin'), ('user', 'user')]
|
| |
- #
|
| |
- # for d in data_users:
|
| |
- # u = User(*d)
|
| |
- # db.session.add(u)
|
| |
- #
|
| |
- # db.session.commit()
|
| |
- # else:
|
| |
- # print " - skipped User"
|
| |
- #
|
| |
- #
|
| |
- #def main():
|
| |
- # possible_commands = ['init_db', 'mock_data', 'upgrade_db', 'init_alembic']
|
| |
- #
|
| |
- # usage = 'usage: [DEV=true] %prog ' + "(%s)" % ' | '.join(possible_commands)
|
| |
- # parser = OptionParser(usage=usage)
|
| |
- # parser.add_option("-d", "--destructive",
|
| |
- # action="store_true", dest="destructive", default=False,
|
| |
- # help="Drop tables in `init_db`; Store data in `mock_data` "
|
| |
- # "even if the tables are not empty")
|
| |
- #
|
| |
- # (options, args) = parser.parse_args()
|
| |
- #
|
| |
- # if len(args) != 1 or args[0] not in possible_commands:
|
| |
- # print usage
|
| |
- # print
|
| |
- # print 'Please use one of the following commands: %s' % str(possible_commands)
|
| |
- # sys.exit(1)
|
| |
- #
|
| |
- # command = {
|
| |
- # 'init_db': initialize_db,
|
| |
- # 'mock_data': mock_data,
|
| |
- # 'upgrade_db': upgrade_db,
|
| |
- # 'init_alembic': init_alembic,
|
| |
- # }[args[0]]
|
| |
- #
|
| |
- # if not options.destructive:
|
| |
- # print "Proceeding with non-destructive init. To perform destructive "\
|
| |
- # "steps use -d option."
|
| |
- #
|
| |
- # command(options.destructive)
|
| |
- #
|
| |
+ import os
|
| |
+ import sys
|
| |
+ from optparse import OptionParser
|
| |
+
|
| |
+ from alembic.config import Config
|
| |
+ from alembic import command as al_command
|
| |
+ from alembic.migration import MigrationContext
|
| |
+
|
| |
+ from oraculum import app, db, controllers
|
| |
+ from oraculum.utils import db_utils
|
| |
+
|
| |
+ def get_alembic_config():
|
| |
+ # the location of the alembic ini file and alembic scripts changes when
|
| |
+ # installed via package
|
| |
+ if os.path.exists("./alembic.ini"):
|
| |
+ alembic_cfg = Config("./alembic.ini")
|
| |
+ else:
|
| |
+ alembic_cfg = Config("/usr/share/blockerbugs/alembic.ini",
|
| |
+ ini_section='alembic-packaged')
|
| |
+ return alembic_cfg
|
| |
+
|
| |
+
|
| |
+ def initialize_db():
|
| |
+ alembic_cfg = get_alembic_config()
|
| |
+
|
| |
+ # check to see if the db has already been initialized by checking for an
|
| |
+ # alembic revision
|
| |
+ context = MigrationContext.configure(db.engine.connect())
|
| |
+ current_rev = context.get_current_revision()
|
| |
+
|
| |
+ if current_rev:
|
| |
+ print("Database already initialized and at rev %s - not re-initializing" % current_rev)
|
| |
+ else:
|
| |
+ print("Initializing Database")
|
| |
+ db.drop_all()
|
| |
+ db.create_all()
|
| |
+
|
| |
+ print("Initializing alembic version")
|
| |
+
|
| |
+ al_command.stamp(alembic_cfg, "head")
|
| |
+
|
| |
+
|
| |
+ def upgrade_db():
|
| |
+ print("Upgrading Database to Latest Revision")
|
| |
+ alembic_cfg = get_alembic_config()
|
| |
+ al_command.upgrade(alembic_cfg, "head")
|
| |
+
|
| |
+ def sync():
|
| |
+ print("Refreshing DB Cache")
|
| |
+ app.config['MAX_DB_AGE'] = 0
|
| |
+ app.config['SKIP_CACHE_AGE_CHECK'] = False
|
| |
+ db_utils.refresh_data("get_actions", controllers.main.get_actions())
|
| |
+ #db_utils.refresh_data("api_v0_meetings", controllers.main.api_v0_meetings()) FIXME
|
| |
|
| |
def main():
|
| |
- print("No cli options available")
|
| |
+ possible_commands = ['init_db', 'generate_config', 'upgrade_db', 'sync']
|
| |
+
|
| |
+ usage = 'usage: %prog [options] command \n Possible Commands: ' + ' '.join(
|
| |
+ possible_commands)
|
| |
+ parser = OptionParser(usage=usage)
|
| |
+ parser.add_option('-d', '--dburi', dest='dburi', help='dburi to use')
|
| |
+
|
| |
+ (options, args) = parser.parse_args()
|
| |
+
|
| |
+ if len(args) < 1:
|
| |
+ print("need to have at least 1 command")
|
| |
+ sys.exit(1)
|
| |
+
|
| |
+ command = args[0]
|
| |
+ if not command in possible_commands:
|
| |
+ print("Invalid command: %s" % command)
|
| |
+ print("Please use one of the following commands: %s" % str(possible_commands))
|
| |
+ sys.exit(1)
|
| |
+
|
| |
+ elif command == 'upgrade_db':
|
| |
+ upgrade_db()
|
| |
+ elif command == 'init_db':
|
| |
+ initialize_db()
|
| |
+ elif command == 'sync':
|
| |
+ sync()
|
| |
|
| |
if __name__ == '__main__':
|
| |
main()
|
| |
Implements caching in database instead of memory as described in https://pagure.io/fedora-qa/oraculum/issue/3 .
Currently, handles only landing_page stuff (meetings, last_qa_meeting, schedule, blockerbugs, stable, devel).
TODO:
Handle case where it might be okay to have for example zero meetings and stop refreshing db every time in that case. Maybe rely more on provider_timestamp and relax some checks in db_utils... - DONE
dtdate in schedule is displayed in a different date/time format when received from cache compared to fetch. At the moment, I don't have any idea why is that happening, might need to take a second look. It's stored and always handled as a string, this shouldn't be happening :/
Some questions that came to my mind:
Notes to myself:
Don't save time_created for each stored value (they should never differ too much), create special table that would store time_created on per provider basis. This would make checking cache age cheaper. - DONE
Create sync function in cli.py that would preload all data into database. That way, it could be called every n minutes by cron, systemd timer,... - DONE
Make it possible to configure the app to completely skip time_created verification (in deployments where the db cache creation is handled elsewhere). - DONE (might be optimized a little more - by skipping "load_all_providers_timestamp" completely)