diff --git a/migrations/message_store_postgres/content_script_version_6.nim b/migrations/message_store_postgres/content_script_version_6.nim index 38a25ac4ed..50dc192761 100644 --- a/migrations/message_store_postgres/content_script_version_6.nim +++ b/migrations/message_store_postgres/content_script_version_6.nim @@ -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; + """ diff --git a/migrations/message_store_postgres/content_script_version_6_manual.nim b/migrations/message_store_postgres/content_script_version_6_manual.nim deleted file mode 100644 index 3ad8bc570d..0000000000 --- a/migrations/message_store_postgres/content_script_version_6_manual.nim +++ /dev/null @@ -1,117 +0,0 @@ -## Manual script for migration 5 -> 6 -## This script was created for situation where the total disk size can't be doubled. - -const ContentScriptVersion_6_1 = -""" --- Rename old table -ALTER TABLE MESSAGES -RENAME TO OLD_MESSAGES; - --- Remove old message index -ALTER TABLE OLD_MESSAGES -DROP CONSTRAINT MESSAGEINDEX; - --- Create new empty table -CREATE TABLE 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; - - -- Create new partition with the same name and bounds - EXECUTE format('CREATE TABLE new_%I PARTITION OF messages FOR VALUES FROM (%L) TO (%L)', partition_name, min_timestamp, max_timestamp + 1); - - ELSE - - -- Drop old partition. - EXECUTE format('DROP TABLE %I', partition_name); - - END If; - - END LOOP; - -END $$; - --- Update to new version -UPDATE VERSION -SET - VERSION = 6 -WHERE - VERSION = 5; -""" - -## This MUST be run after v6.1 -## This script MUST be run for each partitions in the DB. -## Then the old_messages table can be dropped. -const ContentScriptVersion_6_2 = - """ -DO $$ -DECLARE - partition_name TEXT; - partition_count numeric; -BEGIN - - 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 LIMIT 1 INTO partition_name; - - -- 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 - - -- Insert partition rows into new table - EXECUTE format('INSERT INTO new_%I (messageHash, pubsubTopic, contentTopic, payload, version, timestamp, meta) - SELECT messageHash, pubsubTopic, contentTopic, payload, version, timestamp, meta - FROM %I', partition_name, partition_name); - - -- Drop old partition. - EXECUTE format('DROP TABLE %I', partition_name); - - -- Rename new partition - EXECUTE format('ALTER TABLE new_%I RENAME TO %I', partition_name, partition_name); - - END IF; - -END $$; -"""