def clean_database(table_name): with data_access.get_cursor(DSN) as cursor: data_access.execute_sql( cursor, f""" DROP TABLE IF EXISTS {table_name} """, )
def apply(self, dsn): LOGGER.debug("%s - running apply", self) with data_access.get_cursor(dsn) as cursor: if self.is_applied(cursor): LOGGER.debug("%s - nothing to do", self) return data_access.execute_sql(cursor, self.apply_script.sql) data_access.record_apply(cursor, self.name) LOGGER.debug("%s - apply succeeded", self)
def migration(table, migration_name): with data_access.get_cursor(DSN) as cursor: data_access.execute_sql( cursor, f""" INSERT INTO {table} (name) VALUES ('{migration_name}') """, ) return migration_name
def table(table_name): with data_access.get_cursor(DSN) as cursor: data_access.execute_sql( cursor, f""" CREATE TABLE {table_name} ( name varchar(200) PRIMARY KEY ) """, ) return table_name
def test_rollback(): migrations = Migrations(locations=[BASE_LOCATION]) migrations.apply(DSN) for table, migration in zip(["second", "first"], reversed(migrations.migrations[-2:])): migrations.rollback(DSN, migration.name) with data_access.get_cursor(DSN) as cursor: assert not data_access.table_exists(cursor, table) assert not data_access.has_migration_been_applied( cursor, migration.name)
def test_apply(): migrations = Migrations(locations=[BASE_LOCATION]) migrations.apply(DSN) expected_tables = ["first", "second"] with data_access.get_cursor(DSN) as cursor: for table in expected_tables: assert data_access.table_exists(cursor, table) for migration in migrations.migrations: assert data_access.has_migration_been_applied( cursor, migration.name)
def rollback(self, dsn): LOGGER.debug("%s - running rollback", self) with data_access.get_cursor(dsn) as cursor: if not self.is_applied(cursor): LOGGER.debug("%s - nothing to do", self) return data_access.execute_sql(cursor, self.rollback_script.sql) # When processing the core migrations the table may not exist yet if data_access.table_exists(cursor, constants.MIGRATIONS_TABLE_NAME): data_access.record_rollback(cursor, self.name) LOGGER.debug("%s - rollback succeeded", self)
def clean_database(): tables = [constants.MIGRATIONS_TABLE_NAME, "first", "second"] with data_access.get_cursor(DSN) as cursor: for table in tables: if data_access.table_exists(cursor, table): data_access.drop_table(cursor, table)
def test_record_migration(table, migration): with data_access.get_cursor(DSN) as cursor: assert data_access.has_migration_been_applied(cursor, migration) data_access.record_rollback(cursor, migration) assert not data_access.has_migration_been_applied(cursor, migration)
def test_has_migration_been_applied_when_not_applied(table, migration_name): with data_access.get_cursor(DSN) as cursor: assert not data_access.has_migration_been_applied( cursor, migration_name)
def test_has_migration_been_applied_when_applied(migration): with data_access.get_cursor(DSN) as cursor: assert data_access.has_migration_been_applied(cursor, migration)
def test_drop_table(table): with data_access.get_cursor(DSN) as cursor: assert data_access.table_exists(cursor, table) data_access.drop_table(cursor, table) assert not data_access.table_exists(cursor, table)
def test_table_exists_when_not_exists(table_name): with data_access.get_cursor(DSN) as cursor: assert not data_access.table_exists(cursor, table_name)