Phase 1-3 of decoupling plan: - Shared DB, models, infrastructure, browser, config, utils - Event infrastructure (domain_events outbox, bus, processor) - Structured logging - Generic container concept (container_type/container_id) - Alembic migrations for all schema changes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
from __future__ import annotations
|
|
import os, sys
|
|
from logging.config import fileConfig
|
|
from alembic import context
|
|
from sqlalchemy import engine_from_config, pool
|
|
|
|
config = context.config
|
|
|
|
if config.config_file_name is not None:
|
|
try:
|
|
fileConfig(config.config_file_name)
|
|
except Exception:
|
|
pass
|
|
|
|
# Add project root so all app model packages are importable
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")))
|
|
|
|
from shared.db.base import Base
|
|
|
|
# Import ALL models so Base.metadata sees every table
|
|
import shared.models # noqa: F401 User, KV, MagicLink, MenuItem, Ghost*
|
|
import blog.models # noqa: F401 Post, Author, Tag, Snippet, TagGroup
|
|
import market.models # noqa: F401 Product, CartItem, MarketPlace, etc.
|
|
import cart.models # noqa: F401 Order, OrderItem, PageConfig
|
|
import events.models # noqa: F401 Calendar, CalendarEntry, Ticket, etc.
|
|
|
|
target_metadata = Base.metadata
|
|
|
|
def _get_url() -> str:
|
|
url = os.getenv(
|
|
"ALEMBIC_DATABASE_URL",
|
|
os.getenv("DATABASE_URL", config.get_main_option("sqlalchemy.url") or "")
|
|
)
|
|
print(url)
|
|
return url
|
|
|
|
def run_migrations_offline() -> None:
|
|
url = _get_url()
|
|
context.configure(
|
|
url=url,
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
dialect_opts={"paramstyle": "named"},
|
|
compare_type=True,
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
def run_migrations_online() -> None:
|
|
url = _get_url()
|
|
if url:
|
|
config.set_main_option("sqlalchemy.url", url)
|
|
|
|
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, compare_type=True)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
run_migrations_online()
|