From f9d628cf9b2164f658032bdf42bf8ff8790f7fc5 Mon Sep 17 00:00:00 2001
From: Devin Howard <devin@callysto.com>
Date: Sat, 13 May 2017 14:12:55 -0700
Subject: [PATCH] before destroy callback on maps to remove source_id foreign
 key relations

---
 app/models/map.rb | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/app/models/map.rb b/app/models/map.rb
index dd5e5604..76fddcd8 100644
--- a/app/models/map.rb
+++ b/app/models/map.rb
@@ -42,6 +42,7 @@ class Map < ApplicationRecord
   after_create :after_created
   after_update :after_updated
   after_save :update_deferring_topics_and_synapses, if: :permission_changed?
+  before_destroy :before_destroyed
 
   delegate :count, to: :topics, prefix: :topic # same as `def topic_count; topics.count; end`
   delegate :count, to: :synapses, prefix: :synapse
@@ -158,4 +159,10 @@ class Map < ApplicationRecord
     end
   end
   handle_asynchronously :after_updated_async
+
+  def before_destroyed
+    Map.where(source_id: id).find_each do |forked_map|
+      forked_map.update(source_id: nil)
+    end
+  end
 end