-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathenv.py
More file actions
280 lines (236 loc) · 11.1 KB
/
Copy pathenv.py
File metadata and controls
280 lines (236 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""Alembic environment configuration for async migrations.
Supports per-database migrations via named Alembic sections (registry, control, admin).
The active section name determines which database URL and metadata target to use.
"""
from __future__ import annotations
import asyncio
import os
import re
from typing import Any
from alembic import context
from alembic.runtime.environment import NameFilterParentNames, NameFilterType
from sqlalchemy import MetaData, pool, text
from sqlalchemy.engine import URL, Connection
from sqlalchemy.ext.asyncio import create_async_engine
from jentic_one.migrations.targets import DB_TARGETS
from jentic_one.shared.config import DatabaseConfig, load_config
from jentic_one.shared.db.backends import get_backend
from jentic_one.shared.db.session import get_database_url
config = context.config
def _on_version_apply(
ctx: Any,
step: Any,
heads: Any,
run_args: Any,
**kwargs: Any,
) -> None:
"""Print a line per migration as it is applied.
Alembic invokes this once for every revision step it runs (the ``step``
argument is a :class:`~alembic.runtime.migration.MigrationInfo`). It fires
for both the local ``start-fixtures`` flow (alembic CLI) and the deploy
migration Job (``python -m jentic_one.migrations.run``), so each reports
exactly which revisions were applied. When the database is already current
the hook never fires, so the absence of lines means "nothing to do".
"""
db = config.config_ini_section
direction = "upgrade" if step.is_upgrade else "downgrade"
script = step.up_revision
revision = script.revision if script is not None else "base"
filename = os.path.basename(script.path) if script is not None else "?"
doc = (script.doc or "").strip().splitlines()[0] if script and script.doc else ""
suffix = f" — {doc}" if doc else ""
print(f" [{db}] {direction} {revision} ({filename}){suffix}", flush=True)
def _resolve_db_name() -> str:
"""Determine the target database from the Alembic config section name."""
section = config.config_ini_section
if section in DB_TARGETS:
return section
return "registry"
def get_url() -> URL | str:
"""Resolve database URL for the active migration target.
If the active Alembic section provides an explicit ``sqlalchemy.url``
(used by tests/CI to point at ephemeral databases), it takes precedence
over the application config file lookup.
"""
explicit = config.get_section_option(config.config_ini_section, "sqlalchemy.url")
if explicit:
return explicit
app_config = load_config()
db_name = _resolve_db_name()
db_config = getattr(app_config.databases, db_name)
return get_database_url(db_config)
def get_schema() -> str:
"""Resolve the schema name for the active migration target.
Honours an explicit ``schema_name`` in the active Alembic section
(used by tests) before falling back to the application config.
The returned name is interpolated into a quoted ``CREATE SCHEMA``
identifier below, so it is validated against a conservative identifier
pattern at this sink (SEC-2, defense-in-depth): ``DatabaseConfig`` already
enforces the same pattern, but the Alembic-ini override path bypasses
pydantic entirely.
"""
explicit = config.get_section_option(config.config_ini_section, "schema_name")
if explicit:
schema = explicit
else:
app_config = load_config()
db_name = _resolve_db_name()
db_config: DatabaseConfig = getattr(app_config.databases, db_name)
schema = db_config.schema_name
if not re.fullmatch(r"[A-Za-z_][A-Za-z0-9_]*", schema):
raise ValueError(
f"invalid schema_name {schema!r}: must match [A-Za-z_][A-Za-z0-9_]* "
"(it is embedded in a CREATE SCHEMA identifier)"
)
return schema
def get_dialect_name() -> str:
"""Resolve the SQLAlchemy dialect name for the active migration target.
Infers the dialect from an explicit ``sqlalchemy.url`` when present
(tests/CI), otherwise from the configured backend.
"""
explicit = config.get_section_option(config.config_ini_section, "sqlalchemy.url")
if explicit:
return "sqlite" if explicit.startswith("sqlite") else "postgres"
app_config = load_config()
db_name = _resolve_db_name()
db_config: DatabaseConfig = getattr(app_config.databases, db_name)
return get_backend(db_config).dialect_name
def is_postgres() -> bool:
"""Return True when the active migration target is PostgreSQL."""
return get_dialect_name() == "postgres"
def get_target_metadata() -> MetaData:
"""Return the metadata for the active migration target."""
return DB_TARGETS[_resolve_db_name()].metadata
def get_version_table() -> str:
"""Return the ``alembic_version`` table name for the active migration target.
The built-in targets share the default ``alembic_version`` (scoped per-schema
by ``version_table_schema``); a target may use a distinct name to avoid a
version-tracking collision when it shares a schema with another target.
"""
return DB_TARGETS[_resolve_db_name()].version_table
def _include_name(
name: str | None, type_: NameFilterType, parent_names: NameFilterParentNames
) -> bool:
"""Restrict reflection/autogenerate to the active migration target's schema.
All three logical databases share one PostgreSQL instance separated by
schema. Without this filter, autogenerate sees every schema's tables and
tries to DROP the ones not present in the active metadata. Limiting the
reflected schemas to the active one keeps each migration scoped to its own
database.
"""
if type_ == "schema":
return name in (None, get_schema())
return True
def _include_object(
obj: Any, name: str | None, type_: str, reflected: bool, compare_to: Any
) -> bool:
"""Default: include everything (each built-in target is single-schema).
Overridable seam: a downstream ``env.py`` can replace this to treat certain
schemas as strictly read-only — returning ``False`` for objects whose schema
it does not own — so autogenerate never emits DROP/ALTER against tables it can
legitimately see (for cross-schema FK validation) but does not own.
"""
return True
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode."""
url = get_url()
postgres = is_postgres()
context.configure(
url=url,
target_metadata=get_target_metadata(),
literal_binds=True,
dialect_opts={"paramstyle": "named"},
version_table=get_version_table(),
version_table_schema=get_schema() if postgres else None,
include_schemas=postgres,
include_name=_include_name if postgres else None,
include_object=_include_object,
render_as_batch=not postgres,
on_version_apply=_on_version_apply,
# Each migration owns its own transaction so that migrations using
# ``op.get_context().autocommit_block()`` (CREATE INDEX CONCURRENTLY,
# etc.) only commit their own work, never a sibling's.
transaction_per_migration=True,
)
with context.begin_transaction():
context.run_migrations()
def do_run_migrations(connection: Connection) -> None:
postgres = is_postgres()
context.configure(
connection=connection,
target_metadata=get_target_metadata(),
version_table=get_version_table(),
version_table_schema=get_schema() if postgres else None,
include_schemas=postgres,
include_name=_include_name if postgres else None,
include_object=_include_object,
render_as_batch=not postgres,
on_version_apply=_on_version_apply,
# Each migration owns its own transaction so that migrations using
# ``op.get_context().autocommit_block()`` (CREATE INDEX CONCURRENTLY,
# etc.) only commit their own work, never a sibling's.
transaction_per_migration=True,
)
# Read-only status probe (``migrations.run --check``): the caller stashed a
# dict to be filled with the revisions this database is actually stamped at.
#
# Safety here comes from the *caller* invoking ``alembic current`` rather
# than ``upgrade``: that runs this env under ``dont_mutate=True`` with a
# no-op migration function, so nothing can be applied. Returning early is a
# belt-and-braces guard that also skips opening a pointless transaction — it
# is not the thing that makes the probe safe.
probe = config.attributes.get("status_probe")
if probe is not None:
probe["current"] = sorted(context.get_context().get_current_heads())
return
with context.begin_transaction():
context.run_migrations()
async def run_migrations_online() -> None:
"""Run migrations in 'online' mode."""
url = get_url()
postgres = is_postgres()
if postgres:
schema = get_schema()
connectable = create_async_engine(
url,
poolclass=pool.NullPool,
connect_args={"server_settings": {"search_path": f"{schema},public"}},
)
else:
connectable = create_async_engine(url, poolclass=pool.NullPool)
async with connectable.connect() as connection:
# Alembic does not create schemas, and relying on out-of-band bootstrap
# (a docker-entrypoint-initdb.d script) proved fragile: postgres runs
# init scripts once, on an empty data dir, so a mid-init failure leaves
# a volume that silently never gets its schemas (#992). Creating the
# active target's schema idempotently here makes every migrate
# self-sufficient and lets a half-initialized volume heal on the next
# run.
#
# Gated on an existence probe rather than relying on IF NOT EXISTS:
# postgres checks the CREATE privilege before the IF NOT EXISTS
# short-circuit, so an unconditional statement breaks deployments whose
# migration user owns the (pre-provisioned) schema but not the
# database. Skipped for the read-only status probe (``migrations.run
# --check``), which must not mutate — an uninitialized database
# reports ``uninitialized`` without the schema existing.
if postgres and config.attributes.get("status_probe") is None:
schema = get_schema()
exists = await connection.scalar(
text("SELECT 1 FROM pg_namespace WHERE nspname = :schema"),
{"schema": schema},
)
if not exists:
await connection.execute(text(f'CREATE SCHEMA IF NOT EXISTS "{schema}"'))
# The probe SELECT autobegins a transaction on this connection;
# always end it (both branches). Left open, Alembic treats the
# connection as externally-transacted and migrations that use
# ``op.get_context().autocommit_block()`` die on
# ``assert self._transaction is not None``.
await connection.commit()
await connection.run_sync(do_run_migrations)
await connectable.dispose()
if context.is_offline_mode():
run_migrations_offline()
else:
asyncio.run(run_migrations_online())