From 803f73f837f7c603b2d16436e241117044a63e34 Mon Sep 17 00:00:00 2001 From: Selase Krakani <849886+s3lase@users.noreply.github.com> Date: Wed, 18 Jan 2023 19:53:20 +0000 Subject: [PATCH] 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. --- ...activate_assignments_to_deleted_targets.rb | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 db/migrate/20230113025043_deactivate_assignments_to_deleted_targets.rb diff --git a/db/migrate/20230113025043_deactivate_assignments_to_deleted_targets.rb b/db/migrate/20230113025043_deactivate_assignments_to_deleted_targets.rb new file mode 100644 index 0000000..2e60a85 --- /dev/null +++ b/db/migrate/20230113025043_deactivate_assignments_to_deleted_targets.rb @@ -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