| 12345678910111213141516171819 |
- """迁移使用同一集中配置;离线 SQL 与在线 SQLite 共享元数据。"""
- from alembic import context
- from sqlalchemy import create_engine
- from app.domain.models import Base
- from app.shared.config import settings
- url = settings.db_url.replace('sqlite+aiosqlite://', 'sqlite://')
- settings.data_path.mkdir(parents=True, exist_ok=True)
- if context.is_offline_mode():
- context.configure(url=url, target_metadata=Base.metadata, literal_binds=True, render_as_batch=True)
- with context.begin_transaction():
- context.run_migrations()
- else:
- engine = create_engine(url)
- with engine.connect() as connection:
- context.configure(connection=connection, target_metadata=Base.metadata, render_as_batch=True)
- with context.begin_transaction():
- context.run_migrations()
- engine.dispose()
|