Exemplo n.º 1
0
def test_04_find_migration_string():
    """Verify that a particular migration can be found by the version string match"""
    config = pydbvolve.initialize(TEST_CONFIG_FILE, 'upgrade', 'r1.1.0', True,
                                  False)
    migrations = pydbvolve.setup_migrations(config)
    res = pydbvolve.find_migration_file_version(config, migrations, 'r1.0.0')
    assert (isinstance(res, int))
    assert (res > -1)
    res = pydbvolve.find_migration_file_version(config, migrations,
                                                'r99.99.999')
    assert (res is None)
Exemplo n.º 2
0
def test_05_run_downgrade_migration_job_serial():
    """Verify that downgrade migrations can be applied serially"""
    try:
        os.unlink(TEST_DB_FILE)
    except:
        pass

    config = pydbvolve.initialize(TEST_CONFIG_FILE, 'upgrade', 'r1.2.0', True,
                                  False)
    migrateTableExists = pydbvolve.check_migration_table(config)
    assert (not migrateTableExists)
    if not migrateTableExists:
        pydbvolve.create_migration_table(config)

    migrations = pydbvolve.setup_migrations(config)
    res = pydbvolve.find_migration_file_version(config, migrations, 'r1.2.0')
    rc = pydbvolve.run_migration_job(config, migrations, 0, res, 1)
    assert (rc)

    config = pydbvolve.initialize(TEST_CONFIG_FILE, 'downgrade', 'r1.0.0',
                                  True, False)
    currentVersion = pydbvolve.get_current(config)
    migrations = pydbvolve.setup_migrations(config)
    start = pydbvolve.find_migration_file_version(config, migrations,
                                                  currentVersion['version'])
    start -= 1
    target = pydbvolve.find_migration_file_version(config, migrations,
                                                   'r1.0.0')
    rc = pydbvolve.run_migration_job(config, migrations, start, target, -1)
    assert (rc)

    curr = pydbvolve.get_current(config)
    assert (curr['version'] == 'r1.0.0')

    all_migrations = pydbvolve.get_migration_data(config)
    assert (all_migrations[0]['version'] == 'r0.0.0')
    assert (all_migrations[0]['is_current'] == False)
    assert (all_migrations[1]['version'] == 'r1.0.0')
    assert (all_migrations[1]['is_current'] == False)
    assert (all_migrations[2]['version'] == 'r1.1.0')
    assert (all_migrations[2]['is_current'] == False)
    assert (all_migrations[3]['version'] == 'r1.2.0')
    assert (all_migrations[3]['is_current'] == False)
    assert (all_migrations[4]['version'] == 'r1.1.0')
    assert (all_migrations[4]['is_current'] == False)
    assert (all_migrations[5]['version'] == 'r1.0.0')
    assert (all_migrations[5]['is_current'] == True)

    os.unlink(TEST_DB_FILE)
Exemplo n.º 3
0
def test_02_run_python_migration():
    """Verify that a sql migration can be run successfully"""
    try:
        os.unlink(TEST_DB_FILE)
    except:
        pass

    config = pydbvolve.initialize(TEST_CONFIG_FILE, 'upgrade', 'r1.2.0', True,
                                  False)
    migrations = pydbvolve.setup_migrations(config)
    res = pydbvolve.find_migration_file_version(config, migrations, 'r1.2.0')
    migration = migrations[res]

    assert (migration['filetype'] == 'py')

    rc = pydbvolve.run_python_migration(config, migration)

    assert (rc)
    assert (_table_exists(config['conn'], 'users'))

    sql = """
select count(*) as "count" from "users";
"""
    with config['conn'].cursor() as cur:
        cur.execute(sql)
        res = cur.fetchone()['count']

    assert (res == 1)
    config['conn'].close()

    os.unlink(TEST_DB_FILE)
Exemplo n.º 4
0
def test_03_run_migration_job_not_serial():
    """Verify that a migration can be run and a migration record created"""
    def pre_script(config, migration):
        raise Exception("Force a condition")

    def post_script(config, migration):
        _drop_migration_table(config)

    try:
        os.unlink(TEST_DB_FILE)
    except:
        pass

    config = pydbvolve.initialize(TEST_CONFIG_FILE, 'upgrade', 'r1.0.0', True,
                                  False)
    migrateTableExists = pydbvolve.check_migration_table(config)
    assert (not migrateTableExists)
    if not migrateTableExists:
        pydbvolve.create_migration_table(config)

    migrations = pydbvolve.setup_migrations(config)
    res = pydbvolve.find_migration_file_version(config, migrations, 'r1.0.0')
    migration = migrations[res]

    exc = None
    try:
        rc = pydbvolve.run_migration_job(config, migrations, res, res - 1, 1)
    except Exception as e:
        exc = e

    assert (isinstance(exc, pydbvolve.MigrationError))

    exc = None
    try:
        rc = pydbvolve.run_migration_job(config, migrations, res - 1, res, -1)
    except Exception as e:
        exc = e

    assert (isinstance(exc, pydbvolve.MigrationError))

    rc = pydbvolve.run_migration_job(config, migrations, res, res, 1)
    assert (rc)

    curr = pydbvolve.get_current(config)
    assert (curr['version'] == 'r1.0.0')

    save = pydbvolve.pre_script
    pydbvolve.pre_script = pre_script
    rc = pydbvolve.run_migration_job(config, migrations, res + 1, res + 1, 1)
    assert (rc == False)
    pydbvolve.pre_script = save
    save = pydbvolve.post_script
    pydbvolve.post_script = post_script
    rc = pydbvolve.run_migration_job(config, migrations, res + 1, res + 1, 1)
    assert (rc == False)
    pydbvolve.post_script = save

    os.unlink(TEST_DB_FILE)
