From 3a60a0aacd008ea94d9a4162cc1e6e1b1a6fc08d Mon Sep 17 00:00:00 2001 From: Alex Kriuchykhin Date: Fri, 3 Mar 2023 10:24:14 +0100 Subject: [PATCH] Change migration of protocol templates - do not publish unlinked protocols [SCI-8062] (#5062) --- .../20221125133611_add_protocol_versioning.rb | 36 ++++++++++++------- db/structure.sql | 13 ++++--- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/db/migrate/20221125133611_add_protocol_versioning.rb b/db/migrate/20221125133611_add_protocol_versioning.rb index 0d4a1a03a..e334ffe14 100644 --- a/db/migrate/20221125133611_add_protocol_versioning.rb +++ b/db/migrate/20221125133611_add_protocol_versioning.rb @@ -13,26 +13,38 @@ class AddProtocolVersioning < ActiveRecord::Migration[6.1] t.references :published_by, index: true, foreign_key: { to_table: :users } end + execute( + 'UPDATE "protocols" SET "protocol_type" = 3, "archived" = TRUE WHERE "protocols"."protocol_type" = 4;' + ) + execute( + 'UPDATE "protocols" SET "visibility" = 1 WHERE "protocols"."protocol_type" = 3;' + ) + execute( + 'UPDATE "protocols" SET "protocol_type" = 2 ' \ + 'WHERE "id" IN (' \ + 'SELECT DISTINCT "protocols"."id" FROM "protocols" ' \ + 'JOIN "protocols" "linked_children" ON "linked_children"."parent_id" = "protocols"."id" ' \ + 'WHERE "protocols"."protocol_type" IN (2, 3)' \ + ');' + ) + execute( + 'UPDATE "protocols" SET "protocol_type" = 3 ' \ + 'WHERE "id" IN (' \ + 'SELECT DISTINCT "protocols"."id" FROM "protocols" ' \ + 'LEFT OUTER JOIN "protocols" "linked_children" ON "linked_children"."parent_id" = "protocols"."id" ' \ + 'WHERE "protocols"."protocol_type" IN (2, 3) AND "linked_children"."id" IS NULL' \ + ');' + ) execute( 'UPDATE "protocols" SET "published_on" = "created_at", "published_by_id" = "added_by_id" ' \ - 'WHERE "protocols"."protocol_type" IN (2, 4) ' \ + 'WHERE "protocols"."protocol_type" = 2 ' \ 'AND "protocols"."published_on" IS NULL;' ) - execute( - 'UPDATE "protocols" SET "published_by_id" = "added_by_id", "visibility" = 1 ' \ - 'WHERE "protocols"."protocol_type" = 3;' - ) - execute( - 'UPDATE "protocols" SET "archived" = TRUE WHERE "protocols"."protocol_type" = 4;' - ) - execute( - 'UPDATE "protocols" SET "protocol_type" = 2 WHERE "protocols"."protocol_type" IN (3, 4);' - ) end def down execute( - 'UPDATE "protocols" SET "protocol_type" = 4 WHERE "protocols"."protocol_type" = 2 AND ' \ + 'UPDATE "protocols" SET "protocol_type" = 4 WHERE "protocols"."protocol_type" IN (2, 3, 4) AND ' \ '"protocols"."archived" = TRUE;' ) change_table :protocols, bulk: true do |t| diff --git a/db/structure.sql b/db/structure.sql index 26d79578e..1da5cf729 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1380,7 +1380,8 @@ CREATE TABLE public.protocols ( default_public_user_role_id bigint, previous_version_id bigint, last_modified_by_id bigint, - published_by_id bigint + published_by_id bigint, + linked_at timestamp without time zone ); @@ -5646,17 +5647,17 @@ CREATE INDEX index_protocols_on_parent_id ON public.protocols USING btree (paren -- -<<<<<<< HEAD -- Name: index_protocols_on_previous_version_id; Type: INDEX; Schema: public; Owner: - -- CREATE INDEX index_protocols_on_previous_version_id ON public.protocols USING btree (previous_version_id); -======= + + +-- -- Name: index_protocols_on_protocol_code; Type: INDEX; Schema: public; Owner: - -- CREATE INDEX index_protocols_on_protocol_code ON public.protocols USING gin ((('PT'::text || id)) public.gin_trgm_ops); ->>>>>>> develop -- @@ -8683,6 +8684,8 @@ INSERT INTO "schema_migrations" (version) VALUES ('20221125133611'), ('20221222123021'), ('20230120141017'), -('20230206095817'); +('20230206095817'), +('20230223142119'), +('20230227131215');