A platform for community discussion. Free, open, simple.
FIX: Delete reviewables associated to posts automatically
Loïc Guitautec2ed5b7f640
diff --git a/app/models/post.rb b/app/models/post.rbindex 7aa9155264a..736295205cb 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -61,6 +61,7 @@ class Post < ActiveRecord::Basebelongs_to :image_upload, class_name: "Upload"has_many :post_hotlinked_media, dependent: :destroy, class_name: "PostHotlinkedMedia"+ has_many :reviewables, as: :target, dependent: :destroyvalidates_with PostValidator, unless: :skip_validationdiff --git a/db/post_migrate/20230119091939_drop_orphaned_reviewable_flagged_posts.rb b/db/post_migrate/20230119091939_drop_orphaned_reviewable_flagged_posts.rbnew file mode 100644index 00000000000..3efe3c06882--- /dev/null+++ b/db/post_migrate/20230119091939_drop_orphaned_reviewable_flagged_posts.rb@@ -0,0 +1,17 @@+# frozen_string_literal: true++class DropOrphanedReviewableFlaggedPosts < ActiveRecord::Migration[7.0]+ def up+ DB.exec(<<~SQL)+ DELETE FROM reviewables+ WHERE reviewables.type = 'ReviewableFlaggedPost'+ AND reviewables.status = 0+ AND reviewables.target_type = 'Post'+ AND NOT EXISTS(SELECT 1 FROM posts WHERE posts.id = reviewables.target_id)+ SQL+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end+enddiff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rbindex deb5e637f9e..7fdb40fbaa1 100644--- a/spec/models/post_spec.rb+++ b/spec/models/post_spec.rb@@ -3,9 +3,11 @@RSpec.describe Post dofab!(:coding_horror) { Fabricate(:coding_horror) }+ let(:upload_path) { Discourse.store.upload_path }+before { Oneboxer.stubs :onebox }- let(:upload_path) { Discourse.store.upload_path }+ it { is_expected.to have_many(:reviewables).dependent(:destroy) }describe "#hidden_reasons" docontext "when verifying enum sequence" do