-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: simplify migration script postgres version_4 (#2674)
- Loading branch information
1 parent
38f8b08
commit 91c8573
Showing
1 changed file
with
1 addition
and
64 deletions.
There are no files selected for viewing
65 changes: 1 addition & 64 deletions
65
migrations/message_store_postgres/content_script_version_4.nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,9 @@ | ||
const ContentScriptVersion_4* = | ||
""" | ||
ALTER TABLE IF EXISTS messages_backup RENAME TO messages; | ||
ALTER TABLE messages RENAME TO messages_backup; | ||
CREATE TABLE IF NOT EXISTS messages ( | ||
pubsubTopic VARCHAR NOT NULL, | ||
contentTopic VARCHAR NOT NULL, | ||
payload VARCHAR, | ||
version INTEGER NOT NULL, | ||
timestamp BIGINT NOT NULL, | ||
id VARCHAR NOT NULL, | ||
messageHash VARCHAR NOT NULL, | ||
storedAt BIGINT NOT NULL, | ||
meta VARCHAR, | ||
CONSTRAINT messageIndex PRIMARY KEY (messageHash, storedAt) | ||
) PARTITION BY RANGE (storedAt); | ||
DO $$ | ||
DECLARE | ||
min_storedAt numeric; | ||
max_storedAt numeric; | ||
min_storedAtSeconds integer = 0; | ||
max_storedAtSeconds integer = 0; | ||
partition_name TEXT; | ||
create_partition_stmt TEXT; | ||
BEGIN | ||
SELECT MIN(storedAt) into min_storedAt | ||
FROM messages_backup; | ||
SELECT MAX(storedAt) into max_storedAt | ||
FROM messages_backup; | ||
min_storedAtSeconds := min_storedAt / 1000000000; | ||
max_storedAtSeconds := max_storedAt / 1000000000; | ||
partition_name := 'messages_' || min_storedAtSeconds || '_' || max_storedAtSeconds; | ||
create_partition_stmt := 'CREATE TABLE ' || partition_name || | ||
' PARTITION OF messages FOR VALUES FROM (' || | ||
min_storedAt || ') TO (' || (max_storedAt + 1) || ')'; | ||
IF min_storedAtSeconds > 0 AND max_storedAtSeconds > 0 THEN | ||
EXECUTE create_partition_stmt USING partition_name, min_storedAt, max_storedAt; | ||
END IF; | ||
END $$; | ||
INSERT INTO messages ( | ||
pubsubTopic, | ||
contentTopic, | ||
payload, | ||
version, | ||
timestamp, | ||
id, | ||
messageHash, | ||
storedAt | ||
) | ||
SELECT pubsubTopic, | ||
contentTopic, | ||
payload, | ||
version, | ||
timestamp, | ||
id, | ||
messageHash, | ||
storedAt | ||
FROM messages_backup; | ||
ALTER TABLE messages ADD meta VARCHAR default null; | ||
CREATE INDEX IF NOT EXISTS i_query ON messages (contentTopic, pubsubTopic, storedAt, id); | ||
DROP TABLE messages_backup; | ||
UPDATE version SET version = 4 WHERE version = 3; | ||
""" |