|
| 1 | +""" |
| 2 | +Alembic environment configuration for StillMe database migrations |
| 3 | +Supports SQLite (current) and PostgreSQL (future migration target) |
| 4 | +""" |
| 5 | + |
| 6 | +from logging.config import fileConfig |
| 7 | +from sqlalchemy import engine_from_config |
| 8 | +from sqlalchemy import pool |
| 9 | +from alembic import context |
| 10 | +import os |
| 11 | +import sys |
| 12 | + |
| 13 | +# Add project root to path |
| 14 | +sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) |
| 15 | + |
| 16 | +# Import base for autogenerate support |
| 17 | +# For now, we'll use declarative_base for future SQLAlchemy models |
| 18 | +from sqlalchemy.ext.declarative import declarative_base |
| 19 | + |
| 20 | +# this is the Alembic Config object, which provides |
| 21 | +# access to the values within the .ini file in use. |
| 22 | +config = context.config |
| 23 | + |
| 24 | +# Interpret the config file for Python logging. |
| 25 | +# This line sets up loggers basically. |
| 26 | +if config.config_file_name is not None: |
| 27 | + fileConfig(config.config_file_name) |
| 28 | + |
| 29 | +# add your model's MetaData object here |
| 30 | +# for 'autogenerate' support |
| 31 | +# from myapp import mymodel |
| 32 | +# target_metadata = mymodel.Base.metadata |
| 33 | +# For now, we'll create a base for future use |
| 34 | +Base = declarative_base() |
| 35 | +target_metadata = Base.metadata |
| 36 | + |
| 37 | +# other values from the config, defined by the needs of env.py, |
| 38 | +# can be acquired: |
| 39 | +# my_important_option = config.get_main_option("my_important_option") |
| 40 | +# ... etc. |
| 41 | + |
| 42 | + |
| 43 | +def get_url(): |
| 44 | + """Get database URL from environment or config""" |
| 45 | + # Check for DATABASE_URL environment variable (for PostgreSQL) |
| 46 | + database_url = os.getenv("DATABASE_URL") |
| 47 | + if database_url: |
| 48 | + return database_url |
| 49 | + |
| 50 | + # Check for SQLite path in environment |
| 51 | + sqlite_path = os.getenv("SQLITE_DB_PATH", "data/stillme_migration.db") |
| 52 | + return f"sqlite:///{sqlite_path}" |
| 53 | + |
| 54 | + |
| 55 | +def run_migrations_offline() -> None: |
| 56 | + """Run migrations in 'offline' mode. |
| 57 | +
|
| 58 | + This configures the context with just a URL |
| 59 | + and not an Engine, though an Engine is acceptable |
| 60 | + here as well. By skipping the Engine creation |
| 61 | + we don't even need a DBAPI to be available. |
| 62 | +
|
| 63 | + Calls to context.execute() here emit the given string to the |
| 64 | + script output. |
| 65 | +
|
| 66 | + """ |
| 67 | + url = get_url() |
| 68 | + context.configure( |
| 69 | + url=url, |
| 70 | + target_metadata=target_metadata, |
| 71 | + literal_binds=True, |
| 72 | + dialect_opts={"paramstyle": "named"}, |
| 73 | + ) |
| 74 | + |
| 75 | + with context.begin_transaction(): |
| 76 | + context.run_migrations() |
| 77 | + |
| 78 | + |
| 79 | +def run_migrations_online() -> None: |
| 80 | + """Run migrations in 'online' mode. |
| 81 | +
|
| 82 | + In this scenario we need to create an Engine |
| 83 | + and associate a connection with the context. |
| 84 | +
|
| 85 | + """ |
| 86 | + # Override sqlalchemy.url in config with our dynamic URL |
| 87 | + configuration = config.get_section(config.config_ini_section) |
| 88 | + configuration["sqlalchemy.url"] = get_url() |
| 89 | + |
| 90 | + connectable = engine_from_config( |
| 91 | + configuration, |
| 92 | + prefix="sqlalchemy.", |
| 93 | + poolclass=pool.NullPool, |
| 94 | + ) |
| 95 | + |
| 96 | + with connectable.connect() as connection: |
| 97 | + context.configure( |
| 98 | + connection=connection, target_metadata=target_metadata |
| 99 | + ) |
| 100 | + |
| 101 | + with context.begin_transaction(): |
| 102 | + context.run_migrations() |
| 103 | + |
| 104 | + |
| 105 | +if context.is_offline_mode(): |
| 106 | + run_migrations_offline() |
| 107 | +else: |
| 108 | + run_migrations_online() |
| 109 | + |
0 commit comments