Jun 26 MT-2 phase

This commit is contained in:
2026-06-26 16:57:10 -04:00
parent a9d5e5ad3d
commit 07af8ee35a
4 changed files with 289 additions and 1 deletions
+62
View File
@@ -0,0 +1,62 @@
"""
migrations_tenant/env.py
------------------------
Standalone Alembic environment for running the EXISTING tenant migration chain
(migrations/versions) against an ARBITRARY tenant database.
Unlike migrations/env.py — which is Flask-Migrate-coupled and always targets
the app's default database via current_app — this reads sqlalchemy.url straight
from the Alembic config supplied per-tenant by control/tenant_migrate.py, and
reuses the same version scripts through version_locations. No Flask import, so
it can run for any tenant DB outside an app context.
The normal `flask db ...` workflow is unaffected; it still uses migrations/env.py.
"""
from logging.config import fileConfig
from sqlalchemy import create_engine, pool
from alembic import context
config = context.config
# Only configure logging if an ini file was supplied (the runner builds the
# Config programmatically, so this is typically skipped).
if config.config_file_name is not None:
try:
fileConfig(config.config_file_name)
except Exception:
pass
# Raw-SQL migrations with INFORMATION_SCHEMA guards — autogenerate is not used,
# so no target metadata is required here.
target_metadata = None
def run_migrations_offline():
context.configure(
url=config.get_main_option('sqlalchemy.url'),
target_metadata=target_metadata,
literal_binds=True,
compare_type=False,
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online():
url = config.get_main_option('sqlalchemy.url')
connectable = create_engine(url, poolclass=pool.NullPool, future=True)
try:
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
finally:
connectable.dispose()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
+22
View File
@@ -0,0 +1,22 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade():
${upgrades if upgrades else "pass"}
def downgrade():
${downgrades if downgrades else "pass"}