Commit 734d6fcd authored by Andrey Kumanyaev's avatar Andrey Kumanyaev

Perfomance updating Project activity sort

parent 41bbbb6d
......@@ -29,7 +29,7 @@ class Project < ActiveRecord::Base
attr_accessible :name, :path, :description, :default_branch, :issues_tracker,
:issues_enabled, :wall_enabled, :merge_requests_enabled, :snippets_enabled, :issues_tracker_id,
:wiki_enabled, :public, :import_url, as: [:default, :admin]
:wiki_enabled, :public, :import_url, :last_activity_at, as: [:default, :admin]
attr_accessible :namespace_id, :creator_id, as: :admin
......@@ -92,12 +92,12 @@ class Project < ActiveRecord::Base
scope :in_team, ->(team) { where("id IN (:ids)", ids: team.projects.map(&:id)) }
scope :in_namespace, ->(namespace) { where(namespace_id: namespace.id) }
scope :in_group_namespace, -> { joins(:group) }
scope :sorted_by_activity, -> { order("(SELECT max(events.created_at) FROM events WHERE events.project_id = projects.id) DESC") }
scope :sorted_by_activity, -> { order("last_activity_at DESC") }
scope :personal, ->(user) { where(namespace_id: user.namespace_id) }
scope :joined, ->(user) { where("namespace_id != ?", user.namespace_id) }
scope :public_only, -> { where(public: true) }
enumerize :issues_tracker, :in => (Gitlab.config.issues_tracker.keys).append(:gitlab), :default => :gitlab
enumerize :issues_tracker, in: (Gitlab.config.issues_tracker.keys).append(:gitlab), default: :gitlab
class << self
def abandoned
......@@ -157,8 +157,7 @@ class Project < ActiveRecord::Base
unless creator.can_create_project?
errors[:limit_reached] << ("Your own projects limit is #{creator.projects_limit}! Please contact administrator to increase it")
end
rescue => ex
errors[:base] << ex.message
rescue
errors[:base] << ("Can't check your ability to create project")
end
......@@ -191,7 +190,7 @@ class Project < ActiveRecord::Base
end
def last_activity_date
last_event.try(:created_at) || updated_at
last_activity_at
end
def project_id
......
class ProjectActivityCacheObserver < BaseObserver
observe :event
def after_create(event)
event.project.update_attribute(:last_activity_at, event.created_at) if event.project
end
end
......@@ -24,6 +24,7 @@ module Gitlab
# Activate observers that should always be running.
config.active_record.observers = :activity_observer,
:project_activity_cache_observer,
:issue_observer,
:key_observer,
:merge_request_observer,
......
class AddLastActivityColumnIntoProject < ActiveRecord::Migration
def up
add_column :projects, :last_activity_at, :datetime
add_index :projects, :last_activity_at
Project.find_each do |project|
project.update_attribute(:last_activity_at, project.last_activity_date)
end
end
def down
remove_index :projects, :last_activity_at
remove_column :projects, :last_activity_at
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130325173941) do
ActiveRecord::Schema.define(:version => 20130403003950) do
create_table "events", :force => true do |t|
t.string "target_type"
......@@ -156,9 +156,11 @@ ActiveRecord::Schema.define(:version => 20130325173941) do
t.string "issues_tracker", :default => "gitlab", :null => false
t.string "issues_tracker_id"
t.boolean "snippets_enabled", :default => true, :null => false
t.datetime "last_activity_at"
end
add_index "projects", ["creator_id"], :name => "index_projects_on_owner_id"
add_index "projects", ["last_activity_at"], :name => "index_projects_on_last_activity_at"
add_index "projects", ["namespace_id"], :name => "index_projects_on_namespace_id"
create_table "protected_branches", :force => true do |t|
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment