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-01-20 21:29:25 +08:00
|
|
|
execute(
|
2023-03-03 17:24:14 +08:00
|
|
|
'UPDATE "protocols" SET "protocol_type" = 3, "archived" = TRUE WHERE "protocols"."protocol_type" = 4;'
|
|
|
|
)
|
|
|
|
execute(
|
|
|
|
'UPDATE "protocols" SET "visibility" = 1 WHERE "protocols"."protocol_type" = 3;'
|
2023-01-20 21:29:25 +08:00
|
|
|
)
|
|
|
|
execute(
|
2023-03-03 17:24:14 +08:00
|
|
|
'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)' \
|
|
|
|
');'
|
2023-01-20 21:29:25 +08:00
|
|
|
)
|
2022-12-01 02:52:36 +08:00
|
|
|
execute(
|
2023-03-03 17:24:14 +08:00
|
|
|
'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' \
|
|
|
|
');'
|
2022-12-01 02:52:36 +08:00
|
|
|
)
|
|
|
|
execute(
|
2023-03-03 17:24:14 +08:00
|
|
|
'UPDATE "protocols" SET "published_on" = "created_at", "published_by_id" = "added_by_id" ' \
|
|
|
|
'WHERE "protocols"."protocol_type" = 2 ' \
|
|
|
|
'AND "protocols"."published_on" IS NULL;'
|
2022-12-01 02:52:36 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
execute(
|
2023-03-03 17:24:14 +08:00
|
|
|
'UPDATE "protocols" SET "protocol_type" = 4 WHERE "protocols"."protocol_type" IN (2, 3, 4) AND ' \
|
2022-12-01 02:52:36 +08:00
|
|
|
'"protocols"."archived" = TRUE;'
|
|
|
|
)
|
|
|
|
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
|