Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit e0a579e

Browse files
authored
FIX: Migration was only done for first batch (#344)
#342 was deployed and observed to only have 5000 (batch size) migrated. This is an error in migration where the ids had a gap between the batch. This PR changes the migration to just loop through all topic custom fields with each loop increasing by batch size.
1 parent 55c1eb4 commit e0a579e

2 files changed

+66
-6
lines changed

db/migrate/20250318024953_copy_solved_topic_custom_field_to_discourse_solved_solved_topics.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
class CopySolvedTopicCustomFieldToDiscourseSolvedSolvedTopics < ActiveRecord::Migration[7.2]
44
disable_ddl_transaction!
55

6-
BATCH_SIZE = 5000
6+
BATCH_SIZE = 10_000
77

88
def up
9+
max_id =
10+
DB.query_single(
11+
"SELECT MAX(id) FROM topic_custom_fields WHERE topic_custom_fields.name = 'accepted_answer_post_id'",
12+
).first
13+
return unless max_id
14+
915
last_id = 0
10-
loop do
11-
rows = DB.query(<<~SQL, last_id: last_id, batch_size: BATCH_SIZE)
16+
while last_id < max_id
17+
DB.exec(<<~SQL, last_id: last_id, batch_size: BATCH_SIZE)
1218
INSERT INTO discourse_solved_solved_topics (
1319
topic_id,
1420
answer_post_id,
@@ -33,11 +39,10 @@ def up
3339
AND ua.action_type = 15
3440
WHERE tc.name = 'accepted_answer_post_id'
3541
AND tc.id > :last_id
36-
ORDER BY tc.topic_id, ua.created_at DESC
37-
LIMIT :batch_size
42+
AND tc.id <= :last_id + :batch_size
43+
ON CONFLICT DO NOTHING
3844
SQL
3945

40-
break if rows.length == 0
4146
last_id += BATCH_SIZE
4247
end
4348
end
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# frozen_string_literal: true
2+
#
3+
class CopyRemainingSolvedTopicCustomFieldToDiscourseSolvedSolvedTopics < ActiveRecord::Migration[
4+
7.2
5+
]
6+
disable_ddl_transaction!
7+
8+
BATCH_SIZE = 5000
9+
10+
def up
11+
max_id =
12+
DB.query_single(
13+
"SELECT MAX(id) FROM topic_custom_fields WHERE topic_custom_fields.name = 'accepted_answer_post_id'",
14+
).first
15+
return unless max_id
16+
17+
last_id = 0
18+
while last_id < max_id
19+
DB.exec(<<~SQL, last_id: last_id, batch_size: BATCH_SIZE)
20+
INSERT INTO discourse_solved_solved_topics (
21+
topic_id,
22+
answer_post_id,
23+
topic_timer_id,
24+
accepter_user_id,
25+
created_at,
26+
updated_at
27+
)
28+
SELECT DISTINCT ON (tc.topic_id)
29+
tc.topic_id,
30+
CAST(tc.value AS INTEGER),
31+
CAST(tc2.value AS INTEGER),
32+
COALESCE(ua.acting_user_id, -1),
33+
tc.created_at,
34+
tc.updated_at
35+
FROM topic_custom_fields tc
36+
LEFT JOIN topic_custom_fields tc2
37+
ON tc2.topic_id = tc.topic_id
38+
AND tc2.name = 'solved_auto_close_topic_timer_id'
39+
LEFT JOIN user_actions ua
40+
ON ua.target_topic_id = tc.topic_id
41+
AND ua.action_type = 15
42+
WHERE tc.name = 'accepted_answer_post_id'
43+
AND tc.id > :last_id
44+
AND tc.id <= :last_id + :batch_size
45+
ON CONFLICT DO NOTHING
46+
SQL
47+
48+
last_id += BATCH_SIZE
49+
end
50+
end
51+
52+
def down
53+
raise ActiveRecord::IrreversibleMigration
54+
end
55+
end

0 commit comments

Comments
 (0)