diff --git a/lib/mongoid/config.rb b/lib/mongoid/config.rb index 014f3f1b07..f575a825a8 100644 --- a/lib/mongoid/config.rb +++ b/lib/mongoid/config.rb @@ -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 @@ -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 diff --git a/lib/mongoid/indexes.rb b/lib/mongoid/indexes.rb index 60faeabf26..eb3d04c89d 100644 --- a/lib/mongoid/indexes.rb +++ b/lib/mongoid/indexes.rb @@ -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 diff --git a/lib/mongoid/railties/database.rake b/lib/mongoid/railties/database.rake index 673fdb6842..5297c8caa8 100644 --- a/lib/mongoid/railties/database.rake +++ b/lib/mongoid/railties/database.rake @@ -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 ######## diff --git a/spec/config/mongoid.yml b/spec/config/mongoid.yml index 17a716e29e..2ffb642ac0 100644 --- a/spec/config/mongoid.yml +++ b/spec/config/mongoid.yml @@ -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 diff --git a/spec/unit/mongoid/indexes_spec.rb b/spec/unit/mongoid/indexes_spec.rb index dedf5f2a5f..b6b41117d9 100644 --- a/spec/unit/mongoid/indexes_spec.rb +++ b/spec/unit/mongoid/indexes_spec.rb @@ -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 @@ -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) @@ -69,6 +75,8 @@ def self.hereditary describe ".index" do before do + Mongoid.autocreate_indexes = true + @collection = mock @class = Class.new do include Mongoid::Indexes @@ -76,6 +84,10 @@ def self.hereditary @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 @@ -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)