This project is archived and is in readonly mode.

#4949 ✓resolved
Ken Mayer

postgres adapter mishandles full-text-search indexes

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

  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.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages