Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

postgres adapter mishandles full-text-search indexes

#4949
  1. Create a full text search index in Postgres (using the fine texticle gem, or others).
  2. Run "rake db:migrate"
  3. Run "rake spec" or "rake test"

The schema dumper will create an add_index ... line with "[nil]" for the column names. This will break when db:schema:load runs for testing. The (easy) work-around is to comment out the offending lines (until the next migration, that is).

Enclosed is a patch file (with test) for the postgresql_adapter. It simply throws away any IndexDefinitions that have 0 columns in it. Since the fts index isn't portable anyway, including it in schema.rb is probably not the best place to include.

diff --git a/activerecord/test/cases/schema_test_postgresql.rb b/activerecord/test/cases/schema_test_postgresql.rb
index 3ed9b19..a5c3e69 100644
--- a/activerecord/test/cases/schema_test_postgresql.rb
+++ b/activerecord/test/cases/schema_test_postgresql.rb
@@ -9,9 +9,11 @@ class SchemaTest < ActiveRecord::TestCase
   CAPITALIZED_TABLE_NAME = 'Things'
   INDEX_A_NAME = 'a_index_things_on_name'
   INDEX_B_NAME = 'b_index_things_on_different_columns_in_each_schema'
+  INDEX_C_NAME = 'c_index_full_text_search'
   INDEX_A_COLUMN = 'name'
   INDEX_B_COLUMN_S1 = 'email'
   INDEX_B_COLUMN_S2 = 'moment'
+  INDEX_C_COLUMN = %q{(to_tsvector('english', coalesce(things.name, '')))}
   COLUMNS = [
     'id integer',
     'name character varying(50)',
@@ -45,6 +47,8 @@ class SchemaTest < ActiveRecord::TestCase
     @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});"
+    @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});"
   end
 
   def teardown
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index e842426..fec6550 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -622,11 +622,11 @@ module ActiveRecord
           SQL
 
           column_names = indkey.map {|attnum| columns[attnum] }
-          IndexDefinition.new(table_name, index_name, unique, column_names)
+          column_names.compact == [] ? nil : IndexDefinition.new(table_name, index_name, unique, column_names)
 
         end
 
-        indexes
+        indexes.compact
       end
 
       # Returns the list of all column definitions for a table.

Reported by Ken Mayer · June 23rd, 2010 @ 10:44 PM

State: resolved
Milestone: none
Assigned to: Aaron Patterson Aaron Patterson
Importance: none

Activity

  1. Aaron Patterson
    Aaron Patterson
    • Tag changed from activerecord, postgres, postgresql, schema_dumper to activerecord, patch, postgres, postgresql, schema_dumper

    I've reformated the patch for the current directory structure. Cleaned the implementation a bit.

    June 25th, 2010 @ 11:50 PM

  2. Repository
    Repository
    • State changed from new to resolved

    (from [4464d10e68045e6723d4eb62734213c4296ef339]) index dump should not include full text indexes. Thanks Ken Mayer for the original patch! [#4949 postgres adapter mishandles full-text-search indexes state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/4464d10e68045e6723d4eb62734213...

    June 25th, 2010 @ 11:59 PM

  3. bingbing