refactor: Introduce migration function to streamline upgrade steps in Alembic migrations
All checks were successful
Deploy to Production, build images and push to Gitea Registry / build_and_push (pull_request) Successful in 1m17s
All checks were successful
Deploy to Production, build images and push to Gitea Registry / build_and_push (pull_request) Successful in 1m17s
This commit is contained in:
parent
8f1da5d440
commit
5dcabd51f7
@ -17,6 +17,16 @@ sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), '..'
|
|||||||
from app.database import Base as DatabaseBase
|
from app.database import Base as DatabaseBase
|
||||||
from app.config import settings
|
from app.config import settings
|
||||||
|
|
||||||
|
def _get_migration_fn(script_directory, current_rev):
|
||||||
|
"""Create a migration function that knows how to upgrade from current revision."""
|
||||||
|
def migration_fn(rev, context):
|
||||||
|
# Get all upgrade steps from current revision to head
|
||||||
|
revisions = script_directory._upgrade_revs("head", current_rev)
|
||||||
|
for revision in revisions:
|
||||||
|
script = script_directory.get_revision(revision)
|
||||||
|
script.module.upgrade(context)
|
||||||
|
return migration_fn
|
||||||
|
|
||||||
async def run_migrations():
|
async def run_migrations():
|
||||||
"""Run database migrations asynchronously."""
|
"""Run database migrations asynchronously."""
|
||||||
# Get alembic configuration and script directory
|
# Get alembic configuration and script directory
|
||||||
@ -52,10 +62,10 @@ async def run_migrations():
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Set the migration function
|
||||||
|
migration_context._migrations_fn = _get_migration_fn(script_directory, current_rev)
|
||||||
|
|
||||||
with migration_context.begin_transaction():
|
with migration_context.begin_transaction():
|
||||||
migration_context._migrations_fn = script_directory._upgrade_revs(
|
|
||||||
"head", current_rev
|
|
||||||
)
|
|
||||||
migration_context.run_migrations()
|
migration_context.run_migrations()
|
||||||
|
|
||||||
await connection.run_sync(upgrade_to_head)
|
await connection.run_sync(upgrade_to_head)
|
||||||
|
Loading…
Reference in New Issue
Block a user