Skip to content

Commit

Permalink
Feat(migration): Add rollback mechanism to ensure data integrity on m…
Browse files Browse the repository at this point in the history
…igration failure (#97)

* feat(migration): introduce rollback to protect our data in case of migration failure

* impr(migration): make error messages clearer
  • Loading branch information
tobiichi3227 authored Oct 11, 2024
1 parent 5832a7c commit 557b3ff
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions migration/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,22 @@ async def main():
module_name = filename[:-3]
module = importlib.import_module(module_name)

if not hasattr(module, 'dochange'):
print(f"{filename} is an invalid migration file because the function 'dochange()' was not found.")
continue

if not inspect.iscoroutinefunction(module.dochange):
print(f"The function 'dochange()' in {filename} must be asynchronous.")
continue

try:
if hasattr(module, 'dochange') and inspect.iscoroutinefunction(module.dochange):
async with db_conn.transaction():
await module.dochange(db_conn, redis_conn)

except Exception as e:
print(f"Error running migration file {filename}: {e}")
traceback.print_exc()
continue
break

await db_conn.execute('UPDATE db_version SET "version"=$1', version)

Expand Down

0 comments on commit 557b3ff

Please sign in to comment.