From 5a1318e3c34a9dc978c48a7017eecc06e79b012c Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 24 Mar 2021 08:48:04 +1100 Subject: [PATCH] DEV: upgrade mini_sql (#12465) * DEV: upgrade mini_sql Even though we are not planning on using this quite yet, mini_sql now supports prepared statements. Would like this upgrade merged so we can do some benchmarking. Note, this will not work with pg_bouncer, but sites that are not using it may benefit from the feature. * implement multisite friendly prepared statements --- Gemfile.lock | 2 +- lib/mini_sql_multisite_connection.rb | 14 ++++++++++++++ spec/lib/mini_sql_multisite_connection_spec.rb | 5 +++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 9c140cba2b7..23151f98c16 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -214,7 +214,7 @@ GEM libv8 (~> 8.4.255) mini_scheduler (0.13.0) sidekiq (>= 4.2.3) - mini_sql (1.0.1) + mini_sql (1.1.3) mini_suffix (0.3.2) ffi (~> 1.9) minitest (5.14.4) diff --git a/lib/mini_sql_multisite_connection.rb b/lib/mini_sql_multisite_connection.rb index 2cf50ca61e9..a4feb1a8706 100644 --- a/lib/mini_sql_multisite_connection.rb +++ b/lib/mini_sql_multisite_connection.rb @@ -82,6 +82,20 @@ class MiniSqlMultisiteConnection < MiniSql::Postgres::Connection ActiveRecord::Base.connection.raw_connection end + # make for a multisite friendly prepared statement cache + def prepared(condition = true) + if condition + conn = raw_connection.instance_variable_get(:@mini_sql_prepared_connection) + if !conn + conn = MiniSql::Postgres::PreparedConnection.new(self) + raw_connection.instance_variable_set(:@mini_sql_prepared_connection, conn) + end + conn + else + self + end + end + def build(sql) CustomBuilder.new(self, sql) end diff --git a/spec/lib/mini_sql_multisite_connection_spec.rb b/spec/lib/mini_sql_multisite_connection_spec.rb index 47e8f558371..3534f5e34fc 100644 --- a/spec/lib/mini_sql_multisite_connection_spec.rb +++ b/spec/lib/mini_sql_multisite_connection_spec.rb @@ -73,6 +73,11 @@ describe MiniSqlMultisiteConnection do expect(outputString).to eq("123") end + it "supports prepared statements" do + DB.prepared.query("SELECT ?", 1) + DB.prepared.query("SELECT ?", 2) + end + end end