コード例 #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
ファイル: api.py プロジェクト: peajayni/pgmigrations
 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
ファイル: api.py プロジェクト: peajayni/pgmigrations
    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)