Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Multiple databases with Mongoid #110

Merged
merged 5 commits into from

6 participants

@nyarly

Added code such that:

DatabaseCleaner[:mongoid, :connection => "visitors"].strategy = :truncation

can truncate the non-master "visitors" database.

There weren't existent specs for mongoid, or I would have added to them - can't spare the time now to build them all from scratch.

@kristianmandrup

Does this work with the latest mongoid 3.0.beta ?

I have been trying to upgrade mongoid_search to support mongoid 3, but the specs can't run due to dependency on database_cleaner which needs a mongoid 3 strategy it seems.

@kristianmandrup

Ah... still doesn't support mongoid 3.0. Requires a brand new strategy, since the main mongoid API has changed drastically!

@nyarly

On the other hand, Mongoid 2 is the current release - any chance this PR will get merged?

@bmabey
Owner

I've merged in support for Mongoid 3.0 and so this pull request no longer will merge cleanly. :( Sorry about that. From the looks of it it will be easy to update but we will also need to add the needed code for the 3.0 api as well.

@balinterdi

Not sure how this pull request solves the issue under Mongoid 2.0. As far as I see only one database is truncated. I've made my own fix, and issued a pull request for it

@bmabey
Owner

I plan on merging in @balinterdi's commit once he rebases it. Would you guys mind looking at it and telling me if that is the behavior you would like to see? One downside to it is that it would only allow a single strategy for all Mongoid databases. Hmm.. perhaps that is a showstopper since that is certainly a different approach with the other kinds of databases. WDYT?

@bmabey
Owner

I've recanted and I think this approach is the best. If someone wants to rebase this to master for a clean merge I'll pull it in.

@tranvictor

@bmabey are you going to merge this since i see database_cleaner still doesn't support to clean multiple sessions of mongoid?

@etagwerker etagwerker merged commit bb3dce2 into DatabaseCleaner:master
@etagwerker
Owner

Merged. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 5, 2012
  1. @nyarly

    Hand tested to work

    nyarly authored
Commits on Mar 20, 2013
  1. @nyarly
Commits on Jun 26, 2013
  1. @nyarly
  2. @nyarly
  3. @nyarly

    Oops

    nyarly authored
    Swapped the order of two arrays
