gabriel / musehub public
env.py python
108 lines 3.2 KB
Raw
sha256:3ff9c9863a9891bdcde71b4a43228f66d0493e38b7cc1d09fe9eb7de774046b2 feat: add repair-commit wire endpoint (API parity with repa… Opus 4.8 minor ⚠ breaking 1 day ago
1 """Alembic environment configuration for Muse."""
2 from __future__ import annotations
3
4 import asyncio
5 from logging.config import fileConfig
6
7 from sqlalchemy import pool
8 from sqlalchemy.engine import Connection
9 from sqlalchemy.ext.asyncio import async_engine_from_config
10
11 from alembic import context
12
13 # Import your models' Base
14 from musehub.db.database import Base
15 from musehub.db import muse_cli_models # noqa: F401 - Register Muse CLI commit tables
16 from musehub.config import settings
17
18 # Alembic Config object
19 config = context.config
20
21 # Interpret the config file for Python logging
22 if config.config_file_name is not None:
23 fileConfig(config.config_file_name)
24
25 # Add your model's MetaData object here for 'autogenerate' support
26 target_metadata = Base.metadata
27
28 # Override sqlalchemy.url with the one from settings (required; no default password in repo)
29 if not settings.database_url:
30 raise RuntimeError(
31 "DATABASE_URL (or DB_PASSWORD with Docker Postgres) must be set for migrations. "
32 "set in .env or export before running alembic."
33 )
34 config.set_main_option(
35 "sqlalchemy.url",
36 settings.database_url.replace("+asyncpg", ""),
37 )
38
39
40 def run_migrations_offline() -> None:
41 """Run migrations in 'offline' mode.
42
43 This configures the context with just a URL
44 and not an Engine, though an Engine is acceptable
45 here as well. By skipping the Engine creation
46 we don't even need a DBAPI to be available.
47
48 Calls to context.execute() here emit the given string to the
49 script output.
50 """
51 url = config.get_main_option("sqlalchemy.url")
52 context.configure(
53 url=url,
54 target_metadata=target_metadata,
55 literal_binds=True,
56 dialect_opts={"paramstyle": "named"},
57 compare_type=True,
58 compare_server_default=True,
59 )
60
61 with context.begin_transaction():
62 context.run_migrations()
63
64
65 def do_run_migrations(connection: Connection) -> None:
66 """Run migrations with a connection."""
67 context.configure(
68 connection=connection,
69 target_metadata=target_metadata,
70 compare_type=True,
71 compare_server_default=True,
72 )
73
74 with context.begin_transaction():
75 context.run_migrations()
76
77
78 async def run_async_migrations() -> None:
79 """Run migrations in 'online' mode with async support."""
80 configuration = config.get_section(config.config_ini_section, {})
81
82 # The ini option (set above) has +asyncpg stripped for the offline/sync
83 # autogenerate path. The async engine requires the full async URL, so we
84 # always override with settings.database_url here. Test isolation is
85 # handled by test fixtures that patch settings.database_url directly.
86 configuration["sqlalchemy.url"] = settings.database_url
87
88 connectable = async_engine_from_config(
89 configuration,
90 prefix="sqlalchemy.",
91 poolclass=pool.NullPool,
92 )
93
94 async with connectable.connect() as connection:
95 await connection.run_sync(do_run_migrations)
96
97 await connectable.dispose()
98
99
100 def run_migrations_online() -> None:
101 """Run migrations in 'online' mode."""
102 asyncio.run(run_async_migrations())
103
104
105 if context.is_offline_mode():
106 run_migrations_offline()
107 else:
108 run_migrations_online()
File History 1 commit
sha256:3ff9c9863a9891bdcde71b4a43228f66d0493e38b7cc1d09fe9eb7de774046b2 feat: add repair-commit wire endpoint (API parity with repa… Opus 4.8 minor 1 day ago