-
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.
- Loading branch information
Showing
7 changed files
with
139 additions
and
25 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const ContentScriptVersion_4* = | ||
""" | ||
ALTER TABLE IF EXISTS messages_backup RENAME TO messages; | ||
ALTER TABLE messages RENAME TO messages_backup; | ||
ALTER TABLE messages_backup DROP CONSTRAINT messageIndex; | ||
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; | ||
DROP TABLE messages_backup; | ||
UPDATE version SET version = 4 WHERE version = 3; | ||
""" |
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
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
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
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
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
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