ADR-014 schema-lean investigation + idempotent create_plugin_tables

This commit is contained in:
2026-07-19 00:36:54 -04:00
parent 99025d10f0
commit 18d59f82c9

View File

@@ -85,21 +85,24 @@ def _get_plugin_metadata(plugin_name: str) -> MetaData:
def create_plugin_tables(plugin_name: str): def create_plugin_tables(plugin_name: str):
"""Emit CreateTable DDL for every table this plugin owns. Idempotent """Create every table this plugin owns, sourced from the SQLAlchemy models
via Alembic's batch_op.create_table behavior (raises if exists; the (not duplicated DDL). IDEMPOTENT: a table that already exists is skipped, so
baseline migration is meant to run against an empty schema). this is safe on an existing database that has the table from the pre-cutover
core baseline as well as on a fresh install (ADR-014 Phase 2).
Called from each plugin's 0001_baseline.py upgrade() so the table Called from each plugin's 0001 baseline.py upgrade().
definitions stay sourced from the SQLAlchemy models rather than being
duplicated in handwritten Alembic ops.
""" """
from alembic import op from alembic import op
from sqlalchemy import inspect
from sqlalchemy.schema import CreateTable from sqlalchemy.schema import CreateTable
md = _get_plugin_metadata(plugin_name) md = _get_plugin_metadata(plugin_name)
bind = op.get_bind() bind = op.get_bind()
existing = set(inspect(bind).get_table_names())
# Sort by FK dependency so parent tables are created first. # Sort by FK dependency so parent tables are created first.
for table in md.sorted_tables: for table in md.sorted_tables:
if table.name in existing:
continue
op.execute(str(CreateTable(table).compile(dialect=bind.dialect))) op.execute(str(CreateTable(table).compile(dialect=bind.dialect)))