2026-08-05 17:57:32 +08:00
|
|
|
"""Alembic environment using the server's single Settings entry point."""
|
|
|
|
|
|
|
|
|
|
from logging.config import fileConfig
|
|
|
|
|
|
|
|
|
|
from alembic import context
|
|
|
|
|
from sqlalchemy import engine_from_config, pool
|
|
|
|
|
|
2026-08-05 22:45:57 +08:00
|
|
|
from zhixing_server.bootstrap.config import get_settings, sqlalchemy_database_url
|
2026-08-05 17:57:32 +08:00
|
|
|
from zhixing_server.modules.market_data.infrastructure.schema import metadata
|
|
|
|
|
|
|
|
|
|
config = context.config
|
|
|
|
|
settings = get_settings()
|
2026-08-05 22:45:57 +08:00
|
|
|
sqlalchemy_url = sqlalchemy_database_url(settings.database_url)
|
|
|
|
|
config.set_main_option("sqlalchemy.url", sqlalchemy_url.replace("%", "%%"))
|
2026-08-05 17:57:32 +08:00
|
|
|
if config.config_file_name is not None:
|
|
|
|
|
fileConfig(config.config_file_name)
|
|
|
|
|
|
|
|
|
|
target_metadata = metadata
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
|
|
|
"""Run migrations without opening a database connection."""
|
|
|
|
|
|
|
|
|
|
context.configure(
|
2026-08-05 22:45:57 +08:00
|
|
|
url=sqlalchemy_url,
|
2026-08-05 17:57:32 +08:00
|
|
|
target_metadata=target_metadata,
|
|
|
|
|
literal_binds=True,
|
|
|
|
|
dialect_opts={"paramstyle": "named"},
|
|
|
|
|
compare_type=True,
|
|
|
|
|
)
|
|
|
|
|
with context.begin_transaction():
|
|
|
|
|
context.run_migrations()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_migrations_online() -> None:
|
|
|
|
|
"""Run migrations against the configured PostgreSQL connection."""
|
|
|
|
|
|
|
|
|
|
connectable = engine_from_config(
|
|
|
|
|
config.get_section(config.config_ini_section, {}),
|
|
|
|
|
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()
|