FIX: deleted misconfigured embeddable hosts (#19833)

When EmbeddableHost is configured for a specific category and that category is deleted, then EmbeddableHost should be deleted as well.

In addition, migration was added to fix existing data.
This commit is contained in:
Krzysztof Kotlarek 2023-01-20 13:29:49 +11:00 committed by GitHub
parent f122f24b35
commit f409e977a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 0 deletions

View File

@ -130,6 +130,7 @@ class Category < ActiveRecord::Base
has_many :category_required_tag_groups, -> { order(order: :asc) }, dependent: :destroy
has_many :sidebar_section_links, as: :linkable, dependent: :delete_all
has_many :embeddable_hosts, dependent: :destroy
belongs_to :reviewable_by_group, class_name: "Group"

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
class DeleteMisconfiguredEmbeddableHosts < ActiveRecord::Migration[7.0]
def up
execute(<<~SQL)
DELETE FROM embeddable_hosts eh1
WHERE eh1.id IN (
SELECT eh2.id FROM embeddable_hosts eh2
LEFT JOIN categories ON categories.id = eh2.category_id
WHERE eh2.category_id IS NOT NULL
AND categories.id IS NULL
)
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end

View File

@ -577,6 +577,12 @@ RSpec.describe Category do
expect(SiteSetting.shared_drafts_category).to be_blank
end
it "deletes related embeddable host" do
embeddable_host = Fabricate(:embeddable_host, category: @category)
@category.destroy!
expect { embeddable_host.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
it "triggers a extensibility event" do
event = DiscourseEvent.track(:category_destroyed) { @category.destroy }