20220613110711_migrate_custom_filters.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. # frozen_string_literal: true
  2. class MigrateCustomFilters < ActiveRecord::Migration[6.1]
  3. def up
  4. # Preserve IDs as much as possible to not confuse existing clients.
  5. # As long as this migration is irreversible, we do not have to deal with conflicts.
  6. safety_assured do
  7. execute <<-SQL.squish
  8. INSERT INTO custom_filter_keywords (id, custom_filter_id, keyword, whole_word, created_at, updated_at)
  9. SELECT id, id, phrase, whole_word, created_at, updated_at
  10. FROM custom_filters
  11. SQL
  12. end
  13. end
  14. def down
  15. # Copy back changes from custom filters guaranteed to be from the old API
  16. safety_assured do
  17. execute <<-SQL.squish
  18. UPDATE custom_filters
  19. SET phrase = custom_filter_keywords.keyword, whole_word = custom_filter_keywords.whole_word
  20. FROM custom_filter_keywords
  21. WHERE custom_filters.id = custom_filter_keywords.id AND custom_filters.id = custom_filter_keywords.custom_filter_id
  22. SQL
  23. end
  24. # Drop every keyword as we can't safely provide a 1:1 mapping
  25. safety_assured do
  26. execute <<-SQL.squish
  27. TRUNCATE custom_filter_keywords RESTART IDENTITY
  28. SQL
  29. end
  30. end
  31. end