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