summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLin Jen-Shin <godfat@godfat.org>2017-02-13 19:51:05 +0800
committerLin Jen-Shin <godfat@godfat.org>2017-02-13 19:51:05 +0800
commitb98745ef512868ca1e9e20d97dec458ff0eb10c4 (patch)
treeb21cad1ef63e766500fe00929d9e0a1b111b4dbf
parent7cb21a2c43b4d932e6a609cc59ce9b5c3589316a (diff)
downloadgitlab-ce-gitlab-connection-pool.tar.gz
Make sure PostgreSQL and MySQL give the same resultgitlab-connection-pool
This might go too far by using `ActiveRecord::AttributeSet::Builder` but unfortunately that's how Rails is doing that to unify the results from different database adapters. I don't know why it's built into the model so tightly while it could just stay as in connection.
-rw-r--r--lib/gitlab/database/connection_pool.rb6
-rw-r--r--spec/lib/gitlab/database/connection_pool_spec.rb22
2 files changed, 20 insertions, 8 deletions
diff --git a/lib/gitlab/database/connection_pool.rb b/lib/gitlab/database/connection_pool.rb
index 7cc1604559c..fb4c55a8ac0 100644
--- a/lib/gitlab/database/connection_pool.rb
+++ b/lib/gitlab/database/connection_pool.rb
@@ -23,11 +23,13 @@ module Gitlab
@ar_pool.spec.config[:pool]
end
- def execute_async(sql)
+ # Pass `method: :exec_query` if we want unified result from query
+ # across PostgreSQL and MySQL
+ def execute_async(sql, method: :execute)
@mutex.synchronize do
@workers << Thread.new do
@ar_pool.with_connection do |connection|
- connection.execute(sql)
+ connection.public_send(method, sql)
end
end
end
diff --git a/spec/lib/gitlab/database/connection_pool_spec.rb b/spec/lib/gitlab/database/connection_pool_spec.rb
index a137a3eeb2d..2e3c6a23571 100644
--- a/spec/lib/gitlab/database/connection_pool_spec.rb
+++ b/spec/lib/gitlab/database/connection_pool_spec.rb
@@ -52,27 +52,28 @@ describe Gitlab::Database::ConnectionPool, lib: true do
describe '#execute_async' do
it 'runs the right query' do
- subject.execute_async('SELECT 1+2 AS value;')
+ subject.execute_async('SELECT 1+2 AS value;', method: :exec_query)
+ result = convert_result_to_hash(subject.join)
- expect(subject.join.map(&:to_a)).to eq([[{ 'value' => '3' }]])
+ expect(result).to eq([[{ 'value' => 3 }]])
end
end
describe '#join' do
before do
2.times.map do |n|
- subject.execute_async("SELECT #{n} AS value;")
+ subject.execute_async("SELECT #{n} AS value;", method: :exec_query)
end
end
it 'joins the threads and give respective values, and clear workers' do
- result = subject.join
+ result = convert_result_to_hash(subject.join)
expected = 2.times.map do |n|
- [{ 'value' => n.to_s }]
+ [{ 'value' => n }]
end
- expect(result.map(&:to_a)).to eq(expected)
+ expect(result).to eq(expected)
expect(subject.workers).to be_empty
end
end
@@ -91,4 +92,13 @@ describe Gitlab::Database::ConnectionPool, lib: true do
expect(subject).to be_closed
end
end
+
+ def convert_result_to_hash(values)
+ values.map do |result|
+ result.map do |row|
+ ActiveRecord::AttributeSet::Builder.new({})
+ .build_from_database(row, result.column_types).to_h
+ end
+ end
+ end
end