db867dcbe5
Deploy to Staging / deploy (push) Has been cancelled
Conflict detector / main (push) Has been cancelled
Lint Backend / lint-backend (push) Has been cancelled
Playwright Tests / changes (push) Has been cancelled
Test Backend / test-backend (push) Has been cancelled
Test Docker Compose / test-docker-compose (push) Has been cancelled
Playwright Tests / test-playwright (1, 4) (push) Has been cancelled
Playwright Tests / test-playwright (2, 4) (push) Has been cancelled
Playwright Tests / test-playwright (3, 4) (push) Has been cancelled
Playwright Tests / test-playwright (4, 4) (push) Has been cancelled
Playwright Tests / merge-playwright-reports (push) Has been cancelled
Playwright Tests / alls-green-playwright (push) Has been cancelled
Issue Manager / issue-manager (push) Has been cancelled
81 lines
2.2 KiB
Python
Executable File
81 lines
2.2 KiB
Python
Executable File
import os
|
|
from logging.config import fileConfig
|
|
|
|
from alembic import context
|
|
from sqlalchemy import engine_from_config, pool
|
|
|
|
# 这是 Alembic 配置对象,提供对正在使用的 .ini 文件中值的访问。
|
|
config = context.config
|
|
|
|
# 解析配置文件以进行 Python 日志记录。
|
|
# 这行代码用于设置日志记录器。
|
|
fileConfig(config.config_file_name)
|
|
|
|
# 在此处添加模型的 MetaData 对象
|
|
# 以支持 'autogenerate'
|
|
# from myapp import mymodel
|
|
# target_metadata = mymodel.Base.metadata
|
|
# target_metadata = None
|
|
|
|
from app.models import SQLModel # noqa
|
|
from app.core.config import settings # noqa
|
|
|
|
target_metadata = SQLModel.metadata
|
|
|
|
# 根据 env.py 的需要,可以从配置中获取其他值:
|
|
# my_important_option = config.get_main_option("my_important_option")
|
|
# ... 等等。
|
|
|
|
|
|
def get_url():
|
|
return str(settings.SQLALCHEMY_DATABASE_URI)
|
|
|
|
|
|
def run_migrations_offline():
|
|
"""在"离线"模式下运行迁移。
|
|
|
|
这使用 URL 而不是 Engine 来配置上下文,
|
|
尽管 Engine 在这里也是可以接受的。
|
|
通过跳过 Engine 的创建,我们甚至不需要 DBAPI 可用。
|
|
|
|
在这里调用 context.execute() 会将给定的字符串输出到脚本输出。
|
|
|
|
"""
|
|
url = get_url()
|
|
context.configure(
|
|
url=url, target_metadata=target_metadata, literal_binds=True, compare_type=True
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def run_migrations_online():
|
|
"""在"在线"模式下运行迁移。
|
|
|
|
在这种情况下,我们需要创建一个 Engine
|
|
并将连接与上下文关联。
|
|
|
|
"""
|
|
configuration = config.get_section(config.config_ini_section)
|
|
configuration["sqlalchemy.url"] = get_url()
|
|
connectable = engine_from_config(
|
|
configuration,
|
|
prefix="sqlalchemy.",
|
|
poolclass=pool.NullPool,
|
|
)
|
|
|
|
with connectable.connect() as connection:
|
|
context.configure(
|
|
connection=connection, target_metadata=target_metadata, compare_type=True
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
run_migrations_online()
|