Commit 13e74543 authored by Eugene Howe's avatar Eugene Howe

speed up ExternalWikiService#get_project_wiki_path

* This method previously iterated over all services in a project.  Now it
will directly query the ExternalWikiService for the project and filter
by active state.
* The presence of an external wiki is also cached
* When an external wiki is added or removed, the cached value is updated
parent 61e7453e
......@@ -4,6 +4,7 @@ v 8.11.0 (unreleased)
v 8.10.0 (unreleased)
- Fix profile activity heatmap to show correct day name (eanplatter)
- Speed up ExternalWikiHelper#get_project_wiki_path
- Expose {should,force}_remove_source_branch (Ben Boeckel)
- Disable PostgreSQL statement timeout during migrations
- Fix projects dropdown loading performance with a simplified api cal. !5113 (tiagonbotelho)
......
module ExternalWikiHelper
def get_project_wiki_path(project)
external_wiki_service = project.services.
find { |service| service.to_param == 'external_wiki' }
if external_wiki_service.present? && external_wiki_service.active?
external_wiki_service = project.external_wiki
if external_wiki_service
external_wiki_service.properties['external_wiki_url']
else
namespace_project_wiki_path(project.namespace, project, :home)
......
......@@ -650,6 +650,22 @@ class Project < ActiveRecord::Base
update_column(:has_external_issue_tracker, services.external_issue_trackers.any?)
end
def external_wiki
if has_external_wiki.nil?
cache_has_external_wiki # Populate
end
if has_external_wiki
@external_wiki ||= services.external_wikis.first
else
nil
end
end
def cache_has_external_wiki
update_column(:has_external_wiki, services.external_wikis.any?)
end
def build_missing_services
services_templates = Service.where(template: true)
......
......@@ -17,6 +17,7 @@ class Service < ActiveRecord::Base
after_commit :reset_updated_properties
after_commit :cache_project_has_external_issue_tracker
after_commit :cache_project_has_external_wiki
belongs_to :project, inverse_of: :services
has_one :service_hook
......@@ -25,6 +26,7 @@ class Service < ActiveRecord::Base
scope :visible, -> { where.not(type: ['GitlabIssueTrackerService', 'GitlabCiService']) }
scope :issue_trackers, -> { where(category: 'issue_tracker') }
scope :external_wikis, -> { where(type: 'ExternalWikiService') }
scope :active, -> { where(active: true) }
scope :without_defaults, -> { where(default: false) }
......@@ -212,4 +214,10 @@ class Service < ActiveRecord::Base
project.cache_has_external_issue_tracker
end
end
def cache_project_has_external_wiki
if project && !project.destroyed?
project.cache_has_external_wiki
end
end
end
class AddHasExternalWikiToProjects < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
def change
add_column :projects, :has_external_wiki, :boolean
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160716115710) do
ActiveRecord::Schema.define(version: 20160718153603) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -842,6 +842,7 @@ ActiveRecord::Schema.define(version: 20160716115710) do
t.boolean "only_allow_merge_if_build_succeeds", default: false, null: false
t.boolean "has_external_issue_tracker"
t.string "repository_storage", default: "default", null: false
t.boolean "has_external_wiki"
end
add_index "projects", ["builds_enabled", "shared_runners_enabled"], name: "index_projects_on_builds_enabled_and_shared_runners_enabled", using: :btree
......
......@@ -458,6 +458,47 @@ describe Project, models: true do
end
end
describe "#cache_has_external_wiki" do
let(:project) { create(:project) }
it "stores true if there is an external wiki" do
services = double(:service, external_wikis: [ExternalWikiService.new])
expect(project).to receive(:services).and_return(services)
expect do
project.cache_has_external_wiki
end.to change { project.has_external_wiki }.to(true)
end
it "stores false if there is no external wiki" do
services = double(:service, external_wikis: [])
expect(project).to receive(:services).and_return(services)
expect do
project.cache_has_external_wiki
end.to change { project.has_external_wiki }.to(false)
end
it "changes to true if an external wiki service is created later" do
expect do
project.cache_has_external_wiki
end.to change { project.has_external_wiki }.to(false)
expect do
create(:service, type: "ExternalWikiService", project: project)
end.to change { project.has_external_wiki }.to(true)
end
it "changes to false if an external wiki service is destroyed later" do
service = create(:service, type: "ExternalWikiService", project: project)
expect(project.has_external_wiki).to be_truthy
expect do
service.destroy
end.to change { project.has_external_wiki }.to(false)
end
end
describe '#open_branches' do
let(:project) { create(:project) }
......
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