예제 #1
0
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}
            """,
        )
예제 #2
0
 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)
예제 #3
0
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
예제 #4
0
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
예제 #5
0
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)
예제 #6
0
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)
예제 #7
0
    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)
예제 #8
0
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)
예제 #9
0
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)
예제 #10
0
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)
예제 #11
0
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)
예제 #12
0
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)
예제 #13
0
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)