From b3b0a0c388b27ec9d658823b963f4bc59ff2f270 Mon Sep 17 00:00:00 2001
From: Michael Koziarski <michael@koziarski.com>
Date: Mon, 3 Mar 2008 06:56:31 +0000
Subject: Make the schema dumper respect the schema settings in database.yml. 
 References #8659 [sveiss]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8979 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
---
 .../connection_adapters/postgresql_adapter.rb      |  8 +++--
 activerecord/test/cases/schema_test_postgresql.rb  | 38 ++++++++++++++++++++++
 2 files changed, 43 insertions(+), 3 deletions(-)

(limited to 'activerecord')

diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 6b0204191c..41024539b2 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -486,14 +486,16 @@ module ActiveRecord
 
       # Returns the list of all indexes for a table.
       def indexes(table_name, name = nil)
-        result = query(<<-SQL, name)
-          SELECT i.relname, d.indisunique, a.attname
-            FROM pg_class t, pg_class i, pg_index d, pg_attribute a
+         schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',')
+         result = query(<<-SQL, name)
+           SELECT distinct i.relname, d.indisunique, a.attname
+             FROM pg_class t, pg_class i, pg_index d, pg_attribute a
            WHERE i.relkind = 'i'
              AND d.indexrelid = i.oid
              AND d.indisprimary = 'f'
              AND t.oid = d.indrelid
              AND t.relname = '#{table_name}'
+             AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname IN (#{schemas}) )
              AND a.attrelid = t.oid
              AND ( d.indkey[0]=a.attnum OR d.indkey[1]=a.attnum
                 OR d.indkey[2]=a.attnum OR d.indkey[3]=a.attnum
diff --git a/activerecord/test/cases/schema_test_postgresql.rb b/activerecord/test/cases/schema_test_postgresql.rb
index b5716cac4c..336a38765c 100644
--- a/activerecord/test/cases/schema_test_postgresql.rb
+++ b/activerecord/test/cases/schema_test_postgresql.rb
@@ -4,19 +4,32 @@ class SchemaTest < ActiveRecord::TestCase
   self.use_transactional_fixtures = false
 
   SCHEMA_NAME = 'test_schema'
+  SCHEMA2_NAME = 'test_schema2'
   TABLE_NAME = 'things'
+  INDEX_A_NAME = 'a_index_things_on_name'
+  INDEX_B_NAME = 'b_index_things_on_different_columns_in_each_schema'
+  INDEX_A_COLUMN = 'name'
+  INDEX_B_COLUMN_S1 = 'email'
+  INDEX_B_COLUMN_S2 = 'moment'
   COLUMNS = [
     'id integer',
     'name character varying(50)',
+    'email character varying(50)',
     'moment timestamp without time zone default now()'
   ]
 
   def setup
     @connection = ActiveRecord::Base.connection
     @connection.execute "CREATE SCHEMA #{SCHEMA_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
+    @connection.execute "CREATE SCHEMA #{SCHEMA2_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
+    @connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_A_COLUMN});"
+    @connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_A_COLUMN});"
+    @connection.execute "CREATE INDEX #{INDEX_B_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_B_COLUMN_S1});"
+    @connection.execute "CREATE INDEX #{INDEX_B_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_B_COLUMN_S2});"
   end
 
   def teardown
+    @connection.execute "DROP SCHEMA #{SCHEMA2_NAME} CASCADE"
     @connection.execute "DROP SCHEMA #{SCHEMA_NAME} CASCADE"
   end
 
@@ -48,6 +61,14 @@ class SchemaTest < ActiveRecord::TestCase
     assert_nothing_raised { with_schema_search_path nil }
   end
 
+  def test_dump_indexes_for_schema_one
+    do_dump_index_tests_for_schema(SCHEMA_NAME, INDEX_A_COLUMN, INDEX_B_COLUMN_S1)
+  end
+
+  def test_dump_indexes_for_schema_two
+    do_dump_index_tests_for_schema(SCHEMA2_NAME, INDEX_A_COLUMN, INDEX_B_COLUMN_S2)
+  end
+
   private
     def columns(table_name)
       @connection.send(:column_definitions, table_name).map do |name, type, default|
@@ -61,4 +82,21 @@ class SchemaTest < ActiveRecord::TestCase
     ensure
       @connection.schema_search_path = "'$user', public"
     end
+
+    def do_dump_index_tests_for_schema(this_schema_name, first_index_column_name, second_index_column_name)
+      with_schema_search_path(this_schema_name) do
+        indexes = @connection.indexes(TABLE_NAME).sort_by {|i| i.name}
+        assert_equal 2,indexes.size
+
+        do_dump_index_assertions_for_one_index(indexes[0], INDEX_A_NAME, first_index_column_name)
+        do_dump_index_assertions_for_one_index(indexes[1], INDEX_B_NAME, second_index_column_name)
+      end
+    end
+
+    def do_dump_index_assertions_for_one_index(this_index, this_index_name, this_index_column)
+      assert_equal TABLE_NAME, this_index.table
+      assert_equal 1, this_index.columns.size
+      assert_equal this_index_column, this_index.columns[0]
+      assert_equal this_index_name, this_index.name
+    end
 end
-- 
cgit v1.2.3