05/02 Phase B

This commit is contained in:
Nguyen Ngo
2026-05-02 10:55:28 -04:00
parent aca2e47583
commit bad39522fa
8 changed files with 386 additions and 53 deletions
@@ -0,0 +1,75 @@
"""Add mobile_local_id to inspections and issues for mobile sync idempotency.
Phase B — iPad offline inspection submission.
Each inspection or issue submitted from the iPad app carries a UUID generated
on the device (mobile_local_id). The server checks this field before creating
a new record so that network retries never produce duplicate rows.
Revision ID: phase_b_mobile_local_id
Revises: phase9_user_full_name
"""
from alembic import op
import sqlalchemy as sa
revision = 'phase_b_mobile_local_id'
down_revision = 'phase9_user_full_name'
branch_labels = None
depends_on = None
def upgrade():
# ── inspections.mobile_local_id ───────────────────────────────────────
with op.batch_alter_table('inspections') as batch_op:
# Check if column already exists (safe re-run)
conn = op.get_bind()
columns = [row[1] for row in conn.execute(sa.text('PRAGMA table_info(inspections)')).fetchall()] \
if conn.dialect.name == 'sqlite' \
else [row['COLUMN_NAME'] for row in conn.execute(
sa.text(
"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS "
"WHERE TABLE_NAME = 'inspections' AND TABLE_SCHEMA = DATABASE()"
)
).mappings()]
if 'mobile_local_id' not in columns:
batch_op.add_column(
sa.Column('mobile_local_id', sa.String(64), nullable=True)
)
# Index for fast idempotency lookups
op.execute(sa.text(
"CREATE INDEX IF NOT EXISTS idx_inspections_mobile_local_id "
"ON inspections(mobile_local_id)"
))
# ── issues.mobile_local_id ────────────────────────────────────────────
with op.batch_alter_table('issues') as batch_op:
conn = op.get_bind()
columns = [row[1] for row in conn.execute(sa.text('PRAGMA table_info(issues)')).fetchall()] \
if conn.dialect.name == 'sqlite' \
else [row['COLUMN_NAME'] for row in conn.execute(
sa.text(
"SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS "
"WHERE TABLE_NAME = 'issues' AND TABLE_SCHEMA = DATABASE()"
)
).mappings()]
if 'mobile_local_id' not in columns:
batch_op.add_column(
sa.Column('mobile_local_id', sa.String(64), nullable=True)
)
op.execute(sa.text(
"CREATE INDEX IF NOT EXISTS idx_issues_mobile_local_id "
"ON issues(mobile_local_id)"
))
def downgrade():
with op.batch_alter_table('inspections') as batch_op:
batch_op.drop_column('mobile_local_id')
with op.batch_alter_table('issues') as batch_op:
batch_op.drop_column('mobile_local_id')