Skip to content

Commit baf5336

Browse files
committed
Revert coc backfill migration to raw SQL
1 parent 0192a0e commit baf5336

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
class BackfillCocAgreedAt < ActiveRecord::Migration[7.2]
2+
# Raw SQL on purpose: this backfills from code_of_conduct_agreements, a table
3+
# slated for removal. Referencing the CodeOfConductAgreement model would break
4+
# this migration on replay once that model is deleted.
25
def up
3-
Participant.where(coc_agreed_at: nil).find_each do |participant|
4-
aggreement = CodeOfConductAgreement.where(participant_id: participant.id)
5-
.order(created_at: :desc).first
6-
participant.update_column(:coc_agreed_at, aggreement.created_at) if aggreement
7-
end
6+
execute(<<~SQL)
7+
UPDATE participants
8+
SET coc_agreed_at = coc.last_agreed_at
9+
FROM (
10+
SELECT participant_id, MAX(created_at) AS last_agreed_at
11+
FROM code_of_conduct_agreements
12+
GROUP BY participant_id
13+
) AS coc
14+
WHERE participants.id = coc.participant_id
15+
AND participants.coc_agreed_at IS NULL
16+
SQL
817
end
918

1019
def down
11-
Participant.update_all(coc_agreed_at: nil)
20+
# no-op: backfilled timestamps are indistinguishable from organic agreements
1221
end
1322
end

0 commit comments

Comments
 (0)