diff --git a/migrations/message_store_postgres/content_script_version_4.nim b/migrations/message_store_postgres/content_script_version_4.nim index 2f6ff236e..50ee269f6 100644 --- a/migrations/message_store_postgres/content_script_version_4.nim +++ b/migrations/message_store_postgres/content_script_version_4.nim @@ -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; """