From c5a116859dbfea60241bac938fb0a1a25930c6d2 Mon Sep 17 00:00:00 2001 From: Krzysztof Kotlarek Date: Thu, 18 Mar 2021 15:22:41 +1100 Subject: [PATCH] FIX: delete post action from permanently deleted posts (#12309) When Post is permanently deleted, we should delete correlated PostAction as well. --- app/models/post.rb | 2 +- ...210308010745_delete_orphan_post_actions.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20210308010745_delete_orphan_post_actions.rb diff --git a/app/models/post.rb b/app/models/post.rb index 5afec60316f..7bdc472599b 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -34,7 +34,7 @@ class Post < ActiveRecord::Base has_many :post_replies has_many :replies, through: :post_replies - has_many :post_actions + has_many :post_actions, dependent: :destroy has_many :topic_links has_many :group_mentions, dependent: :destroy diff --git a/db/migrate/20210308010745_delete_orphan_post_actions.rb b/db/migrate/20210308010745_delete_orphan_post_actions.rb new file mode 100644 index 00000000000..4abea24789e --- /dev/null +++ b/db/migrate/20210308010745_delete_orphan_post_actions.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class DeleteOrphanPostActions < ActiveRecord::Migration[6.0] + def up + sql = <<~SQL + DELETE FROM post_actions + USING post_actions pa + LEFT JOIN posts ON posts.id = pa.post_id + WHERE posts.id IS NULL + AND post_actions.id = pa.id + SQL + + execute(sql) + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end