@@ -23,68 +23,118 @@ def upgrade() -> None:
23
23
bind = op .get_bind ()
24
24
inspector = sa .Inspector .from_engine (bind )
25
25
26
- has_apikeys = "apikeys" in inspector .get_table_names ()
27
- has_users = "users" in inspector .get_table_names ()
28
- has_cdms = "cdms" in inspector .get_table_names ()
29
- has_keys = "keys_" in inspector .get_table_names ()
26
+ migrate_existing_apikeys = "apikeys" in inspector .get_table_names ()
27
+ migrate_existing_users = "users" in inspector .get_table_names ()
28
+ migrate_existing_cdms = "cdms" in inspector .get_table_names ()
29
+ migrate_existing_keys = "keys_" in inspector .get_table_names ()
30
+
31
+ if migrate_existing_apikeys :
32
+ op .rename_table ("apikeys" , "apikeys_old" )
33
+
34
+ if migrate_existing_users :
35
+ op .rename_table ("users" , "users_old" )
36
+
37
+ if migrate_existing_cdms :
38
+ op .rename_table ("cdms" , "cdms_old" )
39
+
40
+ if migrate_existing_keys :
41
+ op .rename_table ("keys_" , "keys__old" )
30
42
31
43
# ### commands auto generated by Alembic - please adjust! ###
32
- if not has_apikeys :
33
- op .create_table (
34
- "apikeys" ,
35
- sa .Column ("id" , sa .Integer (), autoincrement = True , nullable = False ),
36
- sa .Column ("created_at" , sa .DateTime (), nullable = False ),
37
- sa .Column ("api_key" , sa .String (length = 255 ), nullable = False ),
38
- sa .Column ("user_id" , sa .String (length = 255 ), nullable = False ),
39
- sa .PrimaryKeyConstraint ("id" ),
40
- sa .UniqueConstraint ("id" ),
44
+ op .create_table (
45
+ "apikeys" ,
46
+ sa .Column ("id" , sa .Integer (), autoincrement = True , nullable = False ),
47
+ sa .Column ("created_at" , sa .DateTime (), nullable = False ),
48
+ sa .Column ("api_key" , sa .String (length = 255 ), nullable = False ),
49
+ sa .Column ("user_id" , sa .String (length = 255 ), nullable = False ),
50
+ sa .PrimaryKeyConstraint ("id" ),
51
+ sa .UniqueConstraint ("id" ),
52
+ )
53
+
54
+ op .create_table (
55
+ "users" ,
56
+ sa .Column ("id" , sa .String (length = 255 ), nullable = False ),
57
+ sa .Column ("username" , sa .String (length = 255 ), nullable = False ),
58
+ sa .Column ("discriminator" , sa .String (length = 255 ), nullable = False ),
59
+ sa .Column ("avatar" , sa .String (length = 255 ), nullable = True ),
60
+ sa .Column ("public_flags" , sa .Integer (), nullable = False ),
61
+ sa .Column ("api_key" , sa .String (length = 255 ), nullable = False ),
62
+ sa .Column ("flags" , sa .Integer (), nullable = False ),
63
+ sa .PrimaryKeyConstraint ("id" ),
64
+ sa .UniqueConstraint ("id" ),
65
+ )
66
+
67
+ op .create_table (
68
+ "cdms" ,
69
+ sa .Column ("id" , sa .Integer (), autoincrement = True , nullable = False ),
70
+ sa .Column ("session_id_type" , sa .String (length = 255 ), nullable = False ),
71
+ sa .Column ("security_level" , sa .Integer (), nullable = False ),
72
+ sa .Column ("client_id_blob_filename" , sa .Text (), nullable = False ),
73
+ sa .Column ("device_private_key" , sa .Text (), nullable = False ),
74
+ sa .Column ("code" , sa .Text (), nullable = False ),
75
+ sa .Column ("uploaded_by" , sa .String (length = 255 ), nullable = True ),
76
+ sa .ForeignKeyConstraint (
77
+ ["uploaded_by" ],
78
+ ["users.id" ],
79
+ ),
80
+ sa .PrimaryKeyConstraint ("id" ),
81
+ )
82
+
83
+ op .create_table (
84
+ "keys_" ,
85
+ sa .Column ("kid" , sa .String (length = 32 ), nullable = False ),
86
+ sa .Column ("added_at" , sa .Integer (), nullable = False ),
87
+ sa .Column ("added_by" , sa .String (length = 255 ), nullable = True ),
88
+ sa .Column ("license_url" , sa .Text (), nullable = False ),
89
+ sa .Column ("key_" , sa .String (length = 255 ), nullable = False ),
90
+ sa .ForeignKeyConstraint (
91
+ ["added_by" ],
92
+ ["users.id" ],
93
+ ),
94
+ sa .PrimaryKeyConstraint ("kid" ),
95
+ )
96
+
97
+ if (
98
+ not migrate_existing_apikeys
99
+ and not migrate_existing_users
100
+ and not migrate_existing_cdms
101
+ and not migrate_existing_keys
102
+ ):
103
+ return
104
+
105
+ # copy data from old tables to new tables
106
+ if migrate_existing_users :
107
+ op .execute (
108
+ "INSERT INTO users (id, username, discriminator, avatar, public_flags, api_key, flags) SELECT id, username, discriminator, avatar, public_flags, api_key, flags FROM users_old"
41
109
)
42
110
43
- if not has_users :
44
- op .create_table (
45
- "users" ,
46
- sa .Column ("id" , sa .String (length = 255 ), nullable = False ),
47
- sa .Column ("username" , sa .String (length = 255 ), nullable = False ),
48
- sa .Column ("discriminator" , sa .String (length = 255 ), nullable = False ),
49
- sa .Column ("avatar" , sa .String (length = 255 ), nullable = True ),
50
- sa .Column ("public_flags" , sa .Integer (), nullable = False ),
51
- sa .Column ("api_key" , sa .String (length = 255 ), nullable = False ),
52
- sa .Column ("flags" , sa .Integer (), nullable = False ),
53
- sa .PrimaryKeyConstraint ("id" ),
54
- sa .UniqueConstraint ("id" ),
111
+ if migrate_existing_cdms :
112
+ op .execute (
113
+ "INSERT INTO cdms (id, session_id_type, security_level, client_id_blob_filename, device_private_key, code, uploaded_by) SELECT id, session_id_type, security_level, client_id_blob_filename, device_private_key, code, uploaded_by FROM cdms_old"
55
114
)
56
115
57
- if not has_cdms :
58
- op .create_table (
59
- "cdms" ,
60
- sa .Column ("id" , sa .Integer (), autoincrement = True , nullable = False ),
61
- sa .Column ("session_id_type" , sa .String (length = 255 ), nullable = False ),
62
- sa .Column ("security_level" , sa .Integer (), nullable = False ),
63
- sa .Column ("client_id_blob_filename" , sa .Text (), nullable = False ),
64
- sa .Column ("device_private_key" , sa .Text (), nullable = False ),
65
- sa .Column ("code" , sa .Text (), nullable = False ),
66
- sa .Column ("uploaded_by" , sa .String (length = 255 ), nullable = True ),
67
- sa .ForeignKeyConstraint (
68
- ["uploaded_by" ],
69
- ["users.id" ],
70
- ),
71
- sa .PrimaryKeyConstraint ("id" ),
116
+ if migrate_existing_apikeys :
117
+ op .execute (
118
+ "INSERT INTO apikeys (id, created_at, api_key, user_id) SELECT id, created_at, api_key, user_id FROM apikeys_old"
72
119
)
73
120
74
- if not has_keys :
75
- op .create_table (
76
- "keys_" ,
77
- sa .Column ("kid" , sa .String (length = 32 ), nullable = False ),
78
- sa .Column ("added_at" , sa .Integer (), nullable = False ),
79
- sa .Column ("added_by" , sa .String (length = 255 ), nullable = True ),
80
- sa .Column ("license_url" , sa .Text (), nullable = False ),
81
- sa .Column ("key_" , sa .String (length = 255 ), nullable = False ),
82
- sa .ForeignKeyConstraint (
83
- ["added_by" ],
84
- ["users.id" ],
85
- ),
86
- sa .PrimaryKeyConstraint ("kid" ),
121
+ if migrate_existing_keys :
122
+ op .execute (
123
+ "INSERT INTO keys_ (kid, added_at, added_by, license_url, key_) SELECT kid, added_at, added_by, license_url, key_ FROM keys__old"
87
124
)
125
+
126
+ # drop old tables
127
+ if migrate_existing_apikeys :
128
+ op .drop_table ("apikeys_old" )
129
+
130
+ if migrate_existing_users :
131
+ op .drop_table ("users_old" )
132
+
133
+ if migrate_existing_cdms :
134
+ op .drop_table ("cdms_old" )
135
+
136
+ if migrate_existing_keys :
137
+ op .drop_table ("keys__old" )
88
138
# ### end Alembic commands ###
89
139
90
140
0 commit comments