Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /sustainability-software-lab_ca-biositing /alembic /env.py
| """ | |
| Alembic environment configuration for SQLModel-based migrations. | |
| This module configures Alembic to generate and apply database migrations | |
| based on SQLModel model definitions. | |
| """ | |
| import os | |
| from logging.config import fileConfig | |
| from sqlalchemy import engine_from_config, pool | |
| from sqlmodel import SQLModel | |
| from alembic import context | |
| from pathlib import Path | |
| from dotenv import load_dotenv | |
| # Path configuration | |
| HERE = Path(__file__).parent.resolve() | |
| PROJECT_ROOT = HERE.parent.resolve() | |
| # Load environment variables from .env | |
| load_dotenv(dotenv_path=PROJECT_ROOT / ".env") | |
| # Import all models to register them with SQLModel.metadata | |
| # No sys.path.insert — package is installed via pixi | |
| import ca_biositing.datamodels.models # noqa: F401 | |
| # Alembic Config object | |
| config = context.config | |
| # Configure database URL from environment variables. | |
| # Priority: DATABASE_URL env var → Settings (handles Cloud Run Unix socket via | |
| # INSTANCE_CONNECTION_NAME, as well as local dev defaults). | |
| DATABASE_URL = os.getenv("DATABASE_URL") | |
| if not DATABASE_URL: | |
| from ca_biositing.datamodels.config import settings | |
| DATABASE_URL = settings.database_url | |
| config.set_main_option("sqlalchemy.url", DATABASE_URL.replace("%", "%%")) | |
| # Setup Python logging | |
| if config.config_file_name is not None: | |
| fileConfig(config.config_file_name) | |
| # Set target metadata for autogenerate support | |
| target_metadata = SQLModel.metadata | |
| # Materialized view names (managed manually in migrations, not autogenerated) | |
| MAT_VIEW_NAMES = { | |
| "landiq_record_view", | |
| "landiq_tileset_view", | |
| "analysis_data_view", | |
| "usda_census_view", | |
| "usda_survey_view", | |
| "billion_ton_tileset_view", | |
| "analysis_average_view", | |
| } | |
| def include_object(obj, name, type_, reflected, compare_to): | |
| """ | |
| Filter objects for autogenerate to prevent unwanted schema changes. | |
| """ | |
| # Exclude PostGIS system tables | |
| if type_ == "table" and name in [ | |
| "spatial_ref_sys", | |
| "geometry_columns", | |
| "geography_columns", | |
| "raster_columns", | |
| "raster_overviews", | |
| ]: | |
| return False | |
| # Exclude materialized views (managed manually in migrations) | |
| if type_ == "table" and name in MAT_VIEW_NAMES: | |
| return False | |
| # Ignore unique constraints on primary key columns to avoid redundant diffs | |
| if type_ == "unique_constraint" and obj is not None: | |
| cols = [c.name for c in obj.columns] | |
| table = obj.table | |
| pk_cols = [c.name for c in table.primary_key.columns] | |
| if cols == pk_cols: | |
| return False | |
| return True | |
| def render_item(type_, obj, autogen_context): | |
| """ | |
| Add custom imports to the migration template for SQLModel types. | |
| """ | |
| if type_ == "type" and hasattr(obj, "__module__"): | |
| if obj.__module__.startswith("sqlmodel"): | |
| autogen_context.imports.add("import sqlmodel") | |
| return False | |
| def run_migrations_offline() -> None: | |
| """ | |
| Run migrations in 'offline' mode. | |
| Configures the context with a URL and without an active connection. | |
| """ | |
| url = config.get_main_option("sqlalchemy.url") | |
| context.configure( | |
| url=url, | |
| target_metadata=target_metadata, | |
| literal_binds=True, | |
| dialect_opts={"paramstyle": "named"}, | |
| render_item=render_item, | |
| include_object=include_object, | |
| ) | |
| with context.begin_transaction(): | |
| context.run_migrations() | |
| def run_migrations_online() -> None: | |
| """ | |
| Run migrations in 'online' mode. | |
| Creates an Engine and associates a connection with the context. | |
| """ | |
| config_section = config.get_section(config.config_ini_section, {}) | |
| # Restrict search_path to 'public' to avoid reflecting PostGIS system tables | |
| connect_args = {"options": "-c search_path=public"} | |
| connectable = engine_from_config( | |
| config_section, | |
| prefix="sqlalchemy.", | |
| poolclass=pool.NullPool, | |
| connect_args=connect_args, | |
| ) | |
| with connectable.connect() as connection: | |
| context.configure( | |
| connection=connection, | |
| target_metadata=target_metadata, | |
| render_item=render_item, | |
| include_object=include_object, | |
| ) | |
| with context.begin_transaction(): | |
| context.run_migrations() | |
| if context.is_offline_mode(): | |
| run_migrations_offline() | |
| else: | |
| run_migrations_online() | |
Xet Storage Details
- Size:
- 4.36 kB
- Xet hash:
- a0785a5e0ec17c30c67447d2e1a5664da6e7238b19578d63d7cfd910fd309ce5
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.