Commit 63c58a6d authored by Yorick Peterse's avatar Yorick Peterse

Memoize Gitlab::Database.version

This removes the need for running a database query every time we want to
check the database version.
parent 3a402fc7
---
title: Memoize Gitlab::Database.version
merge_request:
author:
type: performance
......@@ -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?
......
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment