scinote-web/db/migrate/20230227131215_add_linked_at_to_table.rb
ajugo b24c823597
Add linked_at to the Protocols [SCI-7964] (#5042)
* Add linked_at to the Protocols [SCI-7964]

* Add activity subject_type constrain for migration query [SCI-7964]
2023-03-01 11:33:32 +01:00

22 lines
716 B
Ruby

# frozen_string_literal: true
class AddLinkedAtToTable < ActiveRecord::Migration[6.1]
def up
add_column :protocols, :linked_at, :datetime
execute(
'UPDATE protocols
SET linked_at = act.activities_created_at
FROM (SELECT MAX(activities.created_at) AS activities_created_at, MAX(activities.subject_id) AS subject_id
FROM protocols
LEFT JOIN activities ON activities.subject_id = protocols.id AND activities.subject_type=\'Protocol\'
WHERE protocols.protocol_type = 1 AND activities.type_of = 46
GROUP BY protocols.id) as act
WHERE id = act.subject_id'
)
end
def down
remove_column :protocols, :linked_at
end
end