FIX: Watched Words (+ Groups) with missing action (#26826)

This defaults the action to

- Censor if there are no "replacement"
- Replace if there's a "replacement"

It also fixes the `WatchedWordGroups` action to the action of one of their `WatchedWords`
This commit is contained in:
Régis Hanol 2024-04-30 21:36:02 +02:00 committed by GitHub
parent ddf7bbfa97
commit 7a7a214c11
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 34 additions and 0 deletions

View File

@ -0,0 +1,34 @@
# frozen_string_literal: true
class FixWatchedWordsWithoutAction < ActiveRecord::Migration[7.0]
def up
# Set "censor" action to watched words without replacement
execute <<~SQL
UPDATE watched_words
SET action = 2
WHERE action = 0
AND LENGTH(COALESCE(replacement, '')) = 0
SQL
# Set "replace" action to watched words with replacement
execute <<~SQL
UPDATE watched_words
SET action = 5
WHERE action = 0
SQL
# Update watched word groups with matching action
execute <<~SQL
UPDATE watched_word_groups
SET action = ww.action
FROM watched_words ww
WHERE ww.watched_word_group_id = watched_word_groups.id
AND ww.action != 0
AND watched_word_groups.action = 0
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end