Skip to content

Commit 23ba0be

Browse files
authored
FIX: Remove duplicates before adding index (#315)
Turns out there are one or two duplicates on some sites, so we have to delete them prior to adding the index.
1 parent 572549b commit 23ba0be

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

db/migrate/20250630040849_add_index_post_locale_post_id.rb

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
class ClearDupesAddIndexToPostLocale < ActiveRecord::Migration[7.2]
4+
disable_ddl_transaction!
5+
6+
def up
7+
execute <<~SQL
8+
DELETE FROM discourse_translator_post_locales
9+
WHERE id IN (
10+
SELECT id
11+
FROM (
12+
SELECT id,
13+
ROW_NUMBER() OVER (
14+
PARTITION BY post_id
15+
ORDER BY created_at DESC, id DESC
16+
) AS rnum
17+
FROM discourse_translator_post_locales
18+
) t
19+
WHERE t.rnum > 1
20+
)
21+
SQL
22+
23+
execute <<~SQL
24+
DROP INDEX IF EXISTS index_discourse_translator_post_locales_on_post_id
25+
SQL
26+
27+
execute <<~SQL
28+
CREATE UNIQUE INDEX CONCURRENTLY index_discourse_translator_post_locales_on_post_id
29+
ON discourse_translator_post_locales (post_id)
30+
SQL
31+
end
32+
33+
def down
34+
raise ActiveRecord::IrreversibleMigration
35+
end
36+
end

0 commit comments

Comments
 (0)