27 lines
866 B
Python
27 lines
866 B
Python
from alembic import context
|
|
from sqlalchemy import create_engine, pool
|
|
|
|
from app import models # noqa: F401
|
|
from app.core.config import settings
|
|
from app.db.session import Base
|
|
|
|
config = context.config
|
|
|
|
|
|
def run_migrations(connection):
|
|
context.configure(connection=connection, target_metadata=Base.metadata, compare_type=True)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
raise RuntimeError("迁移需要连接数据库,以检查并保留已有表;请使用 alembic upgrade head")
|
|
elif config.attributes.get("connection") is not None:
|
|
run_migrations(config.attributes["connection"])
|
|
else:
|
|
engine = create_engine(
|
|
settings.database_url, poolclass=pool.NullPool, connect_args={"connect_timeout": 5}
|
|
)
|
|
with engine.connect() as connection:
|
|
run_migrations(connection)
|