Skip to content

Commit

Permalink
Indexes are now *NOT* created by default in Mongoid.
Browse files Browse the repository at this point in the history
Also added rake db:create_indexes that will force mongoid to create the indexes on your database (may take a long time).

If you want the old functionality, just set autocreate_indexes to true (via yml, or direct config). The recommended behavior though is to keep this turned off, and create indexes manually via mongodb.
  • Loading branch information
Jacques Crocker committed Jul 21, 2010
1 parent e45c4f8 commit 6e70806
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
7 changes: 7 additions & 0 deletions lib/mongoid/config.rb
Expand Up @@ -11,6 +11,7 @@ class Config #:nodoc
:parameterize_keys,
:persist_in_safe_mode,
:raise_not_found_error,
:autocreate_indexes,
:use_object_ids,
:skip_version_check

Expand Down Expand Up @@ -184,11 +185,17 @@ def reset
@persist_in_safe_mode = true
@raise_not_found_error = true
@reconnect_time = 3
@autocreate_indexes = false
@use_object_ids = false
@skip_version_check = false
@time_zone = nil
end

# Allows the override of autocreate_indexes via ENV variable
def autocreate_indexes
ENV["MONGOID_CREATE_INDEXES"] || @autocreate_indexes
end

##
# If Mongoid.use_object_ids = true
# Convert args to BSON::ObjectID
Expand Down
12 changes: 8 additions & 4 deletions lib/mongoid/indexes.rb
Expand Up @@ -11,16 +11,20 @@ module ClassMethods #:nodoc
# Add the default indexes to the root document if they do not already
# exist. Currently this is only _type.
def add_indexes
if hereditary && !indexed
self._collection.create_index(:_type, :unique => false, :background => true)
self.indexed = true
if Mongoid.autocreate_indexes
if hereditary && !indexed
self._collection.create_index(:_type, :unique => false, :background => true)
self.indexed = true
end
end
end

# Adds an index on the field specified. Options can be :unique => true or
# :unique => false. It will default to the latter.
def index(name, options = { :unique => false })
collection.create_index(name, options)
if Mongoid.autocreate_indexes
collection.create_index(name, options)
end
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions lib/mongoid/railties/database.rake
Expand Up @@ -54,6 +54,15 @@ namespace :db do
end
end

if not Rake::Task.task_defined?("db:create_indexes")
task :create_indexes do
# force mongoid to create indexes
ENV["MONGOID_CREATE_INDEXES"] = true
Rake::Task["environment"].invoke
end
end


########
# TODO: lots more useful db tasks can be added here. stuff like copyDatabase, etc
########
Expand Down
1 change: 1 addition & 0 deletions spec/config/mongoid.yml
Expand Up @@ -10,6 +10,7 @@ defaults: &defaults
persist_in_safe_mode: false
raise_not_found_error: false
reconnect_time: 5
autocreate_indexes: false
use_object_ids: true
persist_types: false
option_no_exist: false
Expand Down
19 changes: 19 additions & 0 deletions spec/unit/mongoid/indexes_spec.rb
Expand Up @@ -29,6 +29,8 @@
context "when indexes have not been added" do

before do
Mongoid.autocreate_indexes = true

@class = Class.new do
include Mongoid::Indexes
def self.hereditary
Expand All @@ -37,6 +39,10 @@ def self.hereditary
end
end

after do
Mongoid.autocreate_indexes = false
end

it "adds the _type index" do
@class.expects(:_collection).returns(@collection)
@collection.expects(:create_index).with(:_type, :unique => false, :background => true)
Expand Down Expand Up @@ -69,13 +75,19 @@ def self.hereditary
describe ".index" do

before do
Mongoid.autocreate_indexes = true

@collection = mock
@class = Class.new do
include Mongoid::Indexes
end
@class.expects(:collection).returns(@collection)
end

after do
Mongoid.autocreate_indexes = false
end

context "when unique" do

it "creates a unique index on the collection" do
Expand All @@ -86,6 +98,13 @@ def self.hereditary
end

context "when not unique" do
before do
Mongoid.autocreate_indexes = true
end

after do
Mongoid.autocreate_indexes = false
end

it "creates an index on the collection" do
@collection.expects(:create_index).with(:name, :unique => false)
Expand Down

0 comments on commit 6e70806

Please sign in to comment.