FIX: Ignore unique conflicts when backfilling sidebar defaults (#18785)

`insert_all!` raises an error when the insertion violates any unique
constraints which is not what we want here.

Follow-up to 1b56a55f50
This commit is contained in:
Alan Guo Xiang Tan 2022-10-28 07:47:41 +08:00 committed by GitHub
parent d99293d837
commit 4244b1c57d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -48,7 +48,7 @@ class SidebarSiteSettingsBackfiller
end
end
SidebarSectionLink.insert_all!(rows) if rows.present?
SidebarSectionLink.insert_all(rows) if rows.present?
end
end
end

View File

@ -80,6 +80,36 @@ RSpec.describe SidebarSiteSettingsBackfiller do
)
end
it 'creates the right sidebar section link records when categories are added' do
backfiller = described_class.new(
"default_sidebar_categories",
previous_value: "",
new_value: "#{category.id}|#{category2.id}|#{category3.id}"
)
expect do
backfiller.backfill!
end.to change { SidebarSectionLink.count }.by(6)
expect(SidebarSectionLink.where(linkable_type: 'Category', linkable_id: category.id).pluck(:user_id)).to contain_exactly(
user.id,
user2.id,
user3.id
)
expect(SidebarSectionLink.where(linkable_type: 'Category', linkable_id: category2.id).pluck(:user_id)).to contain_exactly(
user.id,
user2.id,
user3.id
)
expect(SidebarSectionLink.where(linkable_type: 'Category', linkable_id: category3.id).pluck(:user_id)).to contain_exactly(
user.id,
user2.id,
user3.id
)
end
it 'deletes and creates the right sidebar section link records when categories are added and removed' do
backfiller = described_class.new(
"default_sidebar_categories",