migrations: Fix UserMessage.id sequence calculation in 0240.

The sequence value should reflect the last id, not the next id, to
avoid leaving a gap of 1.  Also, it should take ArchivedUserMessage.id
into account to avoid collisions during future archiving.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2022-10-01 16:41:09 -07:00 committed by Tim Abbott
parent 11a86ec328
commit 67cbb21267
1 changed files with 7 additions and 1 deletions

View File

@ -22,7 +22,13 @@ class Migration(migrations.Migration):
ALTER TABLE zerver_usermessage RENAME COLUMN bigint_id to id;
ALTER TABLE zerver_usermessage ADD CONSTRAINT zerver_usermessage_pkey PRIMARY KEY USING INDEX zerver_usermessage_bigint_id_idx;
CREATE SEQUENCE zerver_usermessage_id_seq;
SELECT SETVAL('zerver_usermessage_id_seq', (SELECT MAX(id)+1 FROM zerver_usermessage));
SELECT setval(
'zerver_usermessage_id_seq',
GREATEST(
(SELECT max(id) FROM zerver_usermessage),
(SELECT max(id) FROM zerver_archivedusermessage)
)
);
ALTER TABLE zerver_usermessage ALTER COLUMN id SET DEFAULT NEXTVAL('zerver_usermessage_id_seq');
ALTER TABLE zerver_usermessage ALTER COLUMN id_old DROP NOT NULL;
""",