diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2018-05-17 21:20:15 +0200 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2018-05-17 21:23:42 +0200 |
commit | 63c58a6dd0d8a4b3db172ffe6d0e32e127cdabd1 (patch) | |
tree | 0f97dd7a71248c75528ea814c1814b28f18d29c8 | |
parent | 3a402fc7fc1dd0b0397f8cdd8ed73336ac409281 (diff) | |
download | gitlab-ce-memoize-database-version.tar.gz |
Memoize Gitlab::Database.versionmemoize-database-version
This removes the need for running a database query every time we want to
check the database version.
-rw-r--r-- | changelogs/unreleased/memoize-database-version.yml | 5 | ||||
-rw-r--r-- | lib/gitlab/database.rb | 2 | ||||
-rw-r--r-- | spec/lib/gitlab/database_spec.rb | 14 |
3 files changed, 20 insertions, 1 deletions
diff --git a/changelogs/unreleased/memoize-database-version.yml b/changelogs/unreleased/memoize-database-version.yml new file mode 100644 index 00000000000..575348a53a1 --- /dev/null +++ b/changelogs/unreleased/memoize-database-version.yml @@ -0,0 +1,5 @@ +--- +title: Memoize Gitlab::Database.version +merge_request: +author: +type: performance diff --git a/lib/gitlab/database.rb b/lib/gitlab/database.rb index 76501dd50e8..d49d055c3f2 100644 --- a/lib/gitlab/database.rb +++ b/lib/gitlab/database.rb @@ -43,7 +43,7 @@ module Gitlab end def self.version - database_version.match(/\A(?:PostgreSQL |)([^\s]+).*\z/)[1] + @version ||= database_version.match(/\A(?:PostgreSQL |)([^\s]+).*\z/)[1] end def self.join_lateral_supported? diff --git a/spec/lib/gitlab/database_spec.rb b/spec/lib/gitlab/database_spec.rb index 1fe1d3926ad..8ac36ae8bab 100644 --- a/spec/lib/gitlab/database_spec.rb +++ b/spec/lib/gitlab/database_spec.rb @@ -32,6 +32,12 @@ describe Gitlab::Database do end describe '.version' do + around do |example| + described_class.instance_variable_set(:@version, nil) + example.run + described_class.instance_variable_set(:@version, nil) + end + context "on mysql" do it "extracts the version number" do allow(described_class).to receive(:database_version) @@ -49,6 +55,14 @@ describe Gitlab::Database do expect(described_class.version).to eq '9.4.4' end end + + it 'memoizes the result' do + count = ActiveRecord::QueryRecorder + .new { 2.times { described_class.version } } + .count + + expect(count).to eq(1) + end end describe '.join_lateral_supported?' do |