From 9a3e4b8d1c197063bb832247657c8026f8ca307e Mon Sep 17 00:00:00 2001
From: Yorick Peterse <yorickpeterse@gmail.com>
Date: Wed, 20 Sep 2017 13:36:18 +0200
Subject: Remove redundant WHERE from event queries

The default scope in Event would add a "WHERE author_id IS NOT NULL"
clause to every query. Now that "events.author_id" has a NOT NULL clause
set this filter is redundant and we can safely remove it.

In this commit we also add a validation on events.author_id (previously
only defined in PushEvent) just in case something tries to create data
without an author ID. This way we can present a nicer error message
compared to PostgreSQL's foreign key error messages.

Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/38129
---
 app/models/event.rb | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

(limited to 'app/models/event.rb')

diff --git a/app/models/event.rb b/app/models/event.rb
index 0b1f053a7e6..0997b056c6a 100644
--- a/app/models/event.rb
+++ b/app/models/event.rb
@@ -1,7 +1,7 @@
 class Event < ActiveRecord::Base
   include Sortable
   include IgnorableColumn
-  default_scope { reorder(nil).where.not(author_id: nil) }
+  default_scope { reorder(nil) }
 
   CREATED   = 1
   UPDATED   = 2
@@ -77,6 +77,12 @@ class Event < ActiveRecord::Base
 
   scope :for_milestone_id, ->(milestone_id) { where(target_type: "Milestone", target_id: milestone_id) }
 
+  # Authors are required as they're used to display who pushed data.
+  #
+  # We're just validating the presence of the ID here as foreign key constraints
+  # should ensure the ID points to a valid user.
+  validates :author_id, presence: true
+
   self.inheritance_column = 'action'
 
   # "data" will be removed in 10.0 but it may be possible that JOINs happen that
-- 
cgit v1.2.1