@@ -120,8 +120,9 @@ def database_init():
120
120
check_for_hacks ,
121
121
seqno_etc_updates ,
122
122
):
123
- if migrate (conn ):
124
- changes = True
123
+ with transaction (conn ):
124
+ if migrate (conn ):
125
+ changes = True
125
126
126
127
if changes :
127
128
metadata .clear ()
@@ -308,20 +309,19 @@ def seqno_etc_updates(conn):
308
309
if 'seqno' in metadata .tables ['messages' ].c :
309
310
return False
310
311
311
- with transaction (dbconn = conn ):
312
- logging .warning ("Applying message_sequence renames" )
313
- conn .execute ("ALTER TABLE rooms RENAME COLUMN updates TO message_sequence" )
314
- conn .execute ("ALTER TABLE messages RENAME COLUMN updated TO seqno" )
315
-
316
- # We can't insert the required pinned_messages because we don't have the pinned_by user, but
317
- # that isn't a big deal since we didn't have any endpoints for pinned messsages before this
318
- # anyway, so we just recreate the whole thing (along with triggers which we also need to
319
- # update/fix)
320
- logging .warning ("Recreating pinned_messages table" )
321
- conn .execute ("DROP TABLE pinned_messages" )
322
- if engine .name == 'sqlite' :
323
- conn .execute (
324
- """
312
+ logging .warning ("Applying message_sequence renames" )
313
+ conn .execute ("ALTER TABLE rooms RENAME COLUMN updates TO message_sequence" )
314
+ conn .execute ("ALTER TABLE messages RENAME COLUMN updated TO seqno" )
315
+
316
+ # We can't insert the required pinned_messages because we don't have the pinned_by user, but
317
+ # that isn't a big deal since we didn't have any endpoints for pinned messsages before this
318
+ # anyway, so we just recreate the whole thing (along with triggers which we also need to
319
+ # update/fix)
320
+ logging .warning ("Recreating pinned_messages table" )
321
+ conn .execute ("DROP TABLE pinned_messages" )
322
+ if engine .name == 'sqlite' :
323
+ conn .execute (
324
+ """
325
325
CREATE TABLE pinned_messages (
326
326
room INTEGER NOT NULL REFERENCES rooms(id) ON DELETE CASCADE,
327
327
message INTEGER NOT NULL REFERENCES rooms(id) ON DELETE CASCADE,
@@ -330,49 +330,49 @@ def seqno_etc_updates(conn):
330
330
PRIMARY KEY(room, message)
331
331
)
332
332
""" # noqa: E501
333
- )
334
- conn .execute (
335
- """
333
+ )
334
+ conn .execute (
335
+ """
336
336
CREATE TRIGGER messages_after_delete AFTER UPDATE OF data ON messages
337
337
FOR EACH ROW WHEN NEW.data IS NULL AND OLD.data IS NOT NULL
338
338
BEGIN
339
339
-- Unpin if we deleted a pinned message:
340
340
DELETE FROM pinned_messages WHERE message = OLD.id;
341
341
END
342
342
"""
343
- )
344
- conn .execute (
345
- """
343
+ )
344
+ conn .execute (
345
+ """
346
346
CREATE TRIGGER room_metadata_pinned_add AFTER INSERT ON pinned_messages
347
347
FOR EACH ROW
348
348
BEGIN
349
349
UPDATE rooms SET info_updates = info_updates + 1 WHERE id = NEW.room;
350
350
END
351
351
"""
352
- )
353
- conn .execute (
354
- """
352
+ )
353
+ conn .execute (
354
+ """
355
355
CREATE TRIGGER room_metadata_pinned_update AFTER UPDATE ON pinned_messages
356
356
FOR EACH ROW
357
357
BEGIN
358
358
UPDATE rooms SET info_updates = info_updates + 1 WHERE id = NEW.room;
359
359
END
360
360
"""
361
- )
362
- conn .execute (
363
- """
361
+ )
362
+ conn .execute (
363
+ """
364
364
CREATE TRIGGER room_metadata_pinned_remove AFTER DELETE ON pinned_messages
365
365
FOR EACH ROW
366
366
BEGIN
367
367
UPDATE rooms SET info_updates = info_updates + 1 WHERE id = OLD.room;
368
368
END
369
369
"""
370
- )
370
+ )
371
371
372
- logging .warning ("Fixing user_permissions view" )
373
- conn .execute ("DROP VIEW IF EXISTS user_permissions" )
374
- conn .execute (
375
- """
372
+ logging .warning ("Fixing user_permissions view" )
373
+ conn .execute ("DROP VIEW IF EXISTS user_permissions" )
374
+ conn .execute (
375
+ """
376
376
CREATE VIEW user_permissions AS
377
377
SELECT
378
378
rooms.id AS room,
@@ -399,12 +399,12 @@ def seqno_etc_updates(conn):
399
399
users JOIN rooms LEFT OUTER JOIN user_permission_overrides ON
400
400
users.id = user_permission_overrides.user AND rooms.id = user_permission_overrides.room
401
401
""" # noqa: E501
402
- )
402
+ )
403
403
404
- else : # postgresql
405
- logging .warning ("Recreating pinned_messages table" )
406
- conn .execute (
407
- """
404
+ else : # postgresql
405
+ logging .warning ("Recreating pinned_messages table" )
406
+ conn .execute (
407
+ """
408
408
CREATE TABLE pinned_messages (
409
409
room BIGINT NOT NULL REFERENCES rooms ON DELETE CASCADE,
410
410
message BIGINT NOT NULL REFERENCES rooms ON DELETE CASCADE,
@@ -434,11 +434,11 @@ def seqno_etc_updates(conn):
434
434
FOR EACH ROW
435
435
EXECUTE PROCEDURE trigger_room_metadata_info_update_old();
436
436
"""
437
- )
437
+ )
438
438
439
- logging .warning ("Fixing user_permissions view" )
440
- conn .execute (
441
- """
439
+ logging .warning ("Fixing user_permissions view" )
440
+ conn .execute (
441
+ """
442
442
CREATE OR REPLACE VIEW user_permissions AS
443
443
SELECT
444
444
rooms.id AS room,
@@ -465,7 +465,7 @@ def seqno_etc_updates(conn):
465
465
users CROSS JOIN rooms LEFT OUTER JOIN user_permission_overrides ON
466
466
(users.id = user_permission_overrides."user" AND rooms.id = user_permission_overrides.room);
467
467
""" # noqa: E501
468
- )
468
+ )
469
469
470
470
return True
471
471
0 commit comments