Merge pull request #74 from abf/rosa-build:73-increase-performance-autocomplete_to_project

#73: Increase performance Projects::PullRequestsController#autocomplete_to_project
This commit is contained in:
warpc 2013-04-23 22:47:53 +04:00
commit 768307366c
7 changed files with 110 additions and 70 deletions

View File

@ -115,9 +115,12 @@ class Projects::PullRequestsController < Projects::BaseController
end
def autocomplete_to_project
items = Project.accessible_by(current_ability, :membered) | @project.ancestors
term = Regexp.new(Regexp.escape params[:term].downcase)
items.select! {|e| term.match(e.name_with_owner.downcase) && e.repo.branches.count > 0}
items = []
term = params[:term].to_s.strip.downcase
[Project.accessible_by(current_ability, :membered), @project.ancestors].each do |p|
items.concat p.by_owner_and_name(term)
end
items = items.uniq{|i| i.id}.select{|e| e.repo.branches.count > 0}
render :json => json_for_autocomplete_base(items)
end
@ -129,7 +132,7 @@ class Projects::PullRequestsController < Projects::BaseController
def json_for_autocomplete_base items
items.collect do |project|
hash = {"id" => project.id.to_s, "label" => project.name_with_owner, "value" => project.name_with_owner}
hash = {:id => project.id.to_s, :label => project.name_with_owner, :value => project.name_with_owner}
hash[:get_refs_url] = project_refs_list_path(project)
hash
end

View File

@ -19,7 +19,6 @@ class Platform < ActiveRecord::Base
has_many :mass_builds
validates :description, :presence => true
validates :owner, :presence => true
validates :visibility, :presence => true, :inclusion => {:in => VISIBILITIES}
validates :name, :uniqueness => {:case_sensitive => false}, :presence => true, :format => { :with => /\A[a-zA-Z0-9_\-\.]+\z/ }
validates :distrib_type, :presence => true, :inclusion => {:in => APP_CONFIG['distr_types']}

View File

@ -28,7 +28,6 @@ class Project < ActiveRecord::Base
validates :name, :uniqueness => {:scope => [:owner_id, :owner_type], :case_sensitive => false},
:presence => true,
:format => {:with => /\A#{NAME_REGEXP}\z/, :message => I18n.t("activerecord.errors.project.uname")}
validates :owner, :presence => true
validates :maintainer_id, :presence => true, :unless => :new_record?
validates :visibility, :presence => true, :inclusion => {:in => VISIBILITIES}
validate { errors.add(:base, :can_have_less_or_equal, :count => MAX_OWN_PROJECTS) if owner.projects.size >= MAX_OWN_PROJECTS }
@ -49,6 +48,10 @@ class Project < ActiveRecord::Base
scope :search_order, order("CHAR_LENGTH(#{table_name}.name) ASC")
scope :search, lambda {|q| by_name("%#{q.to_s.strip}%")}
scope :by_name, lambda {|name| where("#{table_name}.name ILIKE ?", name) if name.present?}
scope :by_owner_and_name, lambda { |*params|
term = params.map(&:strip).join('/').downcase
where("lower(concat(owner_uname, '/', name)) ILIKE ?", "%#{term}%") if term.present?
}
scope :by_visibilities, lambda {|v| where(:visibility => v)}
scope :opened, where(:visibility => 'open')
scope :package, where(:is_package => true)
@ -66,6 +69,7 @@ class Project < ActiveRecord::Base
}
before_validation :truncate_name, :on => :create
before_save lambda { self.owner_uname = owner.uname if owner_uname.blank? || owner_id_changed? || owner_type_changed? }
before_create :set_maintainer
after_save :attach_to_personal_repository
after_update :set_new_git_head
@ -78,10 +82,8 @@ class Project < ActiveRecord::Base
class << self
def find_by_owner_and_name(owner_name, project_name)
owner = User.find_by_uname(owner_name) || Group.find_by_uname(owner_name) || User.by_uname(owner_name).first || Group.by_uname(owner_name).first and
scoped = where(:owner_id => owner.id, :owner_type => owner.class) and
scoped.find_by_name(project_name) || scoped.by_name(project_name).first
# owner.projects.find_by_name(project_name) || owner.projects.by_name(project_name).first # TODO force this work?
where(:owner_uname => owner_name, :name => project_name).first ||
by_owner_and_name(owner_name, project_name).first
end
def find_by_owner_and_name!(owner_name, project_name)
@ -89,6 +91,10 @@ class Project < ActiveRecord::Base
end
end
def name_with_owner
"#{owner_uname || owner.uname}/#{name}"
end
def to_param
name
end

