Skip to content

Commit

Permalink
Fix issue where lowercased "write" SQL queries would get sent to replica
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydwatkin committed Apr 15, 2024
1 parent 0e886a6 commit b4c7b5a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
janus-ar (0.11.0)
janus-ar (0.13.0)

GEM
remote: http://rubygems.org/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def send_to_replica(sql, connection: nil, method: :exec_query)
end

def write_query?(sql)
WRITE_PREFIXES.include?(sql.split(' ').first)
WRITE_PREFIXES.include?(sql.upcase.split(' ').first)
end

def update_config
Expand Down
2 changes: 1 addition & 1 deletion lib/janus/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Janus
unless defined?(::Janus::VERSION)
module VERSION
MAJOR = 0
MINOR = 12
MINOR = 13
PATCH = 0
PRE = nil

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,25 @@
expect($query_logger.queries.first).to include '[primary]'
end
end

describe 'INSERT' do
let(:insert_query) { 'INSERT INTO test_table SET `id` = 5;' }

before(:each) do
create_test_table
$query_logger.flush_all
Janus::Context.release_all
end

it 'sends INSERT query to primary' do
ActiveRecord::Base.connection.execute(insert_query)
expect($query_logger.queries.first).to include '[primary]'
end

it 'ignores case when directing queries' do
ActiveRecord::Base.connection.execute(insert_query.downcase)
expect($query_logger.queries.first).to include '[primary]'
end
end
end
end

0 comments on commit b4c7b5a

Please sign in to comment.