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 is_applied(self, cursor): # When processing the core migrations the table may not exist yet if not data_access.table_exists(cursor, constants.MIGRATIONS_TABLE_NAME): return False return data_access.has_migration_been_applied(cursor, self.name)
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)