Jun 26 update to the latest codes

This commit is contained in:
2026-06-26 12:09:49 -04:00
parent 77678ed724
commit 5d95cdbbfc
15 changed files with 711 additions and 36 deletions
@@ -0,0 +1,35 @@
"""phase30 — device_registrations table for iOS device tracking
Each row records a device that has opened the JanitorialQC app.
Upserted on every app foreground so last_seen_at stays current.
"""
from alembic import op
revision = 'phase30_device_registry'
down_revision = 'phase29_broadcasts'
branch_labels = None
depends_on = None
def upgrade():
op.execute("""
CREATE TABLE IF NOT EXISTS device_registrations (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
device_id VARCHAR(64) NOT NULL,
user_id INT NOT NULL,
device_name VARCHAR(255) NOT NULL DEFAULT '',
app_version VARCHAR(32) NOT NULL DEFAULT '',
ios_version VARCHAR(32) NOT NULL DEFAULT '',
registered_at DATETIME NOT NULL,
last_seen_at DATETIME NOT NULL,
CONSTRAINT uq_device_id UNIQUE (device_id),
CONSTRAINT fk_device_reg_user
FOREIGN KEY (user_id) REFERENCES users(id)
ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
""")
def downgrade():
op.execute("DROP TABLE IF EXISTS device_registrations")
@@ -0,0 +1,42 @@
"""phase31 — add ios_version and last_seen_at to api_device_tokens
Extends the existing api_device_tokens table (phase7) so the admin
Devices page can show iOS version and time of last app launch.
Drops the unused device_registrations table created by phase30 if it exists.
"""
from alembic import op
revision = 'phase31_device_registry'
down_revision = 'phase30_device_registry'
branch_labels = None
depends_on = None
def upgrade():
# Add ios_version column if it doesn't exist
op.execute("""
ALTER TABLE api_device_tokens
ADD COLUMN IF NOT EXISTS ios_version VARCHAR(20) NULL
""")
# Add last_seen_at column if it doesn't exist
op.execute("""
ALTER TABLE api_device_tokens
ADD COLUMN IF NOT EXISTS last_seen_at DATETIME NULL
""")
# Backfill last_seen_at from registered_at for existing rows
op.execute("""
UPDATE api_device_tokens
SET last_seen_at = registered_at
WHERE last_seen_at IS NULL
""")
# Drop the incorrectly created device_registrations table from phase30 if present
op.execute("DROP TABLE IF EXISTS device_registrations")
def downgrade():
op.execute("ALTER TABLE api_device_tokens DROP COLUMN IF EXISTS ios_version")
op.execute("ALTER TABLE api_device_tokens DROP COLUMN IF EXISTS last_seen_at")
@@ -0,0 +1,66 @@
"""phase32 — add ios_version and last_seen_at to api_device_tokens
phase31 recorded as applied but ALTER statements never executed.
Uses INFORMATION_SCHEMA column-existence checks — safe on MySQL 5.7+.
Also drops the orphaned device_registrations table from phase30 if present.
"""
revision = 'phase32_device_token_columns'
down_revision = 'phase31_device_registry'
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
def _column_exists(conn, table, column):
result = conn.execute(sa.text(
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS "
"WHERE TABLE_SCHEMA = DATABASE() "
"AND TABLE_NAME = :t AND COLUMN_NAME = :c"
), {"t": table, "c": column})
return result.scalar() > 0
def _table_exists(conn, table):
result = conn.execute(sa.text(
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES "
"WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = :t"
), {"t": table})
return result.scalar() > 0
def upgrade():
bind = op.get_bind()
if not _column_exists(bind, 'api_device_tokens', 'ios_version'):
op.execute(sa.text(
"ALTER TABLE api_device_tokens ADD COLUMN ios_version VARCHAR(20) NULL"
))
if not _column_exists(bind, 'api_device_tokens', 'last_seen_at'):
op.execute(sa.text(
"ALTER TABLE api_device_tokens ADD COLUMN last_seen_at DATETIME NULL"
))
op.execute(sa.text(
"UPDATE api_device_tokens SET last_seen_at = registered_at WHERE last_seen_at IS NULL"
))
if _table_exists(bind, 'device_registrations'):
op.execute(sa.text("DROP TABLE device_registrations"))
def downgrade():
bind = op.get_bind()
if _column_exists(bind, 'api_device_tokens', 'ios_version'):
op.execute(sa.text(
"ALTER TABLE api_device_tokens DROP COLUMN ios_version"
))
if _column_exists(bind, 'api_device_tokens', 'last_seen_at'):
op.execute(sa.text(
"ALTER TABLE api_device_tokens DROP COLUMN last_seen_at"
))