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:
Yorick Peterse 2018-05-17 21:20:15 +02:00
parent 3a402fc7fc
commit 63c58a6dd0
No known key found for this signature in database
GPG key ID: EDD30D2BEB691AC9
3 changed files with 20 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
title: Memoize Gitlab::Database.version
merge_request:
author:
type: performance

View file

@ -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?

View file

@ -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