Skip to content
This repository
Browse code

modified table_exists? to always use current search_path or schema if…

… explicitly set
  • Loading branch information...
commit 0252376cc98404cb6dfac92c436aac2149eee29c 1 parent 1ae9b29
Brad Robertson authored June 08, 2011
2  activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -636,7 +636,7 @@ def table_exists?(name)
636 636
             SELECT COUNT(*)
637 637
             FROM pg_tables
638 638
             WHERE tablename = '#{table.gsub(/(^"|"$)/,'')}'
639  
-            #{schema ? "AND schemaname = '#{schema}'" : ''}
  639
+            AND schemaname = #{schema ? "'#{schema}'" : "ANY (current_schemas(false))"}
640 640
         SQL
641 641
       end
642 642
 
13  activerecord/test/cases/adapters/postgresql/schema_test.rb
@@ -5,6 +5,7 @@ class SchemaTest < ActiveRecord::TestCase
5 5
 
6 6
   SCHEMA_NAME = 'test_schema'
7 7
   SCHEMA2_NAME = 'test_schema2'
  8
+  SCHEMA3_NAME = 'foo'
8 9
   TABLE_NAME = 'things'
9 10
   CAPITALIZED_TABLE_NAME = 'Things'
10 11
   INDEX_A_NAME = 'a_index_things_on_name'
@@ -49,11 +50,13 @@ def setup
49 50
     @connection.execute "CREATE INDEX #{INDEX_B_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_B_COLUMN_S2});"
50 51
     @connection.execute "CREATE INDEX #{INDEX_C_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME}  USING gin (#{INDEX_C_COLUMN});"
51 52
     @connection.execute "CREATE INDEX #{INDEX_C_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME}  USING gin (#{INDEX_C_COLUMN});"
  53
+    @connection.execute "CREATE SCHEMA #{SCHEMA3_NAME}"
52 54
   end
53 55
 
54 56
   def teardown
55 57
     @connection.execute "DROP SCHEMA #{SCHEMA2_NAME} CASCADE"
56 58
     @connection.execute "DROP SCHEMA #{SCHEMA_NAME} CASCADE"
  59
+    @connection.execute "DROP SCHEMA #{SCHEMA3_NAME}"
57 60
   end
58 61
 
59 62
   def test_table_exists?
@@ -67,8 +70,16 @@ def test_table_exists_wrong_schema
67 70
     assert(!@connection.table_exists?("foo.things"), "table should not exist")
68 71
   end
69 72
 
  73
+  def test_table_exists_wrong_search_path
  74
+    with_schema_search_path SCHEMA3_NAME do
  75
+      assert(!@connection.table_exists?("things"), "table should not exist")
  76
+    end
  77
+  end
  78
+
70 79
   def test_table_exists_quoted_table
71  
-    assert(@connection.table_exists?('"things.table"'), "table should exist")
  80
+    with_schema_search_path(SCHEMA_NAME) do
  81
+      assert(@connection.table_exists?('"things.table"'), "table should exist")
  82
+    end
72 83
   end
73 84
 
74 85
   def test_with_schema_prefixed_table_name

0 notes on commit 0252376

Please sign in to comment.
Something went wrong with that request. Please try again.