-
Notifications
You must be signed in to change notification settings - Fork 53
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
1 parent
a63db95
commit 65423d4
Showing
2 changed files
with
8 additions
and
195 deletions.
There are no files selected for viewing
86 changes: 8 additions & 78 deletions
86
migrations/message_store_postgres/content_script_version_6.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,85 +1,15 @@ | ||
const ContentScriptVersion_6* = | ||
""" | ||
-- Rename old table | ||
ALTER TABLE IF EXISTS MESSAGES | ||
RENAME TO OLD_MESSAGES; | ||
-- we can drop the timestamp column because this data is also kept in the storedAt column | ||
ALTER TABLE messages DROP COLUMN timestamp; | ||
-- Remove old message index | ||
ALTER TABLE IF EXISTS OLD_MESSAGES | ||
DROP CONSTRAINT MESSAGEINDEX; | ||
-- drop unused column | ||
ALTER TABLE messages DROP COLUMN id; | ||
-- Create new empty table | ||
CREATE TABLE IF NOT EXISTS NEW_MESSAGES ( | ||
MESSAGEHASH VARCHAR NOT NULL, | ||
PUBSUBTOPIC VARCHAR NOT NULL, | ||
CONTENTTOPIC VARCHAR NOT NULL, | ||
PAYLOAD VARCHAR, | ||
VERSION INTEGER NOT NULL, | ||
TIMESTAMP BIGINT NOT NULL, | ||
META VARCHAR, | ||
CONSTRAINT MESSAGEINDEX PRIMARY KEY (TIMESTAMP, MESSAGEHASH) | ||
) | ||
PARTITION BY | ||
RANGE (TIMESTAMP); | ||
DO $$ | ||
DECLARE | ||
partition_name TEXT; | ||
partition_count numeric; | ||
min_timestamp numeric; | ||
max_timestamp numeric; | ||
BEGIN | ||
FOR partition_name in | ||
(SELECT child.relname AS partition_name FROM pg_inherits | ||
JOIN pg_class parent ON pg_inherits.inhparent = parent.oid | ||
JOIN pg_class child ON pg_inherits.inhrelid = child.oid | ||
JOIN pg_namespace nmsp_parent ON nmsp_parent.oid = parent.relnamespace | ||
JOIN pg_namespace nmsp_child ON nmsp_child.oid = child.relnamespace | ||
WHERE parent.relname='old_messages' | ||
ORDER BY partition_name ASC) | ||
LOOP | ||
-- Get the number of rows of this partition | ||
EXECUTE format('SELECT COUNT(1) FROM %I', partition_name) INTO partition_count; | ||
IF partition_count > 0 THEN | ||
-- Get the smallest timestamp of this partition | ||
EXECUTE format('SELECT MIN(timestamp) FROM %I', partition_name) INTO min_timestamp; | ||
-- Get the largest timestamp of this partition | ||
EXECUTE format('SELECT MAX(timestamp) FROM %I', partition_name) INTO max_timestamp; | ||
-- Rename old partition | ||
EXECUTE format('ALTER TABLE %I RENAME TO old_%I', partition_name, partition_name); | ||
-- Create new partition with the same name and bounds | ||
EXECUTE format('CREATE TABLE %I PARTITION OF new_messages FOR VALUES FROM (%L) TO (%L)', partition_name, min_timestamp, max_timestamp + 1); | ||
-- Insert partition rows into new table | ||
EXECUTE format('INSERT INTO %I (messageHash, pubsubTopic, contentTopic, payload, version, timestamp, meta, id, storedAt) | ||
SELECT messageHash, pubsubTopic, contentTopic, payload, version, timestamp, meta, id, storedAt | ||
FROM old_%I', partition_name, partition_name); | ||
-- Drop old partition. | ||
EXECUTE format('DROP TABLE old_%I', partition_name); | ||
END IF; | ||
END LOOP; | ||
END $$; | ||
-- Remove old table | ||
DROP TABLE IF EXISTS OLD_MESSAGES; | ||
-- Rename new table | ||
ALTER TABLE IF EXISTS NEW_MESSAGES | ||
RENAME TO MESSAGES; | ||
-- from now on we are only interested in the message timestamp | ||
ALTER TABLE messages RENAME COLUMN storedAt TO timestamp; | ||
-- Update to new version | ||
UPDATE VERSION | ||
SET | ||
VERSION = 6 | ||
WHERE | ||
VERSION = 5; | ||
UPDATE version SET version = 6 WHERE version = 5; | ||
""" |
117 changes: 0 additions & 117 deletions
117
migrations/message_store_postgres/content_script_version_6_manual.nim
This file was deleted.
Oops, something went wrong.