A platform for community discussion. Free, open, simple.
FIX: Delete associated channel upon category deletion
Loïc Guitaut01392ab90c97
diff --git a/plugins/chat/db/post_migrate/20221117142910_delete_orphaned_channels.rb b/plugins/chat/db/post_migrate/20221117142910_delete_orphaned_channels.rbnew file mode 100644index 00000000000..44f9d9751b5--- /dev/null+++ b/plugins/chat/db/post_migrate/20221117142910_delete_orphaned_channels.rb@@ -0,0 +1,9 @@+# frozen_string_literal: true++class DeleteOrphanedChannels < ActiveRecord::Migration[7.0]+ def up+ DB.exec(+ "DELETE FROM chat_channels WHERE chatable_type = 'Category' AND type = 'CategoryChannel' AND NOT EXISTS (SELECT * FROM categories WHERE categories.id = chat_channels.chatable_id)",+ )+ end+enddiff --git a/plugins/chat/lib/extensions/category_extension.rb b/plugins/chat/lib/extensions/category_extension.rbindex a424ac38106..d12ce387645 100644--- a/plugins/chat/lib/extensions/category_extension.rb+++ b/plugins/chat/lib/extensions/category_extension.rb@@ -5,7 +5,7 @@ module Chat::CategoryExtensioninclude Chatable- prepended { has_one :category_channel, as: :chatable }+ prepended { has_one :category_channel, as: :chatable, dependent: :destroy }def cannot_delete_reasonreturn I18n.t("category.cannot_delete.has_chat_channels") if category_channeldiff --git a/plugins/chat/spec/jobs/regular/auto_manage_channel_memberships_spec.rb b/plugins/chat/spec/jobs/regular/auto_manage_channel_memberships_spec.rbindex b89e7f2c1f4..1ea5470c7f9 100644--- a/plugins/chat/spec/jobs/regular/auto_manage_channel_memberships_spec.rb+++ b/plugins/chat/spec/jobs/regular/auto_manage_channel_memberships_spec.rb@@ -103,9 +103,13 @@ describe Jobs::AutoManageChannelMemberships doendcontext "when chatable doesn’t exist anymore" do- before do- channel.chatable.destroy!- channel.reload+ let(:channel) do+ Fabricate(+ :category_channel,+ auto_join_users: true,+ chatable_type: "Category",+ chatable_id: -1,+ )endit "does nothing" dodiff --git a/plugins/chat/spec/models/category_spec.rb b/plugins/chat/spec/models/category_spec.rbindex 5a68f6449b2..bf16ff3e618 100644--- a/plugins/chat/spec/models/category_spec.rb+++ b/plugins/chat/spec/models/category_spec.rb@@ -8,7 +8,7 @@ RSpec.describe Category dolet(:channel_class) { CategoryChannel }end- it { is_expected.to have_one(:category_channel) }+ it { is_expected.to have_one(:category_channel).dependent(:destroy) }describe "#cannot_delete_reason" dosubject(:reason) { category.cannot_delete_reason }diff --git a/plugins/chat/spec/requests/categories_controller_spec.rb b/plugins/chat/spec/requests/categories_controller_spec.rbindex 798396d640c..431ded15610 100644--- a/plugins/chat/spec/requests/categories_controller_spec.rb+++ b/plugins/chat/spec/requests/categories_controller_spec.rb@@ -24,6 +24,10 @@ RSpec.describe CategoriesController doit "deletes the category" doexpect { destroy_category }.to change { Category.count }.by(-1)end++ it "deletes the associated channel" do+ expect { destroy_category }.to change { CategoryChannel.count }.by(-1)+ endendcontext "when channel has messages" do