Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8659: index_dump_in_postgresql_adapter_and_matching_test_case.patch

File index_dump_in_postgresql_adapter_and_matching_test_case.patch, 3.0 kB (added by jean.helou, 1 year ago)

Patch for postresql_adapter.rb and schema_test_postgresql.rb

  • test/schema_test_postgresql.rb

    old new  
    44  self.use_transactional_fixtures = false 
    55 
    66  SCHEMA_NAME = 'test_schema' 
     7  SCHEMA2_NAME = 'test_schema2' 
    78  TABLE_NAME = 'things' 
     9  INDEX_NAME = 'index_things_on_name' 
     10  INDEX_COLUMN = 'name' 
    811  COLUMNS = [ 
    912    'id integer', 
    1013    'name character varying(50)', 
     
    1417  def setup 
    1518    @connection = ActiveRecord::Base.connection 
    1619    @connection.execute "CREATE SCHEMA #{SCHEMA_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})" 
     20    @connection.execute "CREATE SCHEMA #{SCHEMA2_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})" 
     21    @connection.execute "CREATE INDEX #{INDEX_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_COLUMN});" 
     22    @connection.execute "CREATE INDEX #{INDEX_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_COLUMN});"  
    1723  end 
    1824 
    1925  def teardown 
     26    @connection.execute "DROP INDEX #{SCHEMA2_NAME}.#{INDEX_NAME}" 
     27    @connection.execute "DROP INDEX #{SCHEMA_NAME}.#{INDEX_NAME}" 
     28    @connection.execute "DROP SCHEMA #{SCHEMA2_NAME} CASCADE" 
    2029    @connection.execute "DROP SCHEMA #{SCHEMA_NAME} CASCADE" 
    2130  end 
    2231 
     
    4857    assert_nothing_raised { with_schema_search_path nil } 
    4958  end 
    5059 
     60  def test_dump_indexes 
     61    indexes = @connection.indexes(TABLE_NAME)     
     62    assert_equal 1,indexes.size 
     63    idx = indexes[0] 
     64    assert_equal TABLE_NAME, idx.table 
     65    assert_equal 1, idx.columns.size 
     66    assert_equal INDEX_COLUMN, idx.columns[0] 
     67    assert_equal INDEX_NAME, idx.table.inspect 
     68     
     69  end 
    5170  private 
    5271    def columns(table_name) 
    5372      @connection.send(:column_definitions, table_name).map do |name, type, default| 
  • lib/active_record/connection_adapters/postgresql_adapter.rb

    old new  
    195195      end 
    196196 
    197197      def indexes(table_name, name = nil) #:nodoc: 
    198         result = query(<<-SQL, name) 
    199           SELECT i.relname, d.indisunique, a.attname 
    200             FROM pg_class t, pg_class i, pg_index d, pg_attribute a 
     198        schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',') 
     199        result = query(<<-SQL, name) 
     200          SELECT distinct i.relname, d.indisunique, a.attname, s.schemaname 
     201            FROM pg_class t, pg_class i, pg_index d, pg_attribute a, pg_tables s 
    201202           WHERE i.relkind = 'i' 
    202203             AND d.indexrelid = i.oid 
    203204             AND d.indisprimary = 'f' 
    204205             AND t.oid = d.indrelid 
    205206             AND t.relname = '#{table_name}' 
     207             AND t.relname = s.tablename 
     208             AND s.schemaname in (#{schemas}) 
    206209             AND a.attrelid = t.oid 
    207210             AND ( d.indkey[0]=a.attnum OR d.indkey[1]=a.attnum 
    208211                OR d.indkey[2]=a.attnum OR d.indkey[3]=a.attnum