Browse files

Merge pull request #1607 from bradrobertson/pg_adapter

fix table_exists? in postgresql adapter to always use current search_path
  • Loading branch information...
2 parents fc4bce1 + 0252376 commit 9a4d2b27c8865cfda7e3421500d71676fe542c62 @tenderlove tenderlove committed Jul 11, 2011
View
2 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -636,7 +636,7 @@ def table_exists?(name)
SELECT COUNT(*)
FROM pg_tables
WHERE tablename = '#{table.gsub(/(^"|"$)/,'')}'
- #{schema ? "AND schemaname = '#{schema}'" : ''}
+ AND schemaname = #{schema ? "'#{schema}'" : "ANY (current_schemas(false))"}
SQL
end
View
13 activerecord/test/cases/adapters/postgresql/schema_test.rb
@@ -5,6 +5,7 @@ class SchemaTest < ActiveRecord::TestCase
SCHEMA_NAME = 'test_schema'
SCHEMA2_NAME = 'test_schema2'
+ SCHEMA3_NAME = 'foo'
TABLE_NAME = 'things'
CAPITALIZED_TABLE_NAME = 'Things'
INDEX_A_NAME = 'a_index_things_on_name'
@@ -49,11 +50,13 @@ def setup
@connection.execute "CREATE INDEX #{INDEX_B_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING btree (#{INDEX_B_COLUMN_S2});"
@connection.execute "CREATE INDEX #{INDEX_C_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME} USING gin (#{INDEX_C_COLUMN});"
@connection.execute "CREATE INDEX #{INDEX_C_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING gin (#{INDEX_C_COLUMN});"
+ @connection.execute "CREATE SCHEMA #{SCHEMA3_NAME}"
end
def teardown
@connection.execute "DROP SCHEMA #{SCHEMA2_NAME} CASCADE"
@connection.execute "DROP SCHEMA #{SCHEMA_NAME} CASCADE"
+ @connection.execute "DROP SCHEMA #{SCHEMA3_NAME}"
end
def test_table_exists?
@@ -67,8 +70,16 @@ def test_table_exists_wrong_schema
assert(!@connection.table_exists?("foo.things"), "table should not exist")
end
+ def test_table_exists_wrong_search_path
+ with_schema_search_path SCHEMA3_NAME do
+ assert(!@connection.table_exists?("things"), "table should not exist")
+ end
+ end
+
def test_table_exists_quoted_table
- assert(@connection.table_exists?('"things.table"'), "table should exist")
+ with_schema_search_path(SCHEMA_NAME) do
+ assert(@connection.table_exists?('"things.table"'), "table should exist")
+ end
end
def test_with_schema_prefixed_table_name

0 comments on commit 9a4d2b2

Please sign in to comment.