Skip to content

Commit c44dc0c

Browse files
committed
Make sure migrations happen inside a transaction
1 parent 79b0b51 commit c44dc0c

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

sogs/db.py

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ def database_init():
120120
check_for_hacks,
121121
seqno_etc_updates,
122122
):
123-
if migrate(conn):
124-
changes = True
123+
with transaction(conn):
124+
if migrate(conn):
125+
changes = True
125126

126127
if changes:
127128
metadata.clear()
@@ -308,20 +309,19 @@ def seqno_etc_updates(conn):
308309
if 'seqno' in metadata.tables['messages'].c:
309310
return False
310311

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+
"""
325325
CREATE TABLE pinned_messages (
326326
room INTEGER NOT NULL REFERENCES rooms(id) ON DELETE CASCADE,
327327
message INTEGER NOT NULL REFERENCES rooms(id) ON DELETE CASCADE,
@@ -330,49 +330,49 @@ def seqno_etc_updates(conn):
330330
PRIMARY KEY(room, message)
331331
)
332332
""" # noqa: E501
333-
)
334-
conn.execute(
335-
"""
333+
)
334+
conn.execute(
335+
"""
336336
CREATE TRIGGER messages_after_delete AFTER UPDATE OF data ON messages
337337
FOR EACH ROW WHEN NEW.data IS NULL AND OLD.data IS NOT NULL
338338
BEGIN
339339
-- Unpin if we deleted a pinned message:
340340
DELETE FROM pinned_messages WHERE message = OLD.id;
341341
END
342342
"""
343-
)
344-
conn.execute(
345-
"""
343+
)
344+
conn.execute(
345+
"""
346346
CREATE TRIGGER room_metadata_pinned_add AFTER INSERT ON pinned_messages
347347
FOR EACH ROW
348348
BEGIN
349349
UPDATE rooms SET info_updates = info_updates + 1 WHERE id = NEW.room;
350350
END
351351
"""
352-
)
353-
conn.execute(
354-
"""
352+
)
353+
conn.execute(
354+
"""
355355
CREATE TRIGGER room_metadata_pinned_update AFTER UPDATE ON pinned_messages
356356
FOR EACH ROW
357357
BEGIN
358358
UPDATE rooms SET info_updates = info_updates + 1 WHERE id = NEW.room;
359359
END
360360
"""
361-
)
362-
conn.execute(
363-
"""
361+
)
362+
conn.execute(
363+
"""
364364
CREATE TRIGGER room_metadata_pinned_remove AFTER DELETE ON pinned_messages
365365
FOR EACH ROW
366366
BEGIN
367367
UPDATE rooms SET info_updates = info_updates + 1 WHERE id = OLD.room;
368368
END
369369
"""
370-
)
370+
)
371371

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+
"""
376376
CREATE VIEW user_permissions AS
377377
SELECT
378378
rooms.id AS room,
@@ -399,12 +399,12 @@ def seqno_etc_updates(conn):
399399
users JOIN rooms LEFT OUTER JOIN user_permission_overrides ON
400400
users.id = user_permission_overrides.user AND rooms.id = user_permission_overrides.room
401401
""" # noqa: E501
402-
)
402+
)
403403

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+
"""
408408
CREATE TABLE pinned_messages (
409409
room BIGINT NOT NULL REFERENCES rooms ON DELETE CASCADE,
410410
message BIGINT NOT NULL REFERENCES rooms ON DELETE CASCADE,
@@ -434,11 +434,11 @@ def seqno_etc_updates(conn):
434434
FOR EACH ROW
435435
EXECUTE PROCEDURE trigger_room_metadata_info_update_old();
436436
"""
437-
)
437+
)
438438

439-
logging.warning("Fixing user_permissions view")
440-
conn.execute(
441-
"""
439+
logging.warning("Fixing user_permissions view")
440+
conn.execute(
441+
"""
442442
CREATE OR REPLACE VIEW user_permissions AS
443443
SELECT
444444
rooms.id AS room,
@@ -465,7 +465,7 @@ def seqno_etc_updates(conn):
465465
users CROSS JOIN rooms LEFT OUTER JOIN user_permission_overrides ON
466466
(users.id = user_permission_overrides."user" AND rooms.id = user_permission_overrides.room);
467467
""" # noqa: E501
468-
)
468+
)
469469

470470
return True
471471

0 commit comments

Comments
 (0)