From the database review (verdict: sound-with-minor-issues). Applies the actionable findings. Redundant indexes: five non-unique secondary indexes duplicated a named idx_* or a unique index on the same column - ix_communications_assetid, ix_computers_hostname, ix_networkdevices_hostname, ix_printers_hostname (each shadowing an idx_*), and idx_usb_serial (shadowing the serialnumber unique index). Removed the redundant index source from the models (column index=True / the extra db.Index) and added core migration 7d25 dropping the live duplicates. The unique ix_*_assetid indexes are kept (they enforce assetid uniqueness). Dead column: usbcheckouts.machineid was a NOT NULL soft-ref to the retired machines table storing sentinel 0 (ADR-001). Dropped from the model + the machineid=0 literal in selfhosted checkout; usb plugin migration 0002 drops it live (downgrade restores it default 0). Index: notifications.businessunitid (filtered by the shopfloor feed) was unindexed; added index=True + notifications migration 0002. CI: new migrations-mysql job proves the real multi-site deploy path - fresh `flask db upgrade` + per-plugin install on utf8mb4 MySQL from empty, asserting table count + charset and a clean second-run no-op. The pytest suite only exercises SQLite create_all(), so a regression in the Alembic chain on MySQL would otherwise ship undetected. Verified: fresh core upgrade on a scratch utf8mb4 MySQL builds clean + no-op on rerun (redundant indexes absent, unique assetid kept); plugin migrations applied + verified on the dev DB (machineid gone, bu index present). 953 backend tests pass; naming + pyflakes green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
58 lines
2.1 KiB
Python
58 lines
2.1 KiB
Python
"""Drop redundant secondary indexes duplicating a named idx_* / unique index.
|
|
|
|
The DB review found five non-unique secondary indexes that duplicate an existing
|
|
index on the same column: the auto-named ix_* from a column index=True alongside
|
|
a named idx_* in __table_args__ (communications.assetid, and the hostname on
|
|
computers/networkdevices/printers), plus idx_usb_serial duplicating the
|
|
serialnumber unique index on usbdevices. Each redundant pair costs insert/update
|
|
maintenance with zero read benefit. The models had the redundant index source
|
|
removed; this drops the live duplicates so the schema matches the models.
|
|
|
|
Idempotent (skips a missing table/index); real downgrade recreates them.
|
|
|
|
Revision ID: 7d25_drop_redundant_indexes
|
|
Revises: 7d24_customfield_searchable
|
|
Create Date: 2026-07-13
|
|
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
revision = '7d25_drop_redundant_indexes'
|
|
down_revision = '7d24_customfield_searchable'
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
# (table, index name, columns, unique) - columns/unique used only to recreate on
|
|
# downgrade. All non-unique; the surviving idx_* / unique index still covers each.
|
|
_REDUNDANT = [
|
|
('communications', 'ix_communications_assetid', ['assetid'], False),
|
|
('computers', 'ix_computers_hostname', ['hostname'], False),
|
|
('networkdevices', 'ix_networkdevices_hostname', ['hostname'], False),
|
|
('printers', 'ix_printers_hostname', ['hostname'], False),
|
|
('usbdevices', 'idx_usb_serial', ['serialnumber'], False),
|
|
]
|
|
|
|
|
|
def _index_names(insp, table):
|
|
return {i['name'] for i in insp.get_indexes(table)}
|
|
|
|
|
|
def upgrade():
|
|
bind = op.get_bind()
|
|
insp = sa.inspect(bind)
|
|
tables = set(insp.get_table_names())
|
|
for table, name, _cols, _unique in _REDUNDANT:
|
|
if table in tables and name in _index_names(insp, table):
|
|
op.drop_index(name, table_name=table)
|
|
|
|
|
|
def downgrade():
|
|
bind = op.get_bind()
|
|
insp = sa.inspect(bind)
|
|
tables = set(insp.get_table_names())
|
|
for table, name, cols, unique in _REDUNDANT:
|
|
if table in tables and name not in _index_names(insp, table):
|
|
op.create_index(name, table, cols, unique=unique)
|