feat(migrations): T11 setup Alembic and initial schema migration
- Initialize Alembic with alembic init alembic - Configure alembic.ini to use DATABASE_URL from environment - Configure alembic/env.py to import Base and models metadata - Generate initial migration: c92fc544a483_initial_schema - Migration creates all 4 tables: users, api_keys, api_tokens, usage_stats - Migration includes all indexes, constraints, and foreign keys - Test upgrade/downgrade cycle works correctly Alembic commands: - alembic upgrade head - alembic downgrade -1 - alembic revision --autogenerate -m 'message' Tests: 13 migration tests pass
This commit is contained in:
101
alembic/env.py
Normal file
101
alembic/env.py
Normal file
@@ -0,0 +1,101 @@
|
||||
"""Alembic environment configuration.
|
||||
|
||||
T11: Setup Alembic and initial schema migration
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
from logging.config import fileConfig
|
||||
|
||||
from sqlalchemy import engine_from_config, pool
|
||||
|
||||
from alembic import context
|
||||
|
||||
# Add src to path to import models
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
|
||||
|
||||
# Import models to register them with Base
|
||||
from openrouter_monitor.database import Base
|
||||
from openrouter_monitor.models import User, ApiKey, UsageStats, ApiToken
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
# Override sqlalchemy.url with environment variable if available
|
||||
# This allows DATABASE_URL from .env to be used
|
||||
database_url = os.getenv('DATABASE_URL')
|
||||
if database_url:
|
||||
config.set_main_option('sqlalchemy.url', database_url)
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# Set target_metadata to the Base.metadata from our models
|
||||
# This is required for 'autogenerate' support
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
|
||||
"""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
# For SQLite, we need to handle check_same_thread=False
|
||||
db_url = config.get_main_option("sqlalchemy.url")
|
||||
|
||||
if db_url and 'sqlite' in db_url:
|
||||
# SQLite specific configuration
|
||||
from sqlalchemy import create_engine
|
||||
connectable = create_engine(
|
||||
db_url,
|
||||
connect_args={"check_same_thread": False},
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
else:
|
||||
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
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
Reference in New Issue
Block a user