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

The category table's required_tag_group_id contained references to deleted tag groups, which we copied to the new table. The new serializer tries to get the associated tag group name but fails because the tag group is nil.

This PR adds an inner join in the original migration to make sure tag groups still exist and adds a new post-migration to fix already migrated sites.
This commit is contained in:
Roman Rizzi 2022-04-07 18:02:11 -03:00 committed by GitHub
parent fdd4c91847
commit 39a6de3d73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -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

View File

@ -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