Let a baseline meet a table that is already there
Some checks failed
CI / backend (push) Failing after 11s
CI / naming (push) Successful in 2s
CI / frontend (push) Successful in 11s
CI / migrations-mysql (push) Failing after 7s

Reported: `flask plugin upgrade-all` failed the tools plugin with "table
securesendsecrets already exists".

Every post-cutover baseline emitted a plain op.create_table. That is correct
only while a table's existence and its plugin's Alembic stamp can never
disagree, and they can. Anything that builds tables from the models -
db.create_all(), which `flask db-utils create-all` calls - creates them and
stamps nothing. A run that creates a table and then fails before its stamp
commits leaves the same split, and re-running is the obvious thing to try. In
both cases upgrade-all meets existing tables with an unstamped chain and the
create is fatal.

Nothing caught it because the migration test DROPS the post-cutover tables
db.create_all() built before running upgrade-all. That arranges the one state
where an unguarded create works, and never the state the report came from. The
new test is the same sequence with the drop removed for the plugin under test.

create_table_if_missing and create_index_if_missing are in alembic_template.py
next to create_plugin_tables, which had the idempotency rule right and turns out
to be called by nobody - its docstring claimed every 0001 used it. Fixed the
docstring rather than rewiring the baselines onto it, because the reason they
hand-write their DDL still stands: the helper's per-plugin MetaData cannot
resolve a foreign key to a core table at CreateTable-compile time.

INDEXES ARE MATCHED ON COLUMNS, NOT NAME, and that distinction matters. The two
creators disagree about names - db.create_all() takes SQLAlchemy's default
convention (ix_securesendsecrets_token) and a baseline spells it the way this
repo's naming rules require (ixsecuresendsecretstoken). A name-keyed guard would
have called the index missing and added a second one over the same column, which
MySQL permits with a warning, so every affected site would have quietly grown
duplicates. The test asserts no table ends up with two indexes over the same
columns.

Applied to all nine table-creating plugin migrations, not just the tools
baseline that was reported: the four later migrations that create tables have
the same exposure, and geenforce, measuringtools, printedparts and backups are
the same defect waiting for a different site.

WHAT THIS DOES NOT FIX. It makes a baseline idempotent, not a whole chain. A
longer chain can still meet an ALTER whose column is already present -
printedparts adding gagelabtag to a table create_all built at head is the case
that showed up while testing. Guarding every op one at a time is the wrong
answer to that; a database built from the models is already AT head and its
chains want stamping rather than replaying, and there is no `flask plugin stamp`
today. Left alone deliberately rather than half-built, and the new test is
scoped to single-baseline plugins so it does not pretend otherwise.
This commit is contained in:
cproudlock
2026-08-24 09:28:37 -04:00
parent de3c5aa562
commit b96637e1a1
12 changed files with 285 additions and 74 deletions

View File

@@ -104,7 +104,12 @@ def create_plugin_tables(plugin_name: str):
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().
NOT actually called from anywhere: every post-cutover baseline emits
explicit Alembic ops instead, because this helper's per-plugin MetaData
cannot resolve a foreign key to a CORE table at CreateTable-compile time.
Kept because it is still the reference for what idempotent means here, and
because create_table_if_missing below is the same rule in a form the
baselines can use. See that function for what went wrong without it.
"""
from alembic import op
from sqlalchemy import inspect
@@ -120,6 +125,67 @@ def create_plugin_tables(plugin_name: str):
op.execute(str(CreateTable(table).compile(dialect=bind.dialect)))
def create_table_if_missing(table_name: str, *columns, **kwargs) -> bool:
"""op.create_table, skipped when the table is already there.
WHY EVERY POST-CUTOVER BASELINE NEEDS THIS. A baseline is the sole creator
of its plugin's tables, so it was written as a plain op.create_table - which
is correct only if the table's existence and the plugin's Alembic stamp can
never disagree. They can, and routinely do:
* `flask db-utils create-all` runs db.create_all(), which builds EVERY
table registered on the metadata - plugin tables included - and stamps
NOTHING. `flask plugin upgrade-all` then runs the baseline against
tables that already exist.
* A run that creates the table and then fails before its stamp commits
leaves the same split, and re-running is the obvious thing to try.
Reported as `flask plugin upgrade-all` failing the tools plugin with
"table securesendsecrets already exists". Nothing caught it because the
migration test drops the post-cutover tables db.create_all() made before
running upgrade-all, which arranges the one state where an unguarded create
works and never the state a real database is in.
Returns True when it created the table, so a caller can skip the indexes
that belong to it.
"""
from alembic import op
from sqlalchemy import inspect
if inspect(op.get_bind()).has_table(table_name):
logger.info('%s already exists, skipping create', table_name)
return False
op.create_table(table_name, *columns, **kwargs)
return True
def create_index_if_missing(index_name: str, table_name: str, columns, **kwargs):
"""op.create_index, skipped when an equivalent index already exists.
Matched on the COLUMN LIST rather than the name, because the two creators
do not agree on names. db.create_all() takes them from SQLAlchemy's default
convention (`ix_securesendsecrets_token`) while a baseline spells them the
way this repo's naming rules require (`ixsecuresendsecretstoken`). Matching
on the name would call the index missing and add a second one over the same
column - MySQL permits that, with only a warning, so the duplicate would
accumulate silently on every affected site.
"""
from alembic import op
from sqlalchemy import inspect
wanted = [str(column) for column in columns]
inspector = inspect(op.get_bind())
if not inspector.has_table(table_name):
op.create_index(index_name, table_name, columns, **kwargs)
return
for existing in inspector.get_indexes(table_name):
if list(existing.get('column_names') or []) == wanted:
logger.info('index over %s(%s) already exists as %s, skipping',
table_name, ', '.join(wanted), existing.get('name'))
return
op.create_index(index_name, table_name, columns, **kwargs)
def drop_plugin_tables(plugin_name: str):
"""Mirror of create_plugin_tables for downgrade(). Drops in reverse FK
order."""