mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 17:57:24 +08:00
FIX: Delete reviewables associated to posts automatically
Currently we don’t have an association between reviewables and posts. This sometimes leads to inconsistencies in the DB as a post can have been deleted but an associated reviewable is still present. This patch addresses this issue simply by adding a new association to the `Post` model and by using the `dependent: :destroy` option.
This commit is contained in:
parent
82182ec0c7
commit
ec2ed5b7f6
|
@ -61,6 +61,7 @@ class Post < ActiveRecord::Base
|
|||
belongs_to :image_upload, class_name: "Upload"
|
||||
|
||||
has_many :post_hotlinked_media, dependent: :destroy, class_name: "PostHotlinkedMedia"
|
||||
has_many :reviewables, as: :target, dependent: :destroy
|
||||
|
||||
validates_with PostValidator, unless: :skip_validation
|
||||
|
||||
|
|
|
@ -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
|
||||
end
|
|
@ -3,9 +3,11 @@
|
|||
RSpec.describe Post do
|
||||
fab!(: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" do
|
||||
context "when verifying enum sequence" do
|
||||
|
|
Loading…
Reference in New Issue
Block a user