9
9
from typing import Sequence , Union
10
10
11
11
import sqlalchemy as sa
12
- from sqlalchemy .dialects import mysql
13
12
14
13
from alembic import op
15
14
21
20
22
21
23
22
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
34
24
op .drop_column ("devices" , "security_level" )
35
25
op .drop_column ("devices" , "session_id_type" )
36
26
37
27
# rename code to info
38
28
op .alter_column ("devices" , "code" , new_column_name = "info" , existing_type = sa .String (255 ))
39
- # add new code column with a more unique value
40
29
op .add_column (
41
30
"devices" ,
42
31
sa .Column (
@@ -60,8 +49,6 @@ def upgrade() -> None:
60
49
61
50
# remove all rows where uploaded_by is null, as they are not associated with a user
62
51
op .execute ("DELETE FROM devices WHERE uploaded_by IS NULL" )
63
-
64
- # set uploaded_by to non nullable
65
52
op .alter_column ("devices" , "uploaded_by" , nullable = False , existing_type = sa .String (255 ))
66
53
67
54
# 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:
80
67
)
81
68
82
69
# 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" ])
84
72
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
+ )
88
81
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 ]
90
85
if user_device_insert :
91
86
user_device_insert_sql = "INSERT INTO user_device (user_id, device_code) VALUES " + "," .join (user_device_insert )
92
-
93
- # insert data into user_device
94
87
op .execute (sa .text (user_device_insert_sql ))
95
88
else :
96
89
print ("No devices found to insert into user_device table." )
97
90
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
-
107
91
108
92
def downgrade () -> None :
109
93
raise NotImplementedError ("Downgrade is not supported for this migration." )
0 commit comments