From a5fc53353cea796da4fab8d2605b844cd4ef23ec Mon Sep 17 00:00:00 2001 From: Anda Date: Wed, 27 Mar 2024 06:50:37 -0700 Subject: [PATCH] faster migrations --- ...neric-metrics-nullable-batches.tx.down.sql | 23 ------------------- ...generic-metrics-nullable-batches.tx.up.sql | 22 ------------------ ...ic-metrics-text-partition-type.tx.down.sql | 5 ++-- ...eric-metrics-text-partition-type.tx.up.sql | 21 ++++++++++------- 4 files changed, 16 insertions(+), 55 deletions(-) delete mode 100644 master/static/migrations/20240325144650_generic-metrics-nullable-batches.tx.down.sql delete mode 100644 master/static/migrations/20240325144650_generic-metrics-nullable-batches.tx.up.sql diff --git a/master/static/migrations/20240325144650_generic-metrics-nullable-batches.tx.down.sql b/master/static/migrations/20240325144650_generic-metrics-nullable-batches.tx.down.sql deleted file mode 100644 index 054a0d51a8e..00000000000 --- a/master/static/migrations/20240325144650_generic-metrics-nullable-batches.tx.down.sql +++ /dev/null @@ -1,23 +0,0 @@ -/* - Rollback allowing NULL values on `metrics.total_batches`. - - Drops NOT NULL and default values on `total_batches` for child partitions of `metrics`. - Adds a global NOT NULL and default value back to `metrics` table to be inherited by all children. - */ - - --- Drop NOT NULL and default for `total_batches` on child partitions. -ALTER TABLE generic_metrics ALTER COLUMN total_batches DROP NOT NULL; -ALTER TABLE generic_metrics ALTER COLUMN total_batches DROP DEFAULT; - -ALTER TABLE raw_validations ALTER COLUMN total_batches DROP NOT NULL; -ALTER TABLE raw_validations ALTER COLUMN total_batches DROP DEFAULT; - -ALTER TABLE raw_steps ALTER COLUMN total_batches DROP NOT NULL; -ALTER TABLE raw_steps ALTER COLUMN total_batches DROP DEFAULT; - --- Add NOT NULL and default on `total_batches` to parent `metrics` table. -ALTER TABLE metrics ALTER COLUMN total_batches SET NOT NULL; -ALTER TABLE metrics ALTER COLUMN total_batches SET DEFAULT 0; - - diff --git a/master/static/migrations/20240325144650_generic-metrics-nullable-batches.tx.up.sql b/master/static/migrations/20240325144650_generic-metrics-nullable-batches.tx.up.sql deleted file mode 100644 index 93571ee0078..00000000000 --- a/master/static/migrations/20240325144650_generic-metrics-nullable-batches.tx.up.sql +++ /dev/null @@ -1,22 +0,0 @@ -/* - Allow NULL values on `metrics.total_batches`. - - Since `metrics` is a parent table that is partitioned into child tables, allow each child table - to define whether or not it requires `total_batches` to be defined. - */ - --- On parent `metrics` table: drop NOT NULL and default on `total_batches`. -ALTER TABLE metrics ALTER COLUMN total_batches DROP NOT NULL; -ALTER TABLE metrics ALTER COLUMN total_batches DROP DEFAULT; - - --- Add NOT NULL and default back to child partitions. -ALTER TABLE generic_metrics ALTER COLUMN total_batches SET NOT NULL; -ALTER TABLE generic_metrics ALTER COLUMN total_batches SET DEFAULT 0; - -ALTER TABLE raw_validations ALTER COLUMN total_batches SET NOT NULL; -ALTER TABLE raw_validations ALTER COLUMN total_batches SET DEFAULT 0; - -ALTER TABLE raw_steps ALTER COLUMN total_batches SET NOT NULL; -ALTER TABLE raw_steps ALTER COLUMN total_batches SET DEFAULT 0; - diff --git a/master/static/migrations/20240325144710_generic-metrics-text-partition-type.tx.down.sql b/master/static/migrations/20240325144710_generic-metrics-text-partition-type.tx.down.sql index 11bba1facf3..1fc0a4b724b 100644 --- a/master/static/migrations/20240325144710_generic-metrics-text-partition-type.tx.down.sql +++ b/master/static/migrations/20240325144710_generic-metrics-text-partition-type.tx.down.sql @@ -1,5 +1,6 @@ /* - Rollback changing `partition_type` on `metrics` from ENUM type to TEXT. + Rollback changing `partition_type` on `metrics` from ENUM type to TEXT, + dropping NOT NULL on `total_batches` Partition keys cannot be modified without dropping and recreating the table. */ @@ -52,7 +53,7 @@ CREATE TABLE metrics ( trial_id integer NOT NULL, end_time timestamp with time zone, metrics jsonb, - total_batches integer, + total_batches integer NOT NULL DEFAULT 0, trial_run_id integer NOT NULL DEFAULT 0, archived boolean NOT NULL DEFAULT false, id integer NOT NULL DEFAULT nextval('metrics_id_seq'), diff --git a/master/static/migrations/20240325144710_generic-metrics-text-partition-type.tx.up.sql b/master/static/migrations/20240325144710_generic-metrics-text-partition-type.tx.up.sql index 981d1ae2bae..969fd149f1b 100644 --- a/master/static/migrations/20240325144710_generic-metrics-text-partition-type.tx.up.sql +++ b/master/static/migrations/20240325144710_generic-metrics-text-partition-type.tx.up.sql @@ -1,5 +1,6 @@ /* - Change `partition_type` on `metrics` from ENUM type to TEXT. + Drop 'NOT NULL' on `metrics.total_batches`. + Change `partition_type` on `metrics` from ENUM type to TEXT. Partition keys cannot be modified without dropping and recreating the table. */ @@ -40,7 +41,7 @@ SELECT setval( true ); --- Re-create table with `partition_type` TEXT. +-- Re-create table with `partition_type` TEXT and nullable `total_batches` CREATE TABLE metrics ( trial_id integer NOT NULL, end_time timestamp with time zone, @@ -55,14 +56,18 @@ CREATE TABLE metrics ( -- Modify child partitions to have `partition_type` TEXT and set defaults. -ALTER TABLE raw_steps ALTER COLUMN partition_type TYPE text; -ALTER TABLE raw_steps ALTER COLUMN partition_type SET DEFAULT 'TRAINING'; +-- Drop and recreate the columns, since it's faster and we already know the values. +ALTER TABLE raw_steps DROP COLUMN partition_type; +ALTER TABLE raw_steps ADD COLUMN partition_type TEXT DEFAULT 'TRAINING'; +ALTER TABLE raw_steps ALTER COLUMN partition_type SET NOT NULL; -ALTER TABLE raw_validations ALTER COLUMN partition_type TYPE text; -ALTER TABLE raw_validations ALTER COLUMN partition_type SET DEFAULT 'VALIDATION'; +ALTER TABLE raw_validations DROP COLUMN partition_type; +ALTER TABLE raw_validations ADD COLUMN partition_type TEXT DEFAULT 'VALIDATION'; +ALTER TABLE raw_validations ALTER COLUMN partition_type SET NOT NULL; -ALTER TABLE generic_metrics ALTER COLUMN partition_type TYPE text; -ALTER TABLE generic_metrics ALTER COLUMN partition_type SET DEFAULT 'GENERIC'; +ALTER TABLE generic_metrics DROP COLUMN partition_type; +ALTER TABLE generic_metrics ADD COLUMN partition_type TEXT DEFAULT 'GENERIC'; +ALTER TABLE generic_metrics ALTER COLUMN partition_type SET NOT NULL; -- Drop `metric_partition_type` enum.