def test_migration(shelf_filename, cld_host, dbexport_host, username, password, service_binding): PENDING = sql_from_folder(PENDING_FOLDER) with tempfolder() as tempf: outfile = os.path.join(tempf, 'schemadump.sql') do_schema_dump(outfile, cld_host, dbexport_host, username, password, service_binding) for i in range(len(PENDING) + 1): ATTEMPTING = list(reversed(PENDING))[:i] ATTEMPTING.reverse() print("TESTING MIGRATION USING LAST {} MIGRATION FILES".format(i)) with temporary_db() as dummy_db_url, temporary_db() as target_db_url: with S(dummy_db_url) as s_dummy: load_sql_from_file(s_dummy, outfile) try: for migration_sql in ATTEMPTING: raw_execute(s_dummy, migration_sql) except DB_ERROR_TUPLE: print('TRIED USING LAST {} PENDING FILES TO MIGRATE BUT THIS FAILED, MOVING TO NEXT'.format(i)) continue load_from_app_model(target_db_url) if databases_are_equal(dummy_db_url, target_db_url): print('SUCCESS WITH LAST {} PENDING FILES'.format(i)) shelve_result(shelf_filename, ATTEMPTING) return 0 else: print('TRIED USING LAST {} PENDING FILES TO MIGRATE BUT THIS DOES NOT GIVE A CORRECT OUTCOME, MOVING TO NEXT'.format(i)) print('COULD NOT FIND A CORRECT MIGRATION PATH :(') return 1
def pending(write_to_file=False): with temporary_db() as CURRENT_DB_URL, temporary_db() as TARGET_DB_URL: load_current_production_state(CURRENT_DB_URL) load_current_staging_state(TARGET_DB_URL) with S(CURRENT_DB_URL) as s_current, S(TARGET_DB_URL) as s_target: m = Migration(s_current, s_target) m.set_safety(False) m.add_all_changes() print('-- Pending (prod -> staging):\n\n{}'.format(m.sql)) if write_to_file: with io.open('DB/migration/pending/pending.sql', 'w') as w: w.write(m.sql) with temporary_db() as CURRENT_DB_URL, temporary_db() as TARGET_DB_URL: load_current_staging_state(CURRENT_DB_URL) load_from_app_model(TARGET_DB_URL) with S(CURRENT_DB_URL) as s_current, S(TARGET_DB_URL) as s_target: m = Migration(s_current, s_target) m.set_safety(False) m.add_all_changes() print('-- Pending (staging -> models):\n\n{}'.format(m.sql)) if write_to_file: with io.open('DB/migration/pending/pending.sql', 'w') as w: w.write(m.sql)
def staging_errors(): with temporary_db() as CURRENT_DB_URL, temporary_db() as TARGET_DB_URL: load_current_staging_state(CURRENT_DB_URL) load_current_production_state(TARGET_DB_URL) with S(CURRENT_DB_URL) as s_current, S(TARGET_DB_URL) as s_target: m = Migration(s_current, s_target) m.set_safety(False) m.add_all_changes() print('Differences:\n{}'.format(m.sql))
def check_migration_result(): with temporary_db() as CURRENT_DB_URL, temporary_db() as TARGET_DB_URL: load_post_migration_state(CURRENT_DB_URL) load_from_app_model(TARGET_DB_URL) with S(CURRENT_DB_URL) as s_current, S(TARGET_DB_URL) as s_target: m = Migration(s_current, s_target) m.set_safety(False) m.add_all_changes() print('Differences:\n{}'.format(m.sql))
def do_migration(REAL_DB_URL): PENDING = sql_from_folder(PENDING_FOLDER) with tempfolder() as tempf: outfile = os.path.join(tempf, 'schemadump.sql') do_schema_dump(REAL_DB_URL, outfile) for i in range(len(PENDING) + 1): ATTEMPTING = list(reversed(PENDING))[:i] ATTEMPTING.reverse() print("TESTING MIGRATION USING LAST {} MIGRATION FILES".format(i)) with temporary_db() as dummy_db_url, temporary_db( ) as target_db_url: with S(dummy_db_url) as s_dummy: load_sql_from_file(s_dummy, outfile) try: for migration_sql in ATTEMPTING: raw_execute(s_dummy, migration_sql) except DB_ERROR_TUPLE as e: print( 'TRIED USING LAST {} PENDING FILES TO MIGRATE BUT THIS FAILED, MOVING TO NEXT' .format(i)) continue load_from_app_model(target_db_url) if databases_are_equal(dummy_db_url, target_db_url): print('APPLYING LAST {} PENDING FILES'.format(i)) with S(REAL_DB_URL) as s_real: for migration_sql in ATTEMPTING: if not DRY_RUN: print("EXECUTING:") print(migration_sql) raw_execute(s_real, migration_sql) else: print('DRY RUN, would apply:') print(migration_sql) print('SUCCESS: DATABASE UP TO DATE.') return 0 else: print( 'TRIED USING LAST {} PENDING FILES TO MIGRATE BUT THIS DOES NOT GIVE A CORRECT OUTCOME, MOVING TO NEXT' .format(i)) print('COULD NOT FIND A CORRECT MIGRATION PATH :(') return 1
def sync(DB_URL: str = "postgresql://orflaedi:@localhost/orflaedi"): from sqlbag import S, temporary_database as temporary_db with temporary_db() as TEMP_DB_URL: os.environ["DATABASE_URL"] = TEMP_DB_URL from orflaedi.database import engine, Base from orflaedi.models import Model, Retailer, VehicleClassEnum, TagEnum Base.metadata.create_all(engine) with S(DB_URL) as s_current, S(TEMP_DB_URL) as s_target: m = Migration(s_current, s_target) m.set_safety(False) m.add_all_changes() if m.statements: print("THE FOLLOWING CHANGES ARE PENDING:", end="\n\n") print(m.sql) print() if input("Apply these changes?") == "yes": print("Applying...") m.apply() else: print("Not applying.") else: print("Already synced.")
def dump(): from sqlbag import S, temporary_database as temporary_db, load_sql_from_file from migra import Migration DB_URL_FUTURE = 'postgresql:///cuedev' with temporary_db() as DB_URL_CURRENT: with S(DB_URL_FUTURE) as s_current, S(DB_URL_CURRENT) as s_target: load_sql_from_file(s_target, './host.sql') m = Migration(s_target, s_current) m.set_safety(False) m.add_all_changes() if m.statements: print('THE FOLLOWING CHANGES ARE NOT IMMORTALISED:', end='\n\n') print(m.sql) print() os.system( 'pg_dump -s --no-comments --no-owner --no-acl --no-tablespaces postgresql:///cuedev | sed -e "/^--/d" > host.sql' ) if input('Dump Dev to host.sql?') == 'yes': print('Dumping...') else: print('Not applying.') else: print('Already synced.')
def do_migration(REAL_DB_URL): PENDING = sql_from_folder(PENDING_FOLDER) with tempfolder() as tempf: outfile = os.path.join(tempf, 'schemadump.sql') do_schema_dump(REAL_DB_URL, outfile) for i in range(len(PENDING) + 1): ATTEMPTING = list(reversed(PENDING))[:i] ATTEMPTING.reverse() print("TESTING MIGRATION USING LAST {} MIGRATION FILES".format(i)) with temporary_db() as dummy_db_url, temporary_db() as target_db_url: with S(dummy_db_url) as s_dummy: load_sql_from_file(s_dummy, outfile) try: for migration_sql in ATTEMPTING: raw_execute(s_dummy, migration_sql) except DB_ERROR_TUPLE as e: print('TRIED USING LAST {} PENDING FILES TO MIGRATE BUT THIS FAILED, MOVING TO NEXT'.format(i)) continue load_from_app_model(target_db_url) if databases_are_equal(dummy_db_url, target_db_url): print('APPLYING LAST {} PENDING FILES'.format(i)) with S(REAL_DB_URL) as s_real: for migration_sql in ATTEMPTING: if not DRY_RUN: print("EXECUTING:") print(migration_sql) raw_execute(s_real, migration_sql) else: print('DRY RUN, would apply:') print(migration_sql) print('SUCCESS: DATABASE UP TO DATE.') return 0 else: print('TRIED USING LAST {} PENDING FILES TO MIGRATE BUT THIS DOES NOT GIVE A CORRECT OUTCOME, MOVING TO NEXT'.format(i)) print('COULD NOT FIND A CORRECT MIGRATION PATH :(') return 1
def sync(): DB_URL = get_current_app_db_url() with temporary_db() as TEMP_DB_URL: load_from_app_model(TEMP_DB_URL) with S(DB_URL) as s_current, S(TEMP_DB_URL) as s_target: m = Migration(s_current, s_target) m.set_safety(False) m.add_all_changes() if m.statements: print('THE FOLLOWING CHANGES ARE PENDING:', end='\n\n') print(m.sql) print('Applying...') m.apply() else: print('Already synced.')