View File

@ -0,0 +1,22 @@
class AddNameWithOwnerToProject < ActiveRecord::Migration
def up
add_column :projects, :owner_uname, :string
execute <<-SQL
UPDATE projects SET owner_uname = owners.uname
FROM users as owners
WHERE projects.owner_type = 'User' AND projects.owner_id = owners.id
SQL
execute <<-SQL
UPDATE projects SET owner_uname = owners.uname
FROM groups as owners
WHERE projects.owner_type = 'Group' AND projects.owner_id = owners.id
SQL
change_column :projects, :owner_uname, :string, :null => false
end
def down
remove_column :projects, :owner_uname
end
end

View File

@ -17,8 +17,8 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
t.integer "user_id", :null => false
t.string "kind"
t.text "data"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "advisories", :force => true do |t|
@ -53,8 +53,8 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
create_table "arches", :force => true do |t|
t.string "name", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "arches", ["name"], :name => "index_arches_on_name", :unique => true
@ -63,8 +63,8 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
t.integer "user_id"
t.string "provider"
t.string "uid"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "authentications", ["provider", "uid"], :name => "index_authentications_on_provider_and_uid", :unique => true
@ -75,8 +75,8 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
t.integer "level"
t.integer "status"
t.integer "build_list_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "version"
end
@ -110,8 +110,8 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
t.integer "project_id"
t.integer "arch_id"
t.datetime "notified_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "is_circle", :default => false
t.text "additional_repos"
t.string "name"
@ -150,8 +150,8 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
t.string "commentable_type"
t.integer "user_id"
t.text "body"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.decimal "commentable_id", :precision => 50, :scale => 0
t.integer "project_id"
t.text "data"
@ -178,8 +178,8 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
t.string "controller"
t.string "action"
t.text "message"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "flash_notifies", :force => true do |t|
@ -193,8 +193,8 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
create_table "groups", :force => true do |t|
t.integer "owner_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "uname"
t.integer "own_projects_count", :default => 0, :null => false
t.text "description"
@ -204,6 +204,14 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
t.datetime "avatar_updated_at"
end
create_table "hooks", :force => true do |t|
t.text "data"
t.integer "project_id"
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "issues", :force => true do |t|
t.integer "serial_id"
t.integer "project_id"
@ -211,8 +219,8 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
t.string "title"
t.text "body"
t.string "status", :default => "open"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.datetime "closed_at"
t.integer "closed_by"
@ -290,14 +298,14 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
t.string "description"
t.string "name", :null => false
t.integer "parent_platform_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "released", :default => false, :null => false
t.integer "owner_id"
t.string "owner_type"
t.string "visibility", :default => "open", :null => false
t.string "platform_type", :default => "main", :null => false
t.string "distrib_type"
t.string "distrib_type", :null => false
end
add_index "platforms", ["name"], :name => "index_platforms_on_name", :unique => true, :case_sensitive => false
@ -306,16 +314,16 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
t.integer "platform_id"
t.string "login"
t.string "password"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
create_table "product_build_lists", :force => true do |t|
t.integer "product_id"
t.integer "status", :default => 3, :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "status", :default => 2, :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "project_id"
t.string "project_version"
t.string "commit_hash"
@ -334,8 +342,8 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
create_table "products", :force => true do |t|
t.string "name", :null => false
t.integer "platform_id", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.text "description"
t.integer "project_id"
t.string "params"
@ -350,8 +358,8 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
t.string "name"
t.string "version"
t.datetime "file_mtime"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "platform_id"
end
@ -370,33 +378,34 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
create_table "project_to_repositories", :force => true do |t|
t.integer "project_id"
t.integer "repository_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "project_to_repositories", ["repository_id", "project_id"], :name => "index_project_to_repositories_on_repository_id_and_project_id", :unique => true
create_table "projects", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "owner_id"
t.string "owner_type"
t.string "visibility", :default => "open"
t.text "description"
t.string "ancestry"
t.boolean "has_issues", :default => true
t.boolean "has_wiki", :default => false
t.string "srpm_file_name"
t.string "srpm_content_type"
t.integer "srpm_file_size"
t.datetime "srpm_updated_at"
t.string "srpm_content_type"
t.boolean "has_wiki", :default => false
t.string "default_branch", :default => "master"
t.boolean "is_package", :default => true, :null => false
t.integer "average_build_time", :default => 0, :null => false
t.integer "build_count", :default => 0, :null => false
t.integer "maintainer_id"
t.boolean "publish_i686_into_x86_64", :default => false
t.string "owner_uname", :null => false
end
add_index "projects", ["owner_id", "name", "owner_type"], :name => "index_projects_on_name_and_owner_id_and_owner_type", :unique => true, :case_sensitive => false
@ -421,8 +430,8 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
t.string "token"
t.boolean "approved", :default => false
t.boolean "rejected", :default => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "interest"
t.text "more"
t.string "language"
@ -436,16 +445,16 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
t.string "actor_type"
t.integer "target_id"
t.string "target_type"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "role"
end
create_table "repositories", :force => true do |t|
t.string "description", :null => false
t.integer "platform_id", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.string "name", :null => false
t.boolean "publish_without_qa", :default => true
end
@ -459,8 +468,8 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
t.boolean "new_comment_reply", :default => true
t.boolean "new_issue", :default => true
t.boolean "issue_assign", :default => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "new_comment_commit_owner", :default => true
t.boolean "new_comment_commit_repo_owner", :default => true
t.boolean "new_comment_commit_commentor", :default => true
@ -483,8 +492,8 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
create_table "subscribes", :force => true do |t|
t.string "subscribeable_type"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "status", :default => true
t.integer "project_id"
t.decimal "subscribeable_id", :precision => 50, :scale => 0
@ -492,21 +501,18 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
create_table "users", :force => true do |t|
t.string "name"
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.text "ssh_key"
t.string "uname"
t.string "role"
t.string "language", :default => "en"
t.integer "own_projects_count", :default => 0, :null => false
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "language", :default => "en"
t.integer "own_projects_count", :default => 0, :null => false
t.text "professional_experience"
t.string "site"
t.string "company"
@ -515,11 +521,14 @@ ActiveRecord::Schema.define(:version => 20130417162427) do
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.integer "failed_attempts", :default => 0
t.integer "failed_attempts", :default => 0
t.string "unlock_token"
t.datetime "locked_at"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "authentication_token"
t.integer "build_priority", :default => 50
t.integer "build_priority", :default => 50
end
add_index "users", ["authentication_token"], :name => "index_users_on_authentication_token"

View File

@ -5,12 +5,10 @@ module Modules
extend ActiveSupport::Concern
included do
validates :owner, :presence => true
after_create lambda { relations.create :actor_id => owner.id, :actor_type => owner.class.to_s, :role => 'admin' }
end
def name_with_owner
"#{owner.respond_to?(:uname) ? owner.uname : owner.name}/#{self.name}"
end
end
end
end

View File

@ -480,8 +480,11 @@ describe Api::V1::BuildListsController do
@user = FactoryGirl.create(:user)
@group.actors.create :role => 'reader', :actor_id => @user.id, :actor_type => 'User'
old_path = @project.path
@project.owner = @owner_group
@project.save
# Move GIT repo into new folder
system "mkdir -p #{@project.path} && mv -f #{old_path}/* #{@project.path}/"
@project.relations.create :role => 'reader', :actor_id => @member_group.id, :actor_type => 'Group'
@project.relations.create :role => 'admin', :actor_id => @owner_group.id, :actor_type => 'Group'