This page is out of date. Refresh to see the latest.
View
9 Gemfile
@@ -16,7 +16,8 @@ source "http://rubygems.org"
group :development do
gem "rake"
- gem "ruby-debug"
+ gem "ruby-debug", :platform => :ruby_18
+ gem "ruby-debug19", :platform => :ruby_19
gem "bundler"
gem "jeweler"
@@ -37,9 +38,9 @@ group :development do
end
group :test do
- gem "rspec"
- gem "rspactor"
- gem "rcov"
+ gem "rspec", "< 2.0"
+ gem "rcov", :platform => :ruby_18
+ gem "simplecov", :platform => :ruby_19
gem "ZenTest"
end
View
42 Gemfile.lock
@@ -6,6 +6,7 @@ GEM
activesupport (= 2.3.8)
activesupport (2.3.8)
addressable (2.2.0)
+ archive-tar-minitar (0.5.2)
bson (1.0.4)
builder (2.1.2)
columnize (0.3.1)
@@ -35,7 +36,7 @@ GEM
dm-transactions (= 1.0.0)
dm-types (= 1.0.0)
dm-validations (= 1.0.0)
- diff-lcs (1.1.2)
+ diff-lcs (1.1.3)
dm-aggregates (1.0.0)
dm-core (~> 1.0.0)
dm-constraints (1.0.0)
@@ -73,13 +74,10 @@ GEM
durran-validatable (2.0.1)
extlib (0.9.15)
fastercsv (1.5.3)
- ffi (0.6.3)
- rake (>= 0.8.7)
gemcutter (0.6.1)
gherkin (2.1.5)
trollop (~> 1.16.2)
git (1.2.5)
- growl (1.0.3)
jeweler (1.4.0)
gemcutter (>= 0.1.0)
git (>= 1.2.5)
@@ -88,9 +86,9 @@ GEM
activesupport (>= 2.3.4)
json (1.4.6)
json_pure (1.4.6)
- libnotify (0.2.0)
- ffi (>= 0.6.2)
linecache (0.43)
+ linecache19 (0.5.12)
+ ruby_core_source (>= 0.1.4)
mime-types (1.16)
mongo (1.0.7)
bson (>= 1.0.4)
@@ -104,33 +102,38 @@ GEM
durran-validatable (>= 2.0.1)
mongo (~> 1.0.1)
will_paginate (< 2.9)
+ multi_json (1.2.0)
plucky (0.3.4)
mongo (~> 1.0.7)
rake (0.8.7)
- rb-inotify (0.8.1)
- ffi (>= 0.5.0)
rcov (0.9.8)
rest-client (1.6.0)
mime-types (>= 1.16)
- rspactor (0.7.0.beta.6)
- bundler (>= 1.0.0.rc.5)
- growl (>= 1.0.3)
- libnotify (>= 0.1.3)
- rb-inotify
- sys-uname (>= 0.8.4)
- trollop (>= 1.16.2)
- rspec (1.3.0)
+ rspec (1.3.2)
ruby-debug (0.10.3)
columnize (>= 0.1)
ruby-debug-base (~> 0.10.3.0)
ruby-debug-base (0.10.3)
linecache (>= 0.3)
+ ruby-debug-base19 (0.11.25)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby_core_source (>= 0.1.4)
+ ruby-debug19 (0.11.6)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby-debug-base19 (>= 0.11.19)
+ ruby_core_source (0.1.5)
+ archive-tar-minitar (>= 0.5.2)
rubyforge (2.0.4)
json_pure (>= 1.1.7)
sequel (3.21.0)
+ simplecov (0.6.1)
+ multi_json (~> 1.0)
+ simplecov-html (~> 0.5.3)
+ simplecov-html (0.5.3)
sqlite3-ruby (1.3.1)
stringex (1.1.0)
- sys-uname (0.8.4)
term-ansicolor (1.0.5)
trollop (1.16.2)
tzinfo (0.3.22)
@@ -155,9 +158,10 @@ DEPENDENCIES
mongoid (= 1.9.1)
rake
rcov
- rspactor
- rspec
+ rspec (< 2.0)
ruby-debug
+ ruby-debug19
sequel (~> 3.21.0)
+ simplecov
sqlite3-ruby
tzinfo (= 0.3.22)
View
39 lib/database_cleaner/mongo/truncation.rb
@@ -13,10 +13,45 @@ def clean
private
- def collections
- database.collections.select { |c| c.name !~ /^system\./ }
+ def collections_cache
+ @@collections_cache ||= {}
+ end
+
+ def mongoid_collection_names
+ @@mongoid_collection_names ||= Hash.new{|h,k| h[k]=[]}.tap do |names|
+ ObjectSpace.each_object(Class) do |klass|
+ next unless klass.ancestors.include?(::Mongoid::Document)
+ next if klass.embedded
+ next if klass.collection_name.empty?
+ names[klass.db.name] << klass.collection_name
+ end
+ end
+ end
+
+ def not_caching(db_name, list)
+ @@not_caching ||= {}
+
+ unless @@not_caching.has_key?(db_name)
+ @@not_caching[db_name] = true
+
+ puts "Not caching collection names for db #{db_name}. Missing these from models: #{list}"
+ end
end
+ def collections
+ return collections_cache[database.name] if collections_cache.has_key?(database.name)
+ db_collections = database.collections.select { |c| c.name !~ /^system\./ }
+
+ missing_collections = mongoid_collection_names[database.name] - db_collections.map(&:name)
+
+ if missing_collections.empty?
+ collections_cache[database.name] = db_collections
+ else
+ not_caching(database.name, missing_collections)
+ end
+
+ return db_collections
+ end
end
end
end
View
6 lib/database_cleaner/mongoid/truncation.rb
@@ -12,7 +12,11 @@ class Truncation
private
def database
- ::Mongoid.database
+ if not(@db.nil? or @db == :default)
+ ::Mongoid.databases[@db]
+ else
+ ::Mongoid.database
+ end
end
end
View
23 spec/database_cleaner/base_spec.rb
@@ -47,7 +47,7 @@ module DatabaseCleaner
Object.send(:remove_const, 'CouchPotato') if defined?(::CouchPotato)
Object.send(:remove_const, 'Sequel') if defined?(::Sequel)
end
-
+
let(:cleaner) { DatabaseCleaner::Base.new :autodetect }
it "should raise an error when no ORM is detected" do
@@ -103,7 +103,7 @@ module DatabaseCleaner
cleaner.orm.should == :couch_potato
cleaner.should be_auto_detected
end
-
+
it "should detect Sequel last" do
Object.const_set('Sequel', 'Sequel mock')
@@ -129,6 +129,7 @@ module DatabaseCleaner
describe "comparison" do
it "should be equal if orm, connection and strategy are the same" do
strategy = mock("strategy")
+ strategy.stub!(:to_ary => [strategy])
one = DatabaseCleaner::Base.new(:active_record,:connection => :default)
one.strategy = strategy
@@ -160,7 +161,7 @@ module DatabaseCleaner
cleaner = ::DatabaseCleaner::Base.new "mongoid"
cleaner.orm.should == :mongoid
end
-
+
it "is autodetected if orm is not provided" do
cleaner = ::DatabaseCleaner::Base.new
cleaner.should be_auto_detected
@@ -195,7 +196,11 @@ module DatabaseCleaner
end
describe "strategy_db=" do
- let(:strategy) { mock("strategy") }
+ let(:strategy) {
+ mock("strategy").tap{|strategy|
+ strategy.stub!(:to_ary => [strategy])
+ }
+ }
before(:each) do
subject.strategy = strategy
@@ -295,7 +300,11 @@ module DatabaseCleaner
end
describe "strategy=" do
- let(:mock_strategy) { mock("strategy") }
+ let(:mock_strategy) {
+ mock("strategy").tap{|strategy|
+ strategy.stub!(:to_ary => [strategy])
+ }
+ }
it "should proxy symbolised strategies to create_strategy" do
subject.should_receive(:create_strategy).with(:symbol)
@@ -336,7 +345,9 @@ module DatabaseCleaner
end
it "returns the set strategy" do
- strategum = mock("strategy")
+ strategum = mock("strategy").tap{|strategy|
+ strategy.stub!(:to_ary => [strategy])
+ }
subject.strategy = strategum
subject.strategy.should == strategum
end
Something went wrong with that request. Please try again.