Exemplo n.º 5
0
def test_06_load_python_migration():
    """Verify that a python migration can be loaded into a module reference."""
    config = pydbvolve.initialize(TEST_CONFIG_FILE, 'upgrade', 'r1.2.0', True,
                                  False)
    migrations = pydbvolve.setup_migrations(config)
    res = pydbvolve.find_migration_file_version(config, migrations, 'r1.2.0')
    assert (isinstance(res, int))
    assert (res > -1)
    migration = migrations[res]
    pymigration = pydbvolve.import_arbitrary(migration['filename'],
                                             'pv_mg_' + migration['version'])
    elements = set(dir(pymigration))
    assert ('create_user_table' in elements)
    assert ('add_default_user' in elements)
    assert ('run_migration' in elements)
Exemplo n.º 6
0
def test_05_parse_sql_statements():
    """Verify that a sql migration file's statements can be parsed by the statement separator."""
    config = pydbvolve.initialize(TEST_CONFIG_FILE, 'upgrade', 'r1.1.0', True,
                                  False)
    migrations = pydbvolve.setup_migrations(config)
    res = pydbvolve.find_migration_file_version(config, migrations, 'r1.0.0')
    assert (isinstance(res, int))
    assert (res > -1)
    sqlfile = open(migrations[res]['filename'], 'r')
    sep = pydbvolve.get_sql_statement_sep()
    assert (sep.search('-- run') is not None)
    statements = [stmt for stmt in pydbvolve.get_statements(sqlfile)]
    sqlfile.close()
    assert (len(statements) == 6)
    assert ('create table person' in statements[0])
    assert ('unique index ix01_person' in statements[1])
    assert ('ix02_person' in statements[2])
    assert ('ix03_person' in statements[3])
    assert ('create table school' in statements[4])
    assert ('insert into school' in statements[5])
Exemplo n.º 7
0
def test_01_run_sql_migration():
    """Verify that a sql migration can be run successfully"""
    try:
        os.unlink(TEST_DB_FILE)
    except:
        pass

    config = pydbvolve.initialize(TEST_CONFIG_FILE, 'upgrade', 'r1.0.0', True,
                                  False)
    migrations = pydbvolve.setup_migrations(config)
    res = pydbvolve.find_migration_file_version(config, migrations, 'r1.0.0')
    migration = migrations[res]
    rc = pydbvolve.run_sql_migration(config, migration)

    assert (rc)
    assert (_table_exists(config['conn'], 'person'))
    assert (_table_exists(config['conn'], 'school'))
    config['conn'].close()

    os.unlink(TEST_DB_FILE)
Exemplo n.º 8
0
def test_04_run_migration_job_serial():
    """Verify that a migrations can be applied serially"""
    try:
        os.unlink(TEST_DB_FILE)
    except:
        pass

    config = pydbvolve.initialize(TEST_CONFIG_FILE, 'upgrade', 'r1.1.0', True,
                                  False)
    migrateTableExists = pydbvolve.check_migration_table(config)
    assert (not migrateTableExists)
    if not migrateTableExists:
        pydbvolve.create_migration_table(config)

    migrations = pydbvolve.setup_migrations(config)
    res = pydbvolve.find_migration_file_version(config, migrations, 'r1.1.0')
    migration = migrations[res]

    rc = pydbvolve.run_migration_job(config, migrations, 0, res, 1)
    assert (rc)

    curr = pydbvolve.get_current(config)
    assert (curr['version'] == 'r1.1.0')

    all_migrations = pydbvolve.get_migration_data(config)
    assert (all_migrations[0]['version'] == 'r0.0.0')
    assert (all_migrations[0]['is_current'] == False)
    assert (all_migrations[1]['version'] == 'r1.0.0')
    assert (all_migrations[1]['is_current'] == False)
    assert (all_migrations[2]['version'] == 'r1.1.0')
    assert (all_migrations[2]['is_current'] == True)

    _drop_migration_table(config)
    all_migrations = pydbvolve.get_migration_data(config)
    assert (all_migrations is None)

    os.unlink(TEST_DB_FILE)