2022-12-01 02:52:36 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class AddProtocolVersioning < ActiveRecord::Migration[6.1]
|
|
|
|
def up
|
|
|
|
change_table :protocols, bulk: true do |t|
|
2023-02-13 23:18:26 +08:00
|
|
|
t.integer :visibility, index: true, default: 0
|
2022-12-01 02:52:36 +08:00
|
|
|
t.boolean :archived, default: false, null: false, index: true
|
|
|
|
t.integer :version_number, default: 1
|
2023-01-20 21:29:25 +08:00
|
|
|
t.string :version_comment
|
2023-02-13 23:18:26 +08:00
|
|
|
t.references :default_public_user_role, foreign_key: { to_table: :user_roles }
|
2023-01-20 21:29:25 +08:00
|
|
|
t.references :previous_version, index: true, foreign_key: { to_table: :protocols }
|
|
|
|
t.references :last_modified_by, index: true, foreign_key: { to_table: :users }
|
|
|
|
t.references :published_by, index: true, foreign_key: { to_table: :users }
|
2022-12-01 02:52:36 +08:00
|
|
|
end
|
2023-02-13 23:18:26 +08:00
|
|
|
|
2023-03-30 19:22:25 +08:00
|
|
|
execute(
|
|
|
|
'UPDATE "protocols" SET "visibility" = 1 WHERE "protocols"."protocol_type" = 4 AND "protocols"."published_on" IS NOT NULL;'
|
|
|
|
)
|
2023-01-20 21:29:25 +08:00
|
|
|
execute(
|
2023-03-24 17:58:43 +08:00
|
|
|
'UPDATE "protocols" SET "protocol_type" = 6, "archived" = TRUE, "published_on" = NULL WHERE "protocols"."protocol_type" = 4;'
|
2023-03-03 17:24:14 +08:00
|
|
|
)
|
|
|
|
execute(
|
|
|
|
'UPDATE "protocols" SET "visibility" = 1 WHERE "protocols"."protocol_type" = 3;'
|
2023-01-20 21:29:25 +08:00
|
|
|
)
|
|
|
|
execute(
|
2023-03-13 16:55:53 +08:00
|
|
|
'UPDATE "protocols" SET "protocol_type" = 5 ' \
|
2023-03-03 17:24:14 +08:00
|
|
|
'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)' \
|
|
|
|
');'
|
2023-01-20 21:29:25 +08:00
|
|
|
)
|
2022-12-01 02:52:36 +08:00
|
|
|
execute(
|
2023-03-24 17:58:43 +08:00
|
|
|
'UPDATE "protocols" SET "protocol_type" = 6, "published_on" = NULL ' \
|
2023-03-03 17:24:14 +08:00
|
|
|
'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' \
|
|
|
|
');'
|
2022-12-01 02:52:36 +08:00
|
|
|
)
|
|
|
|
execute(
|
2023-03-27 17:39:09 +08:00
|
|
|
'UPDATE "protocols" SET "published_on" = "updated_at", "published_by_id" = "added_by_id" ' \
|
|
|
|
'WHERE "protocols"."protocol_type" = 5;'
|
2022-12-01 02:52:36 +08:00
|
|
|
)
|
2023-03-24 17:58:43 +08:00
|
|
|
|
|
|
|
execute(
|
|
|
|
'UPDATE "protocols" SET "published_by_id" = "added_by_id" ' \
|
|
|
|
'WHERE "protocols"."protocol_type" = 5 ' \
|
2023-07-11 19:24:44 +08:00
|
|
|
'AND "protocols"."visibility" = 1 ' \
|
2023-03-24 17:58:43 +08:00
|
|
|
'AND "protocols"."published_by_id" IS NULL;'
|
|
|
|
)
|
2022-12-01 02:52:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
execute(
|
2023-03-13 16:55:53 +08:00
|
|
|
'UPDATE "protocols" SET "protocol_type" = 4 WHERE "protocols"."protocol_type" IN (5, 6, 7) AND ' \
|
2022-12-01 02:52:36 +08:00
|
|
|
'"protocols"."archived" = TRUE;'
|
|
|
|
)
|
2023-03-13 16:55:53 +08:00
|
|
|
execute(
|
|
|
|
'UPDATE "protocols" SET "protocol_type" = 3 WHERE "protocols"."protocol_type" IN (5, 6, 7) AND ' \
|
|
|
|
'"protocols"."visibility" = 1;'
|
|
|
|
)
|
|
|
|
execute(
|
|
|
|
'UPDATE "protocols" SET "protocol_type" = 2 WHERE "protocols"."protocol_type" IN (5, 6, 7) AND ' \
|
|
|
|
'"protocols"."visibility" != 1;'
|
|
|
|
)
|
2022-12-01 02:52:36 +08:00
|
|
|
change_table :protocols, bulk: true do |t|
|
2023-01-20 21:29:25 +08:00
|
|
|
t.remove_references :published_by
|
|
|
|
t.remove_references :last_modified_by
|
|
|
|
t.remove_references :previous_version
|
2023-02-13 23:18:26 +08:00
|
|
|
t.remove_references :default_public_user_role
|
2023-01-20 21:29:25 +08:00
|
|
|
t.remove :version_comment
|
2022-12-01 02:52:36 +08:00
|
|
|
t.remove :version_number
|
|
|
|
t.remove :archived
|
2023-02-13 23:18:26 +08:00
|
|
|
t.remove :visibility
|
2022-12-01 02:52:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|