From 7a7a214c11c57fce57e1a068c34f13da1d8c76c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Tue, 30 Apr 2024 21:36:02 +0200 Subject: [PATCH] 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` --- ...185434_fix_watched_words_without_action.rb | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 db/migrate/20240430185434_fix_watched_words_without_action.rb diff --git a/db/migrate/20240430185434_fix_watched_words_without_action.rb b/db/migrate/20240430185434_fix_watched_words_without_action.rb new file mode 100644 index 00000000000..1664ce0caff --- /dev/null +++ b/db/migrate/20240430185434_fix_watched_words_without_action.rb @@ -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