From b8b89f39f19429cc335e073f8544067fee0788c1 Mon Sep 17 00:00:00 2001 From: Nguyen Ngo Date: Wed, 24 Jun 2026 17:25:02 -0400 Subject: [PATCH] Jun 24 - Implement devices tracker - Fix phase30 migration file --- .../versions/phase30_device_registry.py | 52 ++++++++----------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/migrations/versions/phase30_device_registry.py b/migrations/versions/phase30_device_registry.py index 0deab06..5e2bf22 100644 --- a/migrations/versions/phase30_device_registry.py +++ b/migrations/versions/phase30_device_registry.py @@ -1,41 +1,35 @@ """phase30 — device_registrations table for iOS device tracking -Revision ID: phase30_device_registry -Down revision: phase29_broadcasts +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' - -from alembic import op -import sqlalchemy as sa +branch_labels = None +depends_on = None def upgrade(): - # Guard: skip if table already exists (safe to re-run) - conn = op.get_bind() - exists = conn.execute(sa.text( - "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES " - "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'device_registrations'" - )).scalar() - if exists: - return - - op.create_table( - 'device_registrations', - sa.Column('id', sa.Integer(), primary_key=True), - sa.Column('device_id', sa.String(64), nullable=False), - sa.Column('user_id', sa.Integer(), sa.ForeignKey('users.id'), nullable=False), - sa.Column('device_name', sa.String(255), nullable=False, server_default=''), - sa.Column('app_version', sa.String(32), nullable=False, server_default=''), - sa.Column('ios_version', sa.String(32), nullable=False, server_default=''), - sa.Column('registered_at', sa.DateTime(), nullable=False), - sa.Column('last_seen_at', sa.DateTime(), nullable=False), - ) - - op.create_index('uq_device_id', 'device_registrations', ['device_id'], unique=True) - op.create_index('ix_device_registrations_user_id', 'device_registrations', ['user_id']) + 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.drop_table('device_registrations') + op.execute("DROP TABLE IF EXISTS device_registrations") \ No newline at end of file