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 database(app, postgresql): """Creates a test database which is destroyed afterwards Used only internally, if you need to access the database use `db` instead to ensure your modifications are not persistent! """ app.config['SQLALCHEMY_DATABASE_URI'] = postgresql configure_db(app) if 'INDICO_TEST_DATABASE_URI' in os.environ and os.environ.get('INDICO_TEST_DATABASE_HAS_TABLES') == '1': yield db_ return with app.app_context(): create_all_tables(db_) yield db_ db_.session.remove() with app.app_context(): delete_all_tables(db_)
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))