Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4189

remove_undefined_indexes always removes full text indexes

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.2
    • Affects Version/s: 5.0.0
    • Component/s: None
    • Labels:
      None

      As a recent errbit user discovered (https://github.com/errbit/errbit/issues/994), running rake db:mongoid:remove_undefined_indexes removes full text indexes when they are defined.

      You can reproduce this issue with the following steps:
      1. check out errbit: https://github.com/errbit/errbit/commit/d168395807ea897bd48efcc8ee68fb01789073a3
      2. Follow the installation instructions https://github.com/errbit/errbit#installation
      3. Run rake db:mongoid:create_indexes
      4. Run rake db:mongoid:remove_undefined_indexes

      Notice how the full text index on the Problem model is always removed even though it is defined.

      After a bit of debugging, I can see that Mongoid is expecting they keys for Problem.index_specifications to match the keys in Problem.collection.indexes, but only the regular indexes match and the full text index does not:

      ~~~
      [11] pry(Problem)> Problem.index_specifications.map

      { |i| i.key }

      => [{:app_id=>1},
      {:app_name=>1},
      {:message=>1},
      {:last_notice_at=>1},
      {:first_notice_at=>1},
      {:last_deploy_at=>1},
      {:resolved_at=>1},
      {:notices_count=>1},
      {:error_class=>"text",
      :where=>"text",
      :message=>"text",
      :app_name=>"text",
      :environment=>"text"}]
      [12] pry(Problem)> Problem.collection.indexes.map

      { |i| i['key'].symbolize_keys }

      => [{:_id=>1},
      {:app_id=>1},
      {:app_name=>1},
      {:message=>1},
      {:last_notice_at=>1},
      {:first_notice_at=>1},
      {:last_deploy_at=>1},
      {:resolved_at=>1},
      {:notices_count=>1},
      {:_fts=>"text", :_ftsx=>1}]
      ~~~

      Because the index key for the text index doesn't match the specification, the text in this rake task fails: https://github.com/mongodb/mongoid/blob/master/lib/mongoid/tasks/database.rb#L51 and the index is dropped when it should not be dropped.

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            stevecrozz Stephen Crosby
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: