def init_test_db(force=False): """Same as `init_db` command, but creates a database that will be used to run tests. `TEST_SQLALCHEMY_DATABASE_URI` variable must be defined in the config file. """ db.init_db_engine(config.POSTGRES_ADMIN_URI) if force: exit_code = db.run_sql_script_without_transaction(os.path.join(ADMIN_SQL_DIR, 'drop_test_db.sql')) if not exit_code: raise Exception('Failed to drop existing database and user! Exit code: %i' % exit_code) print('Creating database and user for testing...') exit_code = db.run_sql_script_without_transaction(os.path.join(ADMIN_SQL_DIR, 'create_test_db.sql')) if not exit_code: raise Exception('Failed to create new database and user! Exit code: %i' % exit_code) exit_code = db.run_sql_script_without_transaction(os.path.join(ADMIN_SQL_DIR, 'create_extensions.sql')) if not exit_code: raise Exception('Failed to create database extensions! Exit code: %i' % exit_code) db.init_db_engine(config.TEST_SQLALCHEMY_DATABASE_URI) db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_tables.sql')) db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_primary_keys.sql')) db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_foreign_keys.sql')) db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_functions.sql')) db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_indexes.sql')) print("Done!")
def init_db(force): """Initializes database. This process involves several steps: 1. Table structure is created. 2. Primary keys and foreign keys are created. 3. Indexes are created. """ db.init_db_engine(config.POSTGRES_ADMIN_URI) if force: res = db.run_sql_script_without_transaction( os.path.join(ADMIN_SQL_DIR, 'drop_db.sql')) if not res: raise Exception( 'Failed to drop existing database and user! Exit code: %i' % exit_code) print('Creating user and a database...') res = db.run_sql_script_without_transaction( os.path.join(ADMIN_SQL_DIR, 'create_db.sql')) if not res: raise Exception( 'Failed to create new database and user! Exit code: %i' % exit_code) print('Creating database extensions...') exit_code = db.run_sql_script_without_transaction( os.path.join(ADMIN_SQL_DIR, 'create_extensions.sql')) app = create_app() with app.app_context(): print('Creating tables...') db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_tables.sql')) print('Creating primary and foreign keys...') db.run_sql_script( os.path.join(ADMIN_SQL_DIR, 'create_primary_keys.sql')) db.run_sql_script( os.path.join(ADMIN_SQL_DIR, 'create_foreign_keys.sql')) print('Creating indexes...') db.run_sql_script(os.path.join(ADMIN_SQL_DIR, 'create_indexes.sql')) print("Done!")
def reset_msb_db(self): msb.init_db_engine(config.MESSYBRAINZ_ADMIN_URI) msb.run_sql_script_without_transaction(os.path.join(MESSYBRAINZ_SQL_DIR, 'drop_db.sql')) msb.run_sql_script_without_transaction(os.path.join(MESSYBRAINZ_SQL_DIR, 'create_db.sql')) msb.engine.dispose() msb.init_db_engine(config.MESSYBRAINZ_ADMIN_MSB_URI) msb.run_sql_script_without_transaction(os.path.join(MESSYBRAINZ_SQL_DIR, 'create_extensions.sql')) msb.engine.dispose() msb.init_db_engine(config.MESSYBRAINZ_SQLALCHEMY_DATABASE_URI) msb.run_sql_script(os.path.join(MESSYBRAINZ_SQL_DIR, 'create_tables.sql')) msb.run_sql_script(os.path.join(MESSYBRAINZ_SQL_DIR, 'create_functions.sql')) msb.run_sql_script(os.path.join(MESSYBRAINZ_SQL_DIR, 'create_indexes.sql')) msb.run_sql_script(os.path.join(MESSYBRAINZ_SQL_DIR, 'create_primary_keys.sql')) msb.run_sql_script(os.path.join(MESSYBRAINZ_SQL_DIR, 'create_foreign_keys.sql')) msb.engine.dispose()
def create_db(self): db.run_sql_script_without_transaction( os.path.join(ADMIN_SQL_DIR, 'create_db.sql'))