14
14
import importlib .resources
15
15
import sys
16
16
import re
17
+ import time
17
18
18
19
import sogs # noqa: F401
19
20
50
51
old = sqlite3 .connect (f"file:{ args .sogs_db [0 ]} ?mode=ro" , uri = True )
51
52
old .row_factory = sqlite3 .Row
52
53
53
- pgsql = psycopg .connect (args .postgresql_url [0 ])
54
+ pgsql = psycopg .connect (args .postgresql_url [0 ], autocommit = True )
54
55
55
56
TABLES = [
56
57
"rooms" ,
113
114
# We have circular foreign keys that we need to break for the copy to work:
114
115
curout .execute ("ALTER TABLE rooms DROP CONSTRAINT room_image_fk" )
115
116
116
- curout .execute ("SET CONSTRAINTS ALL DEFERRED" )
117
-
118
117
def copy (table ):
119
118
120
119
cols = [r ['name' ] for r in curin .execute (f"PRAGMA table_info({ table } )" )]
@@ -141,6 +140,9 @@ def copy(table):
141
140
)
142
141
143
142
count = 0
143
+ count_total = curin .execute (f"SELECT COUNT(*) FROM { table } " ).fetchone ()[0 ]
144
+ print (f"Importing { table } : { count } /{ count_total } " , end = "" , flush = True )
145
+ started = time .time ()
144
146
for row in curin .execute (f"SELECT * FROM { table } " ):
145
147
colnames = ', ' .join ('"' + c + '"' if c == "user" else c for c in cols )
146
148
vals = ', ' .join ('%s' for _ in cols )
@@ -153,27 +155,67 @@ def copy(table):
153
155
)
154
156
count += 1
155
157
156
- print (f"rooms: { count } rows copied" )
158
+ if count % 10 == 0 and args .commit :
159
+ curout .execute ("COMMIT; BEGIN" )
160
+ if count % 1000 == 0 :
161
+ print (f"\r Importing { table } : { count } /{ count_total } " , end = "" , flush = True )
162
+
163
+ if args .commit :
164
+ curout .execute ("COMMIT; BEGIN" )
165
+ print (
166
+ f"\r Finished importing { table } : { count } /{ count_total } rows imported "
167
+ f"[{ time .time () - started :.3f} s]" ,
168
+ flush = True ,
169
+ )
157
170
158
171
for t in TABLES :
159
172
copy (t )
160
173
161
174
# Put the foreign key we deleted back in:
175
+ print ("Reactivating room_image foreign key..." , end = "" , flush = True )
176
+ started = time .time ()
162
177
curout .execute (
163
178
"ALTER TABLE rooms ADD CONSTRAINT room_image_fk FOREIGN KEY (image)"
164
179
" REFERENCES files ON DELETE SET NULL"
165
180
)
181
+ if args .commit :
182
+ curout .execute ("COMMIT; BEGIN" )
183
+ print (f" done [{ time .time () - started :.3f} s]." )
166
184
167
185
# Our DB triggers mess with the seqno/updates values, so restore them:
186
+ print ("Updating message sequence counters..." , end = "" , flush = True )
187
+ started = time .time ()
188
+ count = 0
189
+ count_total = curin .execute ("SELECT COUNT(*) FROM messages" ).fetchone ()[0 ]
168
190
for row in curin .execute ("SELECT id, seqno FROM messages" ):
169
191
curout .execute ("UPDATE messages SET seqno = %s WHERE id = %s" , [row [1 ], row [0 ]])
192
+ count += 1
193
+ if count % 1000 == 0 :
194
+ if args .commit :
195
+ curout .execute ("COMMIT; BEGIN" )
196
+ print (
197
+ f"\r Updating message sequence counters... { count } /{ count_total } " , end = "" , flush = True
198
+ )
199
+ if args .commit :
200
+ curout .execute ("COMMIT; BEGIN" )
201
+ print (
202
+ f"\r Updated message sequence counters... { count } /{ count_total } "
203
+ f"[{ time .time () - started :.3f} s]" ,
204
+ flush = True ,
205
+ )
206
+
207
+ print ("Updating room sequence/updates counters..." )
208
+ started = time .time ()
170
209
for row in curin .execute ("SELECT id, message_sequence, info_updates FROM rooms" ):
171
210
curout .execute (
172
211
"UPDATE rooms SET message_sequence = %s, info_updates = %s WHERE id = %s" ,
173
212
[row [1 ], row [2 ], row [0 ]],
174
213
)
214
+ print (f" done [{ time .time () - started :.3f} s]." )
175
215
176
216
# Restart the identity sequences (otherwise new inserts will fail with conflicting ids)
217
+ print ("Restarting identity sequences..." , end = "" , flush = True )
218
+ started = time .time ()
177
219
identities = [
178
220
(r [0 ], r [1 ])
179
221
for r in curout .execute (
@@ -184,8 +226,9 @@ def copy(table):
184
226
for table , col in identities :
185
227
next_id = curout .execute (f"SELECT MAX({ col } ) FROM { table } " ).fetchone ()[0 ]
186
228
if next_id is not None :
187
- print (f"Updating { table } .{ col } identity to start at { next_id + 1 } " )
229
+ print (f" { table } .{ col } = { next_id + 1 } " , end = "" , flush = True )
188
230
curout .execute (f"ALTER TABLE { table } ALTER COLUMN { col } RESTART WITH { next_id + 1 } " )
231
+ print (f". Done [{ time .time () - started :.3f} s]." )
189
232
190
233
if not args .commit :
191
234
print ("Import finished, aborting transaction (because --commit not given)" )
0 commit comments