Skip to content

Commit

Permalink
Merge commit 'refs/pull/2909/head' of https://github.com/rails/rails
Browse files Browse the repository at this point in the history
…into rawr

* https://github.com/rails/rails:
  Postgresql adapter: added current_schema check for table_exists?
  Postgresql adapter: added current_schema check for table_exists?
  • Loading branch information
tenderlove committed Sep 7, 2011
1 parent 9dd5c03 commit f5ea24b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Expand Up @@ -719,12 +719,10 @@ def table_exists?(name)
binds << [nil, schema] if schema

exec_query(<<-SQL, 'SCHEMA', binds).rows.first[0].to_i > 0
SELECT COUNT(*)
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind in ('v','r')
AND c.relname = $1
AND n.nspname = #{schema ? '$2' : 'ANY (current_schemas(false))'}
SELECT COUNT(*)
FROM pg_tables
WHERE tablename = $1
AND schemaname = #{schema ? "'#{schema}'" : "ANY (current_schemas(false))"}
SQL
end

Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/adapters/postgresql/schema_test.rb
Expand Up @@ -91,6 +91,12 @@ def test_table_exists_quoted_names
end
end

def test_table_exists_quoted_table
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
assert_nothing_raised do
assert_equal COLUMNS, columns("#{SCHEMA_NAME}.#{TABLE_NAME}")
Expand Down

0 comments on commit f5ea24b

Please sign in to comment.