Commit 63831a24 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'memoize-database-version' into 'master'

Memoize Gitlab::Database.version

See merge request gitlab-org/gitlab-ce!19021
parents ff944005 63c58a6d
---
title: Memoize Gitlab::Database.version
merge_request:
author:
type: performance
...@@ -43,7 +43,7 @@ module Gitlab ...@@ -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 ...@@ -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 ...@@ -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
......
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