def test_05_check_migration_table_bad_baseline(): """Verify that multiple baseline recurds are detected""" config = pydbvolve.initialize(TEST_CONFIG_FILE, 'info', 'r1.1.10', True, False) _drop_migration_table(config) res = pydbvolve.create_migration_table(config) assert(res) record = { 'version': 'r1.0.0', 'applied_ts': datetime.datetime.now(), 'migration_file': 'somefile.sql', 'migration_type': 'sql', 'migration_user': config['migration_user'], 'db_user': config['db_user'], 'is_current': 0, 'is_baseline': 1 } _add_migration_record(config, record) record['version'] = 'r1.0.1' _add_migration_record(config, record) try: res = pydbvolve.check_migration_table(config) except Exception as e: res = False assert(isinstance(e, pydbvolve.MigrationTableConstraintError)) assert(res == False)
def test_06_set_baseline(): """Verify set_baseline functions""" try: os.unlink(TEST_DB_FILE) except: pass config = pydbvolve.initialize(TEST_CONFIG_FILE, 'baseline', 'r0.0.0', True, False) migrateTableExists = pydbvolve.check_migration_table(config) assert (not migrateTableExists) if not migrateTableExists: pydbvolve.create_migration_table(config) rc = pydbvolve.set_baseline(config) assert (rc == 0) baseline = pydbvolve.get_baseline(config) assert (baseline['version'] == 'r0.0.0') assert (baseline['is_baseline'] == True) _drop_migration_table(config) rc = pydbvolve.set_baseline(config) assert (rc != 0) os.unlink(TEST_DB_FILE)
def test_09_verify_version(): """Verify set_baseline functions""" try: os.unlink(TEST_DB_FILE) except: pass config = pydbvolve.initialize(TEST_CONFIG_FILE, 'upgrade', 'r1.3.0', True, False) migrateTableExists = pydbvolve.check_migration_table(config) assert (not migrateTableExists) if not migrateTableExists: pydbvolve.create_migration_table(config) pydbvolve.set_baseline(config) config['conn'].close() config = pydbvolve.initialize(TEST_CONFIG_FILE, 'verify', 'r1.3.0', True, False) rc = pydbvolve.verify_version(config) assert (rc == 0) config['version'] = 'r1.1.1' rc = pydbvolve.verify_version(config) assert (rc != 0) pydbvolve.clear_current(config) rc = pydbvolve.verify_version(config) assert (rc != 0) config['conn'].close() os.unlink(TEST_DB_FILE)
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)
def test_03_create_migration_table(): """Verify that the migration table can be created""" config = pydbvolve.initialize(TEST_CONFIG_FILE, 'info', 'r1.1.10', True, False) _drop_migration_table(config) res = pydbvolve.create_migration_table(config) assert(res == True) assert(pydbvolve.check_migration_table(config)) res = pydbvolve.create_migration_table(config) assert(res == False)
def test_08_run_downgrade(): """Verify set_baseline functions""" def no_migrations(*args, **kwargs): return [] try: os.unlink(TEST_DB_FILE) except: pass config = pydbvolve.initialize(TEST_CONFIG_FILE, 'baseline', 'r1.2.0', True, False) migrateTableExists = pydbvolve.check_migration_table(config) assert (not migrateTableExists) if not migrateTableExists: pydbvolve.create_migration_table(config) pydbvolve.set_baseline(config) config['conn'].close() config = pydbvolve.initialize(TEST_CONFIG_FILE, 'upgrade', 'r1.3.0', True, False) rc = pydbvolve.run_upgrade(config) assert (rc == 0) config['conn'].close() config = pydbvolve.initialize(TEST_CONFIG_FILE, 'downgrade', 'r1.2.0', True, False) rc = pydbvolve.run_downgrade(config) assert (rc == 0) curr = pydbvolve.get_current(config) assert (curr['version'] == 'r1.2.0') config = pydbvolve.initialize(TEST_CONFIG_FILE, 'downgrade', 'r1.2.0', True, False) rc = pydbvolve.run_downgrade(config) assert (rc == 0) save = pydbvolve.setup_migrations pydbvolve.setup_migrations = no_migrations config = pydbvolve.initialize(TEST_CONFIG_FILE, 'downgrade', 'r1.1.0', True, False) rc = pydbvolve.run_upgrade(config) assert (rc != 0) pydbvolve.setup_migrations = save config = pydbvolve.initialize(TEST_CONFIG_FILE, 'downgrade', 'r1.1.0', True, False) rc = pydbvolve.run_upgrade(config) assert (rc != 0) config['conn'].close() importlib.reload(pydbvolve) os.unlink(TEST_DB_FILE)
def test_02_check_migration_table_bad_structure(): """Verify that an aberrant table structure can be detected""" config = pydbvolve.initialize(TEST_CONFIG_FILE, 'info', 'r1.1.10', True, False) _create_bad_migration_table(config) exc = None try: res = pydbvolve.check_migration_table(config) except Exception as e: exc = e assert(isinstance(exc, pydbvolve.MigrationTableOutOfSync))
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)
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)
def test_07_run_upgrade(): """Verify set_baseline functions""" def no_migrations(*args, **kwargs): return [] try: os.unlink(TEST_DB_FILE) except: pass config = pydbvolve.initialize(TEST_CONFIG_FILE, 'baseline', 'r1.1.0', True, False) migrateTableExists = pydbvolve.check_migration_table(config) assert (not migrateTableExists) if not migrateTableExists: pydbvolve.create_migration_table(config) rc = pydbvolve.set_baseline(config) assert (rc == 0) config = pydbvolve.initialize(TEST_CONFIG_FILE, 'upgrade', 'r1.2.0', True, False) rc = pydbvolve.run_upgrade(config) assert (rc == 0) config['conn'].close() # cannot upgrade to an earlier version than current config = pydbvolve.initialize(TEST_CONFIG_FILE, 'upgrade', 'r1.1.0', True, False) rc = pydbvolve.run_upgrade(config) assert (rc != 0) config['conn'].close() # can upgrade to later version config = pydbvolve.initialize(TEST_CONFIG_FILE, 'upgrade', 'r1.3.0', True, False) rc = pydbvolve.run_upgrade(config) assert (rc == 0) all_migrations = pydbvolve.get_migration_data(config) all_versions = set(m['version'] for m in all_migrations) assert ('r1.2.0' in all_versions) assert ('r1.2.9' in all_versions) assert ('r1.3.0' in all_versions) assert ('r1.3.1' not in all_versions) save = pydbvolve.setup_migrations pydbvolve.setup_migrations = no_migrations config = pydbvolve.initialize(TEST_CONFIG_FILE, 'upgrade', 'r1.3.0', True, False) rc = pydbvolve.run_upgrade(config) assert (rc != 0) pydbvolve.setup_migrations = save config = pydbvolve.initialize(TEST_CONFIG_FILE, 'upgrade', 'r1.3.0', True, False) rc = pydbvolve.run_upgrade(config) assert (rc == 0) config = pydbvolve.initialize(TEST_CONFIG_FILE, 'baseline', 'r1.2.0', True, False) rc = pydbvolve.set_baseline(config) assert (rc == 0) pydbvolve.setup_migrations = no_migrations config = pydbvolve.initialize(TEST_CONFIG_FILE, 'upgrade', 'r1.0.0', True, False) rc = pydbvolve.run_upgrade(config) assert (rc != 0) config['conn'].close() importlib.reload(pydbvolve) os.unlink(TEST_DB_FILE)