Skip to content

Commit 3088d23

Browse files
committed
Merge pull request rails#3232 from Juanmcuello/pg_prepared_statements
Use the schema_search_path in prepared statements.
2 parents 9988848 + ac659b8 commit 3088d23

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -1035,13 +1035,14 @@ def exec_no_cache(sql, binds)
10351035
end
10361036

10371037
def exec_cache(sql, binds)
1038-
unless @statements.key? sql
1038+
sql_key = "#{schema_search_path}-#{sql}"
1039+
unless @statements.key? sql_key
10391040
nextkey = @statements.next_key
10401041
@connection.prepare nextkey, sql
1041-
@statements[sql] = nextkey
1042+
@statements[sql_key] = nextkey
10421043
end
10431044

1044-
key = @statements[sql]
1045+
key = @statements[sql_key]
10451046

10461047
# Clear the queue
10471048
@connection.get_last_result

activerecord/test/cases/adapters/postgresql/schema_test.rb

+19
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class Thing4 < ActiveRecord::Base
3838
set_table_name 'test_schema."Things"'
3939
end
4040

41+
class Thing5 < ActiveRecord::Base
42+
set_table_name 'things'
43+
end
44+
4145
def setup
4246
@connection = ActiveRecord::Base.connection
4347
@connection.execute "CREATE SCHEMA #{SCHEMA_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
@@ -236,6 +240,21 @@ def test_current_schema
236240
end
237241
end
238242

243+
def test_prepared_statements_with_multiple_schemas
244+
245+
@connection.schema_search_path = SCHEMA_NAME
246+
Thing5.create(:id => 1, :name => "thing inside #{SCHEMA_NAME}", :email => "thing1@localhost", :moment => Time.now)
247+
248+
@connection.schema_search_path = SCHEMA2_NAME
249+
Thing5.create(:id => 1, :name => "thing inside #{SCHEMA2_NAME}", :email => "thing1@localhost", :moment => Time.now)
250+
251+
@connection.schema_search_path = SCHEMA_NAME
252+
assert_equal 1, Thing5.count
253+
254+
@connection.schema_search_path = SCHEMA2_NAME
255+
assert_equal 1, Thing5.count
256+
end
257+
239258
def test_schema_exists?
240259
{
241260
'public' => true,

0 commit comments

Comments
 (0)