Memoize Gitlab::Database.version
This removes the need for running a database query every time we want to check the database version.
This commit is contained in:
parent
3a402fc7fc
commit
63c58a6dd0
3 changed files with 20 additions and 1 deletions
5
changelogs/unreleased/memoize-database-version.yml
Normal file
5
changelogs/unreleased/memoize-database-version.yml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
title: Memoize Gitlab::Database.version
|
||||||
|
merge_request:
|
||||||
|
author:
|
||||||
|
type: performance
|
|
@ -43,7 +43,7 @@ module Gitlab
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.version
|
def self.version
|
||||||
database_version.match(/\A(?:PostgreSQL |)([^\s]+).*\z/)[1]
|
@version ||= database_version.match(/\A(?:PostgreSQL |)([^\s]+).*\z/)[1]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.join_lateral_supported?
|
def self.join_lateral_supported?
|
||||||
|
|
|
@ -32,6 +32,12 @@ describe Gitlab::Database do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '.version' do
|
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
|
context "on mysql" do
|
||||||
it "extracts the version number" do
|
it "extracts the version number" do
|
||||||
allow(described_class).to receive(:database_version)
|
allow(described_class).to receive(:database_version)
|
||||||
|
@ -49,6 +55,14 @@ describe Gitlab::Database do
|
||||||
expect(described_class.version).to eq '9.4.4'
|
expect(described_class.version).to eq '9.4.4'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'memoizes the result' do
|
||||||
|
count = ActiveRecord::QueryRecorder
|
||||||
|
.new { 2.times { described_class.version } }
|
||||||
|
.count
|
||||||
|
|
||||||
|
expect(count).to eq(1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '.join_lateral_supported?' do
|
describe '.join_lateral_supported?' do
|
||||||
|
|
Loading…
Reference in a new issue