Skip to content

A platform for community discussion. Free, open, simple.

FIX: Clean required category tag groups with invalid tag_group ids (#16414)

Roman Rizzi39a6de3d73c4
diff --git a/db/migrate/20220401130745_create_category_required_tag_groups.rb b/db/migrate/20220401130745_create_category_required_tag_groups.rb
index 1d0bfbb4270..dd054a9e11b 100644
--- a/db/migrate/20220401130745_create_category_required_tag_groups.rb
+++ b/db/migrate/20220401130745_create_category_required_tag_groups.rb
@@ -17,7 +17,8 @@ class CreateCategoryRequiredTagGroups < ActiveRecord::Migration[6.1]
(category_id, tag_group_id, min_count, updated_at, created_at)
SELECT c.id, c.required_tag_group_id, c.min_tags_from_required_group, NOW(), NOW()
FROM categories c
- WHERE c.required_tag_group_id IS NOT NULL
+ INNER JOIN tag_groups tg ON tg.id = c.required_tag_group_id
+ WHERE tg.id IS NOT NULL
SQL
end
diff --git a/db/post_migrate/20220407195246_remove_category_required_tag_groups_without_tag_groups.rb b/db/post_migrate/20220407195246_remove_category_required_tag_groups_without_tag_groups.rb
new file mode 100644
index 00000000000..e8cd6388e39
--- /dev/null
+++ b/db/post_migrate/20220407195246_remove_category_required_tag_groups_without_tag_groups.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+class RemoveCategoryRequiredTagGroupsWithoutTagGroups < ActiveRecord::Migration[6.1]
+ def up
+ execute <<~SQL
+ DELETE FROM category_required_tag_groups
+ WHERE id IN (
+ SELECT crtg.id
+ FROM category_required_tag_groups crtg
+ LEFT OUTER JOIN tag_groups tg ON crtg.tag_group_id = tg.id
+ WHERE tg.id IS NULL
+ )
+ SQL
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end