|
| 1 | +"""Alembic environment for PolicyEngine API v1 raw-SQL schema migrations.""" |
| 2 | + |
| 3 | +from logging.config import fileConfig |
| 4 | +import importlib.util |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | +import sys |
| 8 | + |
| 9 | +from sqlalchemy import engine_from_config, pool |
| 10 | + |
| 11 | +from alembic import context |
| 12 | + |
| 13 | +sys.path.insert(0, str(Path(__file__).parent.parent)) |
| 14 | + |
| 15 | +metadata_path = ( |
| 16 | + Path(__file__).parent.parent / "policyengine_api" / "data" / "alembic_metadata.py" |
| 17 | +) |
| 18 | +metadata_spec = importlib.util.spec_from_file_location( |
| 19 | + "policyengine_api_alembic_metadata", |
| 20 | + metadata_path, |
| 21 | +) |
| 22 | +if metadata_spec is None or metadata_spec.loader is None: |
| 23 | + raise RuntimeError(f"Could not load Alembic metadata helper from {metadata_path}") |
| 24 | +metadata_module = importlib.util.module_from_spec(metadata_spec) |
| 25 | +metadata_spec.loader.exec_module(metadata_module) |
| 26 | +build_metadata_from_sql = metadata_module.build_metadata_from_sql |
| 27 | + |
| 28 | + |
| 29 | +config = context.config |
| 30 | + |
| 31 | +database_url = os.environ.get("POLICYENGINE_ALEMBIC_DATABASE_URL") or os.environ.get( |
| 32 | + "DATABASE_URL" |
| 33 | +) |
| 34 | +if database_url: |
| 35 | + config.set_main_option("sqlalchemy.url", database_url) |
| 36 | + |
| 37 | +if config.config_file_name is not None: |
| 38 | + fileConfig(config.config_file_name) |
| 39 | + |
| 40 | +schema_sql_path = os.environ.get("POLICYENGINE_ALEMBIC_SCHEMA_SQL") |
| 41 | +target_metadata = build_metadata_from_sql(schema_sql_path) |
| 42 | + |
| 43 | + |
| 44 | +def _configure_context(connection=None, url: str | None = None) -> None: |
| 45 | + options = { |
| 46 | + "target_metadata": target_metadata, |
| 47 | + "compare_type": False, |
| 48 | + "compare_server_default": False, |
| 49 | + } |
| 50 | + if connection is not None: |
| 51 | + context.configure(connection=connection, **options) |
| 52 | + else: |
| 53 | + context.configure( |
| 54 | + url=url, |
| 55 | + literal_binds=True, |
| 56 | + dialect_opts={"paramstyle": "named"}, |
| 57 | + **options, |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def run_migrations_offline() -> None: |
| 62 | + _configure_context(url=config.get_main_option("sqlalchemy.url")) |
| 63 | + with context.begin_transaction(): |
| 64 | + context.run_migrations() |
| 65 | + |
| 66 | + |
| 67 | +def run_migrations_online() -> None: |
| 68 | + connectable = engine_from_config( |
| 69 | + config.get_section(config.config_ini_section, {}), |
| 70 | + prefix="sqlalchemy.", |
| 71 | + poolclass=pool.NullPool, |
| 72 | + ) |
| 73 | + |
| 74 | + with connectable.connect() as connection: |
| 75 | + _configure_context(connection=connection) |
| 76 | + with context.begin_transaction(): |
| 77 | + context.run_migrations() |
| 78 | + |
| 79 | + |
| 80 | +if context.is_offline_mode(): |
| 81 | + run_migrations_offline() |
| 82 | +else: |
| 83 | + run_migrations_online() |
0 commit comments