Adjusted environment parsing in config.py
Some checks failed
CI / Lint & Format (push) Has been cancelled
CI / Tests (Pytest + Alembic + Postgres) (push) Has been cancelled
CI / Docker Build (push) Has been cancelled

This commit is contained in:
2025-09-22 20:15:46 +01:00
parent 84e03b2ae9
commit 8c53c2ffaa
3 changed files with 35 additions and 36 deletions

View File

@@ -1,62 +1,57 @@
import sys, pathlib
import os
import sys
import pathlib
import asyncio
from logging.config import fileConfig
# Add project root (parent of the "alembic" dir) to sys.path
from alembic import context
from sqlalchemy import pool
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
# Ensure project root is importable
PROJECT_ROOT = pathlib.Path(__file__).resolve().parents[1]
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
from logging.config import fileConfig
from alembic import context
# Import metadata (should NOT import settings)
from app.db.base import Base # Base.metadata must include all models
# Import your app's config & models
from app.core.config import settings
from app.db.base import Base # imports all models via app/models/__init__.py
# Alembic Config object
# Alembic config & logging
config = context.config
# Set up Python logging from alembic.ini
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# Target metadata (for autogenerate)
target_metadata = Base.metadata
# DSN from your app config (e.g. postgresql+asyncpg://…)
DB_URL = settings.POSTGRES_DSN
# Get DB URL from env (prefer KEYWARDEN_ prefix, fall back to unprefixed, then a sane default for local)
DB_URL = (
os.getenv("KEYWARDEN_POSTGRES_DSN")
or os.getenv("POSTGRES_DSN")
or "postgresql+asyncpg://postgres:postgres@localhost:5432/keywarden"
)
def run_migrations_offline():
"""Run migrations in 'offline' mode."""
url = DB_URL
context.configure(
url=url,
url=DB_URL,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def do_run_migrations(connection):
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
async def run_migrations_online():
"""Run migrations in 'online' mode with async engine."""
connectable: AsyncEngine = create_async_engine(
DB_URL,
poolclass=pool.NullPool,
)
connectable: AsyncEngine = create_async_engine(DB_URL, poolclass=pool.NullPool)
async with connectable.connect() as connection:
await connection.run_sync(do_run_migrations)
await connectable.dispose()