Skip to content

Commit 7f5470d

Browse files
committed
fix migrations
1 parent d60f19e commit 7f5470d

File tree

2 files changed

+32
-43
lines changed

2 files changed

+32
-43
lines changed

alembic/versions/684292138a0a_many_devices_to_many_users.py

+14-30
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from typing import Sequence, Union
1010

1111
import sqlalchemy as sa
12-
from sqlalchemy.dialects import mysql
1312

1413
from alembic import op
1514

@@ -21,22 +20,12 @@
2120

2221

2322
def upgrade() -> None:
24-
# create a user <-> device association table
25-
op.create_table(
26-
"user_device",
27-
sa.Column("user_id", sa.VARCHAR(255), nullable=False),
28-
sa.Column("device_code", sa.VARCHAR(255), nullable=False),
29-
sa.ForeignKeyConstraint(["device_code"], ["devices.code"], ondelete="CASCADE"),
30-
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
31-
)
32-
33-
# remove useless columns, these never served any purpose
23+
# Remove columns from devices table that are no longer needed
3424
op.drop_column("devices", "security_level")
3525
op.drop_column("devices", "session_id_type")
3626

3727
# rename code to info
3828
op.alter_column("devices", "code", new_column_name="info", existing_type=sa.String(255))
39-
# add new code column with a more unique value
4029
op.add_column(
4130
"devices",
4231
sa.Column(
@@ -60,8 +49,6 @@ def upgrade() -> None:
6049

6150
# remove all rows where uploaded_by is null, as they are not associated with a user
6251
op.execute("DELETE FROM devices WHERE uploaded_by IS NULL")
63-
64-
# set uploaded_by to non nullable
6552
op.alter_column("devices", "uploaded_by", nullable=False, existing_type=sa.String(255))
6653

6754
# generate code for rows as a sha256 in the format of "client_id_blob_filename sha256:device_private_key sha265:uploaded_by"
@@ -80,30 +67,27 @@ def upgrade() -> None:
8067
)
8168

8269
# for mapping, cause we need to dedupe and create a unique constraint
83-
old_devices = op.get_bind().execute(sa.text("SELECT * FROM devices")).fetchall()
70+
op.execute("DELETE FROM devices WHERE id NOT IN (SELECT MIN(id) FROM devices GROUP BY code)")
71+
op.create_unique_constraint(None, "devices", ["code"])
8472

85-
user_device_insert = []
86-
for device in old_devices:
87-
user_device_insert.append(f"('{device[6]}', '{device[7]}')")
73+
# create a user <-> device association table
74+
op.create_table(
75+
"user_device",
76+
sa.Column("user_id", sa.VARCHAR(255), nullable=False),
77+
sa.Column("device_code", sa.VARCHAR(255), nullable=False),
78+
sa.ForeignKeyConstraint(["device_code"], ["devices.code"], ondelete="CASCADE"),
79+
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
80+
)
8881

89-
# Join the values and create the full INSERT statement
82+
# Insert existing data into user_device table
83+
old_devices = op.get_bind().execute(sa.text("SELECT * FROM devices")).fetchall()
84+
user_device_insert = [f"('{device[4]}', '{device[5]}')" for device in old_devices]
9085
if user_device_insert:
9186
user_device_insert_sql = "INSERT INTO user_device (user_id, device_code) VALUES " + ",".join(user_device_insert)
92-
93-
# insert data into user_device
9487
op.execute(sa.text(user_device_insert_sql))
9588
else:
9689
print("No devices found to insert into user_device table.")
9790

98-
# deduplicate devices by code
99-
op.execute("DELETE FROM devices WHERE id NOT IN (SELECT MIN(id) FROM devices GROUP BY code)")
100-
101-
# add a unique constraint to the code column
102-
op.create_unique_constraint(None, "devices", ["code"])
103-
104-
# reset ids
105-
# op.execute("SET @id=0; UPDATE devices SET id=@id:=@id+1")
106-
10791

10892
def downgrade() -> None:
10993
raise NotImplementedError("Downgrade is not supported for this migration.")

alembic/versions/8fbc59f03986_convert_device_to_wvd.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,24 @@ def upgrade() -> None:
3737
op.get_bind().execute(sa.text("SELECT code,client_id_blob_filename,device_private_key FROM devices")).fetchall()
3838
)
3939
for code, client_id, private_key in devices:
40-
logger.info(f"Converting device {code} to WVD")
41-
wvd = Device(
42-
type_=DeviceTypes.ANDROID,
43-
security_level=3,
44-
flags=None,
45-
private_key=base64.b64decode(private_key),
46-
client_id=base64.b64decode(client_id),
47-
)
48-
49-
wvd_b64 = base64.b64encode(wvd.dumps()).decode()
50-
op.get_bind().execute(
51-
sa.text("UPDATE devices SET wvd = :wvd WHERE code = :code"), {"wvd": wvd_b64, "code": code}
52-
)
40+
try:
41+
logger.info(f"Converting device {code} to WVD")
42+
wvd = Device(
43+
type_=DeviceTypes.ANDROID,
44+
security_level=3,
45+
flags=None,
46+
private_key=base64.b64decode(private_key),
47+
client_id=base64.b64decode(client_id),
48+
)
49+
50+
wvd_b64 = base64.b64encode(wvd.dumps()).decode()
51+
op.get_bind().execute(
52+
sa.text("UPDATE devices SET wvd = :wvd WHERE code = :code"), {"wvd": wvd_b64, "code": code}
53+
)
54+
except Exception as e:
55+
logger.error(f"Failed to convert device {code} to WVD: {e}\nPK: {private_key}\nCID: {client_id}")
56+
# remove the device from the database
57+
op.get_bind().execute(sa.text("DELETE FROM devices WHERE code = :code"), {"code": code})
5358

5459
# make the wvd column non-nullable
5560
op.alter_column("devices", "wvd", existing_type=sa.Text(), nullable=False)

0 commit comments

Comments
 (0)