Skip to content

Event default scope adds an unnecessary WHERE author_is IS NOT NULL condition

Looking at our production database the author_id column is always set, thus the filter WHERE author_id IS NOT NULL is redundant and can even result in less ideal query plans. We should:

  1. Add validates :author_id, presence: true to Event (don't use validates :author since this will do a query which is redundant as this should be taken care of using a foreign key)
  2. Add a foreign key on events.author_id pointing to users.id with a cascading delete
  3. Remove the WHERE from the default scope

See https://gitlab.com/gitlab-org/gitlab-ce/issues/31806 for more info.