Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Multiple databases with Mongoid #110

Merged
merged 5 commits into from Oct 14, 2014
View
@@ -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
@@ -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)
@@ -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
@@ -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
@@ -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