Files
shopdb-flask/plugins/measuringtools/migrations/versions/0001_measuringtools_baseline.py
cproudlock b96637e1a1
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
Let a baseline meet a table that is already there
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.
2026-08-24 09:28:37 -04:00

75 lines
3.3 KiB
Python

"""measuringtools plugin baseline (real create).
This plugin was built AFTER the ADR-008 ownership cutover, so unlike the ten
bundled plugins whose 0001 is a stamp-only no-op anchor, this baseline actually
CREATES the plugin's tables. The core Alembic chain never knew about
measuringtooltypes / measuringtools, so this per-plugin chain is their sole
authoritative creator. It runs from `flask plugin install measuringtools`
(and `flask plugin upgrade-all`) after `flask db upgrade` builds the core schema.
Note on create_plugin_tables: the shared helper in
shopdb.plugins.alembic_template builds a per-plugin MetaData filtered to only the
plugin's own tables, which means a foreign key to a core table (assets) cannot
be resolved at CreateTable-compile time (NoReferencedTableError). Since these
tables reference assets.assetid, the baseline emits explicit Alembic ops (the
same shape Alembic autogenerate produces) instead. Tables inherit the
connection's default charset, so on a utf8mb4 database they are utf8mb4, matching
how the core chain creates its tables.
"""
from alembic import op
from shopdb.plugins.alembic_template import (
create_index_if_missing,
create_table_if_missing,
)
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'measuringtools0001baseline'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
create_table_if_missing(
'measuringtooltypes',
sa.Column('measuringtooltypeid', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('color', sa.String(length=20), nullable=True),
sa.Column('createddate', sa.DateTime(), nullable=False),
sa.Column('modifieddate', sa.DateTime(), nullable=False),
sa.Column('isactive', sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint('measuringtooltypeid'),
sa.UniqueConstraint('name'),
)
create_table_if_missing(
'measuringtools',
sa.Column('measuringtoolid', sa.Integer(), nullable=False),
sa.Column('assetid', sa.Integer(), nullable=False),
sa.Column('measuringtooltypeid', sa.Integer(), nullable=True),
sa.Column('calibrationintervaldays', sa.Integer(), nullable=True),
sa.Column('lastcalibrationdate', sa.Date(), nullable=True),
sa.Column('nextcalibrationdate', sa.Date(), nullable=True),
sa.Column('calibrationprovider', sa.String(length=150), nullable=True),
sa.Column('notes', sa.Text(), nullable=True),
sa.Column('createddate', sa.DateTime(), nullable=False),
sa.Column('modifieddate', sa.DateTime(), nullable=False),
sa.Column('isactive', sa.Boolean(), nullable=False),
sa.ForeignKeyConstraint(['assetid'], ['assets.assetid'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['measuringtooltypeid'],
['measuringtooltypes.measuringtooltypeid']),
sa.PrimaryKeyConstraint('measuringtoolid'),
sa.UniqueConstraint('assetid'),
)
create_index_if_missing('idx_measuringtool_type', 'measuringtools',
['measuringtooltypeid'])
def downgrade():
op.drop_index('idx_measuringtool_type', table_name='measuringtools')
op.drop_table('measuringtools')
op.drop_table('measuringtooltypes')