Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the schema_search_path in prepared statements. #3232

Merged
merged 2 commits into from Oct 7, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1035,13 +1035,14 @@ def exec_no_cache(sql, binds)
end

def exec_cache(sql, binds)
unless @statements.key? sql
sql_key = "#{schema_search_path}-#{sql}"
unless @statements.key? sql_key
nextkey = @statements.next_key
@connection.prepare nextkey, sql
@statements[sql] = nextkey
@statements[sql_key] = nextkey
end

key = @statements[sql]
key = @statements[sql_key]

# Clear the queue
@connection.get_last_result
Expand Down
19 changes: 19 additions & 0 deletions activerecord/test/cases/adapters/postgresql/schema_test.rb
Expand Up @@ -38,6 +38,10 @@ class Thing4 < ActiveRecord::Base
set_table_name 'test_schema."Things"'
end

class Thing5 < ActiveRecord::Base
set_table_name 'things'
end

def setup
@connection = ActiveRecord::Base.connection
@connection.execute "CREATE SCHEMA #{SCHEMA_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
Expand Down Expand Up @@ -236,6 +240,21 @@ def test_current_schema
end
end

def test_prepared_statements_with_multiple_schemas

@connection.schema_search_path = SCHEMA_NAME
Thing5.create(:id => 1, :name => "thing inside #{SCHEMA_NAME}", :email => "thing1@localhost", :moment => Time.now)

@connection.schema_search_path = SCHEMA2_NAME
Thing5.create(:id => 1, :name => "thing inside #{SCHEMA2_NAME}", :email => "thing1@localhost", :moment => Time.now)

@connection.schema_search_path = SCHEMA_NAME
assert_equal 1, Thing5.count

@connection.schema_search_path = SCHEMA2_NAME
assert_equal 1, Thing5.count
end

def test_schema_exists?
{
'public' => true,
Expand Down