diff options
author | Stan Hu <stanhu@gmail.com> | 2018-03-15 12:50:31 -0700 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2018-03-15 12:51:39 -0700 |
commit | 4acbc9410d2d3f5624ebf9cf8850b189524d321d (patch) | |
tree | 476bdf735bd27b7cc0ff0c837851e257d0c420e7 | |
parent | ff292d40a23e3df9b08f7b4dc715f1e35bf34de1 (diff) | |
download | gitlab-ce-4acbc9410d2d3f5624ebf9cf8850b189524d321d.tar.gz |
Cache column_exists? for application settingssh-cache-column-exists
This is most a backport of
https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/4987/diffs but includes
other columns that use column_exists? in a way that may cause unnecessary
schema loads.
-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 0dee6df525d..3cbbf8b5dfa 100644 --- a/app/models/application_setting.rb +++ b/app/models/application_setting.rb @@ -347,15 +347,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) |