velofy/migrations/env.py

51 lines
1.4 KiB
Python
Raw Normal View History

"""Configure Alembic's migration environment."""
import os
from logging import config as log_config
import sqlalchemy as sa
from alembic import context
2026-11-24 10:26:51 +01:00
from velofy import config as velofy_config
from velofy import db
# Disable the --sql option, a.k.a, the "offline mode".
if context.is_offline_mode():
2026-11-28 03:08:55 +01:00
raise NotImplementedError("The --sql option is not implemented in this project")
# Set up the default Python logger from the alembic.ini file.
log_config.fileConfig(context.config.config_file_name)
def include_object(obj, _name, type_, _reflected, _compare_to):
"""Only include the clean schema into --autogenerate migrations."""
2026-11-28 02:01:12 +01:00
if (
2026-11-28 03:08:55 +01:00
type_ in {"table", "column"}
and hasattr(obj, "schema") # => fix for rare edge case
2026-11-24 10:26:51 +01:00
and obj.schema != velofy_config.CLEAN_SCHEMA
):
return False
return True
2026-11-24 10:26:51 +01:00
engine = sa.create_engine(velofy_config.DATABASE_URI)
with engine.connect() as connection:
context.configure(
connection=connection,
include_object=include_object,
target_metadata=db.Base.metadata,
2026-11-28 03:08:55 +01:00
version_table="{alembic_table}{test_schema}".format(
2026-11-24 10:26:51 +01:00
alembic_table=velofy_config.ALEMBIC_TABLE,
test_schema=(
2026-11-28 03:08:55 +01:00
f"_{velofy_config.CLEAN_SCHEMA}" if os.getenv("TESTING") else ""
2026-11-24 10:26:51 +01:00
),
),
2026-11-24 10:26:51 +01:00
version_table_schema=velofy_config.ALEMBIC_TABLE_SCHEMA,
)
with context.begin_transaction():
context.run_migrations()