def prepare(): """Initializes an empty database (creates tables, sets alembic rev to HEAD)""" tables = get_all_tables(db) if 'alembic_version' not in tables['public']: print colored('Setting the alembic version to HEAD', 'green') stamp() PluginScriptDirectory.dir = os.path.join(current_app.root_path, 'core', 'plugins', 'alembic') alembic.command.ScriptDirectory = PluginScriptDirectory plugin_msg = cformat("%{cyan}Setting the alembic version of the %{cyan!}{}%{reset}%{cyan} " "plugin to HEAD%{reset}") for plugin in plugin_engine.get_active_plugins().itervalues(): if not os.path.exists(plugin.alembic_versions_path): continue print plugin_msg.format(plugin.name) with plugin.plugin_context(): stamp() # Retrieve the table list again, just in case we created unexpected hables tables = get_all_tables(db) tables['public'] = [t for t in tables['public'] if not t.startswith('alembic_version')] if any(tables.viewvalues()): print colored('Your database is not empty!', 'red') print colored('If you just added a new table/model, create an alembic revision instead!', 'yellow') print print 'Tables in your database:' for schema, schema_tables in sorted(tables.items()): for t in schema_tables: print cformat(' * %{cyan}{}%{reset}.%{cyan!}{}%{reset}').format(schema, t) return if not _require_extensions('unaccent', 'pg_trgm'): return print colored('Creating tables', 'green') db.create_all()
def prepare(): """Initializes an empty database (creates tables, sets alembic rev to HEAD)""" tables = get_all_tables(db) if 'alembic_version' not in tables['public']: print colored('Setting the alembic version to HEAD', 'green') stamp() PluginScriptDirectory.dir = os.path.join(current_app.root_path, 'core', 'plugins', 'alembic') alembic.command.ScriptDirectory = PluginScriptDirectory plugin_msg = cformat("%{cyan}Setting the alembic version of the %{cyan!}{}%{reset}%{cyan} " "plugin to HEAD%{reset}") for plugin in plugin_engine.get_active_plugins().itervalues(): if not os.path.exists(plugin.alembic_versions_path): continue print plugin_msg.format(plugin.name) with plugin.plugin_context(): stamp() # Retrieve the table list again, just in case we created unexpected hables tables = get_all_tables(db) tables['public'] = [t for t in tables['public'] if not t.startswith('alembic_version')] if any(tables.viewvalues()): print colored('Your database is not empty!', 'red') print colored('If you just added a new table/model, create an alembic revision instead!', 'yellow') print print 'Tables in your database:' for schema, schema_tables in sorted(tables.items()): for t in schema_tables: print cformat(' * %{cyan}{}%{reset}.%{cyan!}{}%{reset}').format(schema, t) return if not _require_extensions('unaccent', 'pg_trgm'): return print colored('Creating tables', 'green') db.create_all()
def prepare_db(empty=False, root_path=None, verbose=True): """Initialize an empty database (create tables, set alembic rev to HEAD).""" if not _require_pg_version('9.6'): return if not _require_encoding('UTF8'): return if not _require_extensions('unaccent', 'pg_trgm'): return root_path = root_path or current_app.root_path tables = get_all_tables(db) if 'alembic_version' not in tables['public']: if verbose: print(cformat('%{green}Setting the alembic version to HEAD')) stamp(directory=os.path.join(root_path, 'migrations'), revision='heads') PluginScriptDirectory.dir = os.path.join(root_path, 'core', 'plugins', 'alembic') alembic.command.ScriptDirectory = PluginScriptDirectory plugin_msg = cformat( "%{cyan}Setting the alembic version of the %{cyan!}{}%{reset}%{cyan} " "plugin to HEAD%{reset}") for plugin in plugin_engine.get_active_plugins().itervalues(): if not os.path.exists(plugin.alembic_versions_path): continue if verbose: print(plugin_msg.format(plugin.name)) with plugin.plugin_context(): stamp(revision='heads') # Retrieve the table list again, just in case we created unexpected tables tables = get_all_tables(db) tables['public'] = [ t for t in tables['public'] if not t.startswith('alembic_version') ] if any(tables.viewvalues()): if verbose: print(cformat('%{red}Your database is not empty!')) print( cformat( '%{yellow}If you just added a new table/model, create an alembic revision instead!' )) print() print('Tables in your database:') for schema, schema_tables in sorted(tables.items()): for t in schema_tables: print( cformat( ' * %{cyan}{}%{reset}.%{cyan!}{}%{reset}').format( schema, t)) return create_all_tables(db, verbose=verbose, add_initial_data=(not empty))
def setup(logger, zodb_root, sqlalchemy_uri, dblog=False, restore=False): app = IndicoFlask('indico_migrate') app.config['PLUGINENGINE_NAMESPACE'] = 'indico.plugins' app.config['SQLALCHEMY_DATABASE_URI'] = sqlalchemy_uri app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True _monkeypatch_config() plugin_engine.init_app(app) if not plugin_engine.load_plugins(app): print( cformat('%[red!]Could not load some plugins: {}%[reset]').format( ', '.join(plugin_engine.get_failed_plugins(app)))) sys.exit(1) db.init_app(app) if dblog: app.debug = True apply_db_loggers(app, force=True) db_logger = Logger.get('_db') db_logger.level = logging.DEBUG db_logger.propagate = False db_logger.addHandler(SocketHandler('127.0.0.1', 9020)) # avoid "no handlers registered" warnings logging.root.addHandler(logging.NullHandler()) import_all_models() configure_mappers() alembic_migrate.init_app(app, db, os.path.join(app.root_path, 'migrations')) try: tz = pytz.timezone( getattr(zodb_root['MaKaCInfo']['main'], '_timezone', 'UTC')) except KeyError: tz = pytz.utc with app.app_context(): if not restore: all_tables = sum(get_all_tables(db).values(), []) if all_tables: if db_has_data(): logger.fatal_error( 'Your database is not empty!\n' 'If you want to reset it, please drop and recreate it first.' ) else: # the DB is empty, prepare DB tables # prevent alembic from messing with the logging config tmp = logging.config.fileConfig logging.config.fileConfig = lambda fn: None prepare_db(empty=True, root_path=get_root_path('indico'), verbose=False) logging.config.fileConfig = tmp _create_oauth_apps() return app, tz
def reset_alembic(): """Resets the alembic state carried over from 1.9.x Only run this command right after upgrading from a 1.9.x version so the references to old alembic revisions (which were removed in 2.0) are reset. """ tables = get_all_tables(db)['public'] if 'alembic_version' not in tables: print('No alembic_version table found') sys.exit(1) current_revs = [ rev for rev, in db.session.execute( 'SELECT version_num FROM alembic_version').fetchall() ] if current_revs != ['65c079b091bf']: print( 'Your database is not at the latest 1.9.11 revision (got [{}], expected [65c079b091bf]).' .format(', '.join(current_revs))) print('This can have multiple reasons:') print( '1) You did not upgrade from 1.9.x, so you do not need this command' ) print('2) You have already executed the script') print( '3) You did not fully upgrade to the latest 1.9.11 revision before upgrading to 2.x' ) print( 'In case of (3), you need to install v1.9.11 and then upgrade the database before updating Indico back ' 'to {}'.format(indico.__version__)) sys.exit(1) plugins = sorted(x[23:] for x in tables if x.startswith('alembic_version_plugin_')) print('Resetting core alembic state...') _stamp() print('Plugins found: {}'.format(', '.join(plugins))) no_revision_plugins = {'audiovisual', 'payment_cern'} for plugin in no_revision_plugins: # All revisions were just data migrations -> get rid of them if plugin not in plugins: continue print('[{}] Deleting revision table'.format(plugin)) db.session.execute( 'DROP TABLE alembic_version_plugin_{}'.format(plugin)) plugin_revisions = { 'chat': '3888761f35f7', 'livesync': 'aa0dbc6c14aa', 'outlook': '6093a83228a7', 'vc_vidyo': '6019621fea50' } for plugin, revision in plugin_revisions.iteritems(): if plugin not in plugins: continue print('[{}] Stamping to new revision'.format(plugin)) _stamp(plugin, revision) db.session.commit()
def prepare(): """Initializes an empty database (creates tables, sets alembic rev to HEAD)""" tables = get_all_tables(db) if 'alembic_version' not in tables['public']: print colored('Setting the alembic version to HEAD', 'green') stamp() # Retrieve the table list again, that way we fail if the alembic version table was not created tables = get_all_tables(db) tables['public'].remove('alembic_version') if any(tables.viewvalues()): print colored('Your database is not empty!', 'red') print colored('If you just added a new table/model, create an alembic revision instead!', 'yellow') print print 'Tables in your database:' for schema, schema_tables in sorted(tables.items()): for t in schema_tables: print cformat(' * %{cyan}{}%{reset}.%{cyan!}{}%{reset}').format(schema, t) return print colored('Creating tables', 'green') db.create_all()
def prepare_db(empty=False, root_path=None, verbose=True): """Initialize an empty database (create tables, set alembic rev to HEAD).""" if not _require_pg_version('9.6'): return if not _require_encoding('UTF8'): return if not _require_extensions('unaccent', 'pg_trgm'): return root_path = root_path or current_app.root_path tables = get_all_tables(db) if 'alembic_version' not in tables['public']: if verbose: print(cformat('%{green}Setting the alembic version to HEAD')) stamp(directory=os.path.join(root_path, 'migrations'), revision='heads') PluginScriptDirectory.dir = os.path.join(root_path, 'core', 'plugins', 'alembic') alembic.command.ScriptDirectory = PluginScriptDirectory plugin_msg = cformat("%{cyan}Setting the alembic version of the %{cyan!}{}%{reset}%{cyan} " "plugin to HEAD%{reset}") for plugin in plugin_engine.get_active_plugins().itervalues(): if not os.path.exists(plugin.alembic_versions_path): continue if verbose: print(plugin_msg.format(plugin.name)) with plugin.plugin_context(): stamp(revision='heads') # Retrieve the table list again, just in case we created unexpected tables tables = get_all_tables(db) tables['public'] = [t for t in tables['public'] if not t.startswith('alembic_version')] if any(tables.viewvalues()): if verbose: print(cformat('%{red}Your database is not empty!')) print(cformat('%{yellow}If you just added a new table/model, create an alembic revision instead!')) print() print('Tables in your database:') for schema, schema_tables in sorted(tables.items()): for t in schema_tables: print(cformat(' * %{cyan}{}%{reset}.%{cyan!}{}%{reset}').format(schema, t)) return create_all_tables(db, verbose=verbose, add_initial_data=(not empty))
def prepare(): """Initializes an empty database (creates tables, sets alembic rev to HEAD)""" tables = get_all_tables(db) if 'alembic_version' not in tables['public']: print colored('Setting the alembic version to HEAD', 'green') stamp() # Retrieve the table list again, that way we fail if the alembic version table was not created tables = get_all_tables(db) tables['public'].remove('alembic_version') if any(tables.viewvalues()): print colored('Your database is not empty!', 'red') print colored( 'If you just added a new table/model, create an alembic revision instead!', 'yellow') print print 'Tables in your database:' for schema, schema_tables in sorted(tables.items()): for t in schema_tables: print cformat( ' * %{cyan}{}%{reset}.%{cyan!}{}%{reset}').format( schema, t) return print colored('Creating tables', 'green') db.create_all()
def setup(logger, zodb_root, sqlalchemy_uri, dblog=False, restore=False): app = IndicoFlask('indico_migrate') app.config['PLUGINENGINE_NAMESPACE'] = 'indico.plugins' app.config['SQLALCHEMY_DATABASE_URI'] = sqlalchemy_uri app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True _monkeypatch_config() plugin_engine.init_app(app) if not plugin_engine.load_plugins(app): print( cformat('%[red!]Could not load some plugins: {}%[reset]').format( ', '.join(plugin_engine.get_failed_plugins(app)))) sys.exit(1) db.init_app(app) if dblog: app.debug = True apply_db_loggers(app) import_all_models() configure_mappers() alembic_migrate.init_app(app, db, os.path.join(app.root_path, 'migrations')) try: tz = pytz.timezone( getattr(zodb_root['MaKaCInfo']['main'], '_timezone', 'UTC')) except KeyError: tz = pytz.utc with app.app_context(): if not restore: all_tables = sum(get_all_tables(db).values(), []) if all_tables: if db_has_data(): logger.fatal_error( 'Your database is not empty!\n' 'If you want to reset it, please drop and recreate it first.' ) else: # the DB is empty, prepare DB tables prepare_db(empty=True, root_path=get_root_path('indico'), verbose=False) return app, tz
def reset_alembic(): """Resets the alembic state carried over from 1.9.x Only run this command right after upgrading from a 1.9.x version so the references to old alembic revisions (which were removed in 2.0) are reset. """ tables = get_all_tables(db)['public'] if 'alembic_version' not in tables: print 'No alembic_version table found' sys.exit(1) current_revs = [rev for rev, in db.session.execute('SELECT version_num FROM alembic_version').fetchall()] if current_revs != ['65c079b091bf']: print ('Your database is not at the latest 1.9.11 revision (got [{}], expected [65c079b091bf]).' .format(', '.join(current_revs))) print 'This can have multiple reasons:' print '1) You did not upgrade from 1.9.x, so you do not need this command' print '2) You have already executed the script' print '3) You did not fully upgrade to the latest 1.9.11 revision before upgrading to 2.x' print ('In case of (3), you need to install v1.9.11 and then upgrade the database before updating Indico back ' 'to {}'.format(indico.__version__)) sys.exit(1) plugins = sorted(x[23:] for x in tables if x.startswith('alembic_version_plugin_')) print 'Resetting core alembic state...' _stamp() print 'Plugins found: {}'.format(', '.join(plugins)) no_revision_plugins = {'audiovisual', 'payment_cern'} for plugin in no_revision_plugins: # All revisions were just data migrations -> get rid of them if plugin not in plugins: continue print '[{}] Deleting revision table'.format(plugin) db.session.execute('DROP TABLE alembic_version_plugin_{}'.format(plugin)) plugin_revisions = {'chat': '3888761f35f7', 'livesync': 'aa0dbc6c14aa', 'outlook': '6093a83228a7', 'vc_vidyo': '6019621fea50'} for plugin, revision in plugin_revisions.iteritems(): if plugin not in plugins: continue print '[{}] Stamping to new revision'.format(plugin) _stamp(plugin, revision) db.session.commit()