DB review fixes: drop redundant indexes + dead column, add CI MySQL-upgrade job
Some checks failed
CI / backend (push) Successful in 1m39s
CI / naming (push) Successful in 1s
CI / frontend (push) Successful in 8s
CI / migrations-mysql (push) Failing after 8s

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>
This commit is contained in:
cproudlock
2026-07-13 09:29:45 -04:00
parent 9c8b2c9c9e
commit 1c6c7ba14b
12 changed files with 217 additions and 15 deletions

View File

@@ -58,7 +58,6 @@ class Computer(BaseModel):
# Network identity
hostname = db.Column(
db.String(100),
index=True,
comment='Network hostname'
)

View File

@@ -58,7 +58,6 @@ class NetworkDevice(BaseModel):
# Network identity
hostname = db.Column(
db.String(100),
index=True,
comment='Network hostname'
)

View File

@@ -0,0 +1,42 @@
"""Index notifications.businessunitid (shopfloor list route filters on it).
businessunitid is a soft-ref column the shopfloor feed filters by; it had no
index. Add ix_notifications_businessunitid (matches the model's index=True).
Idempotent; downgrade drops it.
Revision ID: notifications0002buidx
Revises: notifications0001anchor
"""
from alembic import op
import sqlalchemy as sa
revision = 'notifications0002buidx'
down_revision = 'notifications0001anchor'
branch_labels = None
depends_on = None
_INDEX = 'ix_notifications_businessunitid'
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)
if 'notifications' not in insp.get_table_names():
return
if _INDEX not in _index_names(insp, 'notifications'):
op.create_index(_INDEX, 'notifications', ['businessunitid'])
def downgrade():
bind = op.get_bind()
insp = sa.inspect(bind)
if 'notifications' not in insp.get_table_names():
return
if _INDEX in _index_names(insp, 'notifications'):
op.drop_index(_INDEX, table_name='notifications')

View File

@@ -69,7 +69,7 @@ class Notification(db.Model):
db.ForeignKey('notificationtypes.notificationtypeid'),
nullable=True
)
businessunitid = db.Column(db.Integer, nullable=True)
businessunitid = db.Column(db.Integer, nullable=True, index=True)
appid = db.Column(db.Integer, nullable=True)
notification = db.Column(db.Text, nullable=False, comment='The message content')
starttime = db.Column(db.DateTime, nullable=True)

View File

@@ -63,7 +63,6 @@ class Printer(BaseModel):
# Network identity
hostname = db.Column(
db.String(100),
index=True,
comment='Network hostname'
)

View File

@@ -198,7 +198,7 @@ def checkout_device(device_id, data):
override = parse_import_datetime(data.get('checkouttime'))
if override is not None:
eventtime = override
db.session.add(USBCheckout(usbdeviceid=device.usbdeviceid, machineid=0, sso=badge,
db.session.add(USBCheckout(usbdeviceid=device.usbdeviceid, sso=badge,
checkoutname=name, checkouttime=eventtime,
checkoutreason=data.get('reason')))
device.ischeckedout = True

View File

@@ -0,0 +1,42 @@
"""Drop the dead usbcheckouts.machineid column (ADR-001: Machine retired).
machineid was a NOT NULL soft-reference to the retired machines table; every
new checkout stored the sentinel 0. It has no FK, no meaning under the asset
model, and no reader. Drop it.
Idempotent (skips a missing table/column). Downgrade restores it NOT NULL with
server_default 0 so any existing rows stay valid.
Revision ID: usb0002dropmachineid
Revises: usb0001anchor
"""
from alembic import op
import sqlalchemy as sa
revision = 'usb0002dropmachineid'
down_revision = 'usb0001anchor'
branch_labels = None
depends_on = None
def upgrade():
bind = op.get_bind()
insp = sa.inspect(bind)
if 'usbcheckouts' not in insp.get_table_names():
return
cols = {c['name'] for c in insp.get_columns('usbcheckouts')}
if 'machineid' in cols:
op.drop_column('usbcheckouts', 'machineid')
def downgrade():
bind = op.get_bind()
insp = sa.inspect(bind)
if 'usbcheckouts' not in insp.get_table_names():
return
cols = {c['name'] for c in insp.get_columns('usbcheckouts')}
if 'machineid' not in cols:
op.add_column('usbcheckouts',
sa.Column('machineid', sa.Integer(), nullable=False,
server_default='0'))

View File

@@ -75,7 +75,6 @@ class USBDevice(BaseModel, AuditMixin):
# Indexes
__table_args__ = (
db.Index('idx_usb_serial', 'serialnumber'),
db.Index('idx_usb_checkedout', 'ischeckedout'),
db.Index('idx_usb_type', 'usbdevicetypeid'),
db.Index('idx_usb_currentuser', 'currentuserid'),
@@ -122,9 +121,6 @@ class USBCheckout(BaseModel):
nullable=True
)
# Legacy reference to machines table (kept for backward compatibility)
machineid = db.Column(db.Integer, nullable=False)
# User info
sso = db.Column(db.String(20), nullable=False, comment='SSO of user')
checkoutname = db.Column(db.String(100), nullable=True, comment='Name of user')