From 7b42defb9a6d95b5d1fe677188d4c4eddd1ddb59 Mon Sep 17 00:00:00 2001 From: Daniel Jiang Date: Wed, 26 Aug 2020 16:50:25 +0800 Subject: [PATCH] Make the 2.1.0 migration SQL script idempotent Signed-off-by: Daniel Jiang --- make/migrations/postgresql/0040_2.1.0_schema.up.sql | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/make/migrations/postgresql/0040_2.1.0_schema.up.sql b/make/migrations/postgresql/0040_2.1.0_schema.up.sql index 5de5f6ade..41c78e8f7 100644 --- a/make/migrations/postgresql/0040_2.1.0_schema.up.sql +++ b/make/migrations/postgresql/0040_2.1.0_schema.up.sql @@ -40,7 +40,7 @@ ALTER TABLE blob ADD COLUMN IF NOT EXISTS version BIGINT default 0; CREATE INDEX IF NOT EXISTS idx_status ON blob (status); CREATE INDEX IF NOT EXISTS idx_version ON blob (version); -CREATE TABLE p2p_preheat_instance ( +CREATE TABLE IF NOT EXISTS p2p_preheat_instance ( id SERIAL PRIMARY KEY NOT NULL, name varchar(255) NOT NULL, description varchar(255), @@ -117,10 +117,10 @@ ALTER TABLE schedule DROP COLUMN IF EXISTS status; UPDATE registry SET type = 'quay' WHERE type = 'quay-io'; -ALTER TABLE artifact ADD COLUMN icon varchar(255); +ALTER TABLE artifact ADD COLUMN IF NOT EXISTS icon varchar(255); /*remove the constraint for name in table 'notification_policy'*/ -ALTER TABLE notification_policy DROP CONSTRAINT notification_policy_name_key; +ALTER TABLE notification_policy DROP CONSTRAINT IF EXISTS notification_policy_name_key; /*add union unique constraint for name and project_id in table 'notification_policy'*/ ALTER TABLE notification_policy ADD UNIQUE(name,project_id); @@ -130,13 +130,14 @@ CREATE TABLE IF NOT EXISTS data_migrations ( creation_time timestamp default CURRENT_TIMESTAMP, update_time timestamp default CURRENT_TIMESTAMP ); -INSERT INTO data_migrations (version) VALUES ( +/* Only insert the record when the table is empty */ +INSERT INTO data_migrations (version) SELECT ( CASE /*if the "extra_attrs" isn't null, it means that the deployment upgrades from v2.0*/ WHEN (SELECT Count(*) FROM artifact WHERE extra_attrs!='')>0 THEN 30 ELSE 0 END -); +) WHERE NOT EXISTS (SELECT * FROM data_migrations); ALTER TABLE schema_migrations DROP COLUMN IF EXISTS data_version; ALTER TABLE artifact ADD COLUMN IF NOT EXISTS icon varchar(255);