From 0e4c899307babe70d2431cf194bca6017072ca3a Mon Sep 17 00:00:00 2001
From: Benjamin Neff <benjamin@coding4coffee.ch>
Date: Sat, 20 Aug 2016 20:37:16 +0200
Subject: [PATCH] Remove started sharing notifications where the contact
 doesn't exist

Before #6873 we deleted contacts when someone blocks a person, but we
didn't drop the notification for the started sharing event. In #6864
we try to get the contact for the notification, which is not there
anymore.

So we need to remove the notifications for the contacts that don't exit
anymore.
---
 ...e_started_sharing_notifications_without_contact.rb | 11 +++++++++++
 db/schema.rb                                          |  2 +-
 2 files changed, 12 insertions(+), 1 deletion(-)
 create mode 100644 db/migrate/20160820181334_remove_started_sharing_notifications_without_contact.rb

diff --git a/db/migrate/20160820181334_remove_started_sharing_notifications_without_contact.rb b/db/migrate/20160820181334_remove_started_sharing_notifications_without_contact.rb
new file mode 100644
index 0000000000..0d9d6c0bb8
--- /dev/null
+++ b/db/migrate/20160820181334_remove_started_sharing_notifications_without_contact.rb
@@ -0,0 +1,11 @@
+class RemoveStartedSharingNotificationsWithoutContact < ActiveRecord::Migration
+  class Notification < ActiveRecord::Base
+  end
+
+  def up
+    Notification.where(type: "Notifications::StartedSharing", target_type: "Person")
+                .joins("INNER JOIN people ON people.id = notifications.target_id")
+                .joins("LEFT OUTER JOIN contacts ON contacts.person_id = people.id")
+                .delete_all("contacts.id IS NULL")
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index d78733848f..334178c497 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20160813115514) do
+ActiveRecord::Schema.define(version: 20160820181334) do
 
   create_table "account_deletions", force: :cascade do |t|
     t.string   "diaspora_handle", limit: 255
-- 
GitLab