diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2018-03-16 12:36:28 +0000 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2018-03-19 12:36:37 -0500 |
commit | 4cfe7682bdf50f41404d130a0e3e63389ed85809 (patch) | |
tree | 99ba6ebd87d7f2a3900012fc9903c6b5de225694 | |
parent | fb4eb55540a91ff86972909d8b1db95b9ad105e1 (diff) | |
download | gitlab-ce-4cfe7682bdf50f41404d130a0e3e63389ed85809.tar.gz |
Merge branch 'sh-cache-column-exists' into 'master'
Cache column_exists? for application settings
See merge request gitlab-org/gitlab-ce!17780
-rw-r--r-- | app/models/application_setting.rb | 6 | ||||
-rw-r--r-- | changelogs/unreleased/sh-cache-column-exists.yml | 5 | ||||
-rw-r--r-- | lib/gitlab/database.rb | 4 | ||||
-rw-r--r-- | spec/lib/gitlab/database_spec.rb | 11 |
4 files changed, 23 insertions, 3 deletions
diff --git a/app/models/application_setting.rb b/app/models/application_setting.rb index 4281d7d267f..862933bf127 100644 --- a/app/models/application_setting.rb +++ b/app/models/application_setting.rb @@ -348,15 +348,15 @@ class ApplicationSetting < ActiveRecord::Base end def home_page_url_column_exists? - ActiveRecord::Base.connection.column_exists?(:application_settings, :home_page_url) + ::Gitlab::Database.cached_column_exists?(:application_settings, :home_page_url) end def help_page_support_url_column_exists? - ActiveRecord::Base.connection.column_exists?(:application_settings, :help_page_support_url) + ::Gitlab::Database.cached_column_exists?(:application_settings, :help_page_support_url) end def sidekiq_throttling_column_exists? - ActiveRecord::Base.connection.column_exists?(:application_settings, :sidekiq_throttling_enabled) + ::Gitlab::Database.cached_column_exists?(:application_settings, :sidekiq_throttling_enabled) end def domain_whitelist_raw diff --git a/changelogs/unreleased/sh-cache-column-exists.yml b/changelogs/unreleased/sh-cache-column-exists.yml new file mode 100644 index 00000000000..8bc648f2b32 --- /dev/null +++ b/changelogs/unreleased/sh-cache-column-exists.yml @@ -0,0 +1,5 @@ +--- +title: Cache column_exists? for application settings +merge_request: +author: +type: performance diff --git a/lib/gitlab/database.rb b/lib/gitlab/database.rb index e51794fef99..d4fc69cb173 100644 --- a/lib/gitlab/database.rb +++ b/lib/gitlab/database.rb @@ -183,6 +183,10 @@ module Gitlab ActiveRecord::Base.connection end + def self.cached_column_exists?(table_name, column_name) + connection.schema_cache.columns_hash(table_name).has_key?(column_name.to_s) + end + private_class_method :connection def self.database_version diff --git a/spec/lib/gitlab/database_spec.rb b/spec/lib/gitlab/database_spec.rb index b2f13fae73f..689bbbfaa8d 100644 --- a/spec/lib/gitlab/database_spec.rb +++ b/spec/lib/gitlab/database_spec.rb @@ -287,6 +287,17 @@ describe Gitlab::Database do end end + describe '.cached_column_exists?' do + it 'only retrieves data once' do + expect(ActiveRecord::Base.connection).to receive(:columns).once.and_call_original + + 2.times do + expect(described_class.cached_column_exists?(:projects, :id)).to be_truthy + expect(described_class.cached_column_exists?(:projects, :bogus_column)).to be_falsey + end + end + end + describe '#true_value' do it 'returns correct value for PostgreSQL' do expect(described_class).to receive(:postgresql?).and_return(true) |