Jun 24 - Implement devices tracker - Phase32 migration script fix
This commit is contained in:
@@ -1,40 +1,66 @@
|
|||||||
"""phase32 — add ios_version and last_seen_at to api_device_tokens
|
"""phase32 — add ios_version and last_seen_at to api_device_tokens
|
||||||
|
|
||||||
phase31 recorded as applied but the ALTER statements never ran because
|
phase31 recorded as applied but ALTER statements never executed.
|
||||||
the migration used op.get_bind() pattern that failed silently.
|
Uses INFORMATION_SCHEMA column-existence checks — safe on MySQL 5.7+.
|
||||||
This migration adds the two columns using raw SQL with IF NOT EXISTS
|
|
||||||
(same pattern as all working migrations in this project).
|
|
||||||
Also drops the orphaned device_registrations table from phase30 if present.
|
Also drops the orphaned device_registrations table from phase30 if present.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from alembic import op
|
|
||||||
|
|
||||||
revision = 'phase32_device_token_columns'
|
revision = 'phase32_device_token_columns'
|
||||||
down_revision = 'phase31_device_registry'
|
down_revision = 'phase31_device_registry'
|
||||||
branch_labels = None
|
branch_labels = None
|
||||||
depends_on = 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():
|
def upgrade():
|
||||||
op.execute("""
|
bind = op.get_bind()
|
||||||
ALTER TABLE api_device_tokens
|
|
||||||
ADD COLUMN IF NOT EXISTS ios_version VARCHAR(20) NULL
|
|
||||||
""")
|
|
||||||
|
|
||||||
op.execute("""
|
if not _column_exists(bind, 'api_device_tokens', 'ios_version'):
|
||||||
ALTER TABLE api_device_tokens
|
op.execute(sa.text(
|
||||||
ADD COLUMN IF NOT EXISTS last_seen_at DATETIME NULL
|
"ALTER TABLE api_device_tokens ADD COLUMN ios_version VARCHAR(20) NULL"
|
||||||
""")
|
))
|
||||||
|
|
||||||
op.execute("""
|
if not _column_exists(bind, 'api_device_tokens', 'last_seen_at'):
|
||||||
UPDATE api_device_tokens
|
op.execute(sa.text(
|
||||||
SET last_seen_at = registered_at
|
"ALTER TABLE api_device_tokens ADD COLUMN last_seen_at DATETIME NULL"
|
||||||
WHERE last_seen_at IS NULL
|
))
|
||||||
""")
|
|
||||||
|
|
||||||
op.execute("DROP TABLE IF EXISTS device_registrations")
|
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():
|
def downgrade():
|
||||||
op.execute("ALTER TABLE api_device_tokens DROP COLUMN IF EXISTS ios_version")
|
bind = op.get_bind()
|
||||||
op.execute("ALTER TABLE api_device_tokens DROP COLUMN IF EXISTS last_seen_at")
|
|
||||||
|
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"
|
||||||
|
))
|
||||||
Reference in New Issue
Block a user