FIX: Deactivate active assignments attached to deleted targets (#428)

Before the implementation of the `post_destroyed` event handler in
the plugin, soft deleting assigned posts/topics did not deactivate the
assignment.
This left sites which assigned and deleted posts/topics before the
introduction of the event handler with 'orphaned' assignments in the UI.

This change deactivates these orphaned assignments.
This commit is contained in:
Selase Krakani 2023-01-18 19:53:20 +00:00 committed by GitHub
parent 02b9d9423e
commit 803f73f837
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1,29 @@
# frozen_string_literal: true
class DeactivateAssignmentsToDeletedTargets < ActiveRecord::Migration[7.0]
def up
execute <<~SQL
UPDATE assignments
SET active = false
FROM posts
WHERE posts.id = assignments.target_id
AND assignments.target_type = 'Post'
AND posts.deleted_at IS NOT NULL
AND assignments.active = true
SQL
execute <<~SQL
UPDATE assignments
SET active = false
FROM topics
WHERE topics.id = assignments.target_id
AND assignments.target_type = 'Topic'
AND topics.deleted_at IS NOT NULL
AND assignments.active = true
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end