mirror of
https://github.com/discourse/discourse.git
synced 2024-11-25 09:42:07 +08:00
DEV: Delete upload references upon deleting draft (#22851)
We currently are accumulating orphaned upload references whenever drafts are deleted. This change deals with future cases by adding a dependent strategy of delete_all on the Draft#upload_references association. (We don't really need destroy strategy here, since UploadReference is a simple data bag and there are no validations or callbacks on the model.) It deals with existing cases through a migration that deletes all existing, orphaned draft upload references.
This commit is contained in:
parent
f79f43ddf9
commit
c4d0bbce62
|
@ -7,6 +7,8 @@ class Draft < ActiveRecord::Base
|
|||
|
||||
belongs_to :user
|
||||
|
||||
has_many :upload_references, as: :target, dependent: :delete_all
|
||||
|
||||
after_commit :update_draft_count, on: %i[create destroy]
|
||||
|
||||
class OutOfSequence < StandardError
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class DeleteOrphanedDraftUploadReferences < ActiveRecord::Migration[7.0]
|
||||
def up
|
||||
execute <<~SQL
|
||||
DELETE
|
||||
FROM
|
||||
"upload_references"
|
||||
WHERE
|
||||
"upload_references"."target_type" = 'Draft' AND
|
||||
"upload_references"."target_id" NOT IN (
|
||||
SELECT "drafts"."id" FROM "drafts"
|
||||
)
|
||||
SQL
|
||||
end
|
||||
|
||||
def down
|
||||
raise ActiveRecord::IrreversibleMigration
|
||||
end
|
||||
end
|
|
@ -5,6 +5,8 @@ RSpec.describe Draft do
|
|||
|
||||
fab!(:post) { Fabricate(:post) }
|
||||
|
||||
it { is_expected.to have_many(:upload_references).dependent(:delete_all) }
|
||||
|
||||
describe "system user" do
|
||||
it "can not set drafts" do
|
||||
# fake a sequence
|
||||
|
|
Loading…
Reference in New Issue
Block a user