Compare commits
No commits in common. "26315cd407460425953eeef62891648228033ecc" and "8517cbee999d296d2a39889bf1e0bb63a73db88d" have entirely different histories.
26315cd407
...
8517cbee99
@ -6,10 +6,6 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
from sqlalchemy.ext.asyncio import create_async_engine
|
from sqlalchemy.ext.asyncio import create_async_engine
|
||||||
from sqlalchemy import pool
|
from sqlalchemy import pool
|
||||||
from alembic.config import Config
|
|
||||||
from alembic.script import ScriptDirectory
|
|
||||||
from alembic.runtime.migration import MigrationContext
|
|
||||||
from alembic.operations import Operations
|
|
||||||
|
|
||||||
# Ensure the app directory is in the Python path
|
# Ensure the app directory is in the Python path
|
||||||
sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), '..')))
|
sys.path.insert(0, os.path.realpath(os.path.join(os.path.dirname(__file__), '..')))
|
||||||
@ -19,9 +15,8 @@ from app.config import settings
|
|||||||
|
|
||||||
async def run_migrations():
|
async def run_migrations():
|
||||||
"""Run database migrations asynchronously."""
|
"""Run database migrations asynchronously."""
|
||||||
# Get alembic configuration and script directory
|
from alembic.runtime.migration import MigrationContext
|
||||||
alembic_cfg = Config(os.path.join(os.path.dirname(__file__), '..', 'alembic.ini'))
|
from alembic.operations import Operations
|
||||||
script_directory = ScriptDirectory.from_config(alembic_cfg)
|
|
||||||
|
|
||||||
# Create async engine
|
# Create async engine
|
||||||
engine = create_async_engine(
|
engine = create_async_engine(
|
||||||
@ -30,34 +25,33 @@ async def run_migrations():
|
|||||||
)
|
)
|
||||||
|
|
||||||
async with engine.connect() as connection:
|
async with engine.connect() as connection:
|
||||||
def get_current_rev(conn):
|
# Get current database schema version
|
||||||
|
def do_get_current_rev(conn):
|
||||||
migration_context = MigrationContext.configure(
|
migration_context = MigrationContext.configure(
|
||||||
conn,
|
conn,
|
||||||
opts={
|
opts={
|
||||||
'target_metadata': DatabaseBase.metadata,
|
'target_metadata': DatabaseBase.metadata,
|
||||||
'script': script_directory
|
'compare_type': True,
|
||||||
|
'compare_server_default': True
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
return migration_context.get_current_revision()
|
return migration_context.get_current_revision()
|
||||||
|
|
||||||
current_rev = await connection.run_sync(get_current_rev)
|
current_rev = await connection.run_sync(do_get_current_rev)
|
||||||
|
|
||||||
def upgrade_to_head(conn):
|
# Run migrations
|
||||||
|
def do_upgrade(conn):
|
||||||
migration_context = MigrationContext.configure(
|
migration_context = MigrationContext.configure(
|
||||||
conn,
|
conn,
|
||||||
opts={
|
opts={
|
||||||
'target_metadata': DatabaseBase.metadata,
|
'target_metadata': DatabaseBase.metadata,
|
||||||
'script': script_directory,
|
'compare_type': True,
|
||||||
'as_sql': False,
|
'compare_server_default': True
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
with Operations.context(migration_context):
|
||||||
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(do_upgrade)
|
||||||
|
|
||||||
await engine.dispose()
|
await engine.dispose()
|
Loading…
Reference in New Issue
Block a user