Skip to content
Snippets Groups Projects

Use a separate table for storing push events

Merged Yorick Peterse requested to merge split-events-into-push-events into master
All threads resolved!
Compare and Show latest version
17 files
+ 543
77
Compare changes
  • Side-by-side
  • Inline
Files
17
+ 56
3
@@ -48,6 +48,7 @@ class Event < ActiveRecord::Base
@@ -48,6 +48,7 @@ class Event < ActiveRecord::Base
belongs_to :author, class_name: "User"
belongs_to :author, class_name: "User"
belongs_to :project
belongs_to :project
belongs_to :target, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
belongs_to :target, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
 
has_one :push_event_payload, foreign_key: :event_id
# For Hash only
# For Hash only
serialize :data # rubocop:disable Cop/ActiveRecordSerialize
serialize :data # rubocop:disable Cop/ActiveRecordSerialize
@@ -55,19 +56,51 @@ class Event < ActiveRecord::Base
@@ -55,19 +56,51 @@ class Event < ActiveRecord::Base
# Callbacks
# Callbacks
after_create :reset_project_activity
after_create :reset_project_activity
after_create :set_last_repository_updated_at, if: :push?
after_create :set_last_repository_updated_at, if: :push?
 
after_create :replicate_event_for_push_events_migration
# Scopes
# Scopes
scope :recent, -> { reorder(id: :desc) }
scope :recent, -> { reorder(id: :desc) }
scope :code_push, -> { where(action: PUSHED) }
scope :code_push, -> { where(action: PUSHED) }
scope :in_projects, ->(projects) do
scope :in_projects, -> (projects) do
where(project_id: projects.pluck(:id)).recent
sub_query = projects
 
.except(:order)
 
.select(1)
 
.where('projects.id = events.project_id')
 
 
where('EXISTS (?)', sub_query).recent
 
end
 
 
scope :with_associations, -> do
 
# We're using preload for "push_event_payload" as otherwise the association
 
# is not always available (depending on the query being built).
 
includes(:author, :project, project: :namespace)
 
.preload(:target, :push_event_payload)
end
end
scope :with_associations, -> { includes(:author, :project, project: :namespace).preload(:target) }
scope :for_milestone_id, ->(milestone_id) { where(target_type: "Milestone", target_id: milestone_id) }
scope :for_milestone_id, ->(milestone_id) { where(target_type: "Milestone", target_id: milestone_id) }
 
self.inheritance_column = 'action'
 
class << self
class << self
 
def find_sti_class(action)
 
if action.to_i == PUSHED
 
PushEvent
 
else
 
Event
 
end
 
end
 
 
def subclass_from_attributes(attrs)
 
# Without this Rails will keep calling this method on the returned class,
 
# resulting in an infinite loop.
 
return unless self == Event
 
 
action = attrs.with_indifferent_access[inheritance_column].to_i
 
 
PushEvent if action == PUSHED
 
end
 
# Update Gitlab::ContributionsCalendar#activity_dates if this changes
# Update Gitlab::ContributionsCalendar#activity_dates if this changes
def contributions
def contributions
where("action = ? OR (target_type IN (?) AND action IN (?)) OR (target_type = ? AND action = ?)",
where("action = ? OR (target_type IN (?) AND action IN (?)) OR (target_type = ? AND action = ?)",
@@ -290,6 +323,16 @@ class Event < ActiveRecord::Base
@@ -290,6 +323,16 @@ class Event < ActiveRecord::Base
@commits ||= (data[:commits] || []).reverse
@commits ||= (data[:commits] || []).reverse
end
end
 
def commit_title
 
commit = commits.last
 
 
commit[:message] if commit
 
end
 
 
def commit_id
 
commit_to || commit_from
 
end
 
def commits_count
def commits_count
data[:total_commits_count] || commits.count || 0
data[:total_commits_count] || commits.count || 0
end
end
@@ -385,6 +428,16 @@ class Event < ActiveRecord::Base
@@ -385,6 +428,16 @@ class Event < ActiveRecord::Base
user ? author_id == user.id : false
user ? author_id == user.id : false
end
end
 
# We're manually replicating data into the new table since database triggers
 
# are not dumped to db/schema.rb. This could mean that a new installation
 
# would not have the triggers in place, thus losing events data in GitLab
 
# 10.0.
 
def replicate_event_for_push_events_migration
 
new_attributes = attributes.with_indifferent_access.except(:title, :data)
 
 
EventForMigration.create!(new_attributes)
 
end
 
private
private
def recent_update?
def recent_update?
Loading