Skip to content

Commit

Permalink
Migrator utility additions
Browse files Browse the repository at this point in the history
* Add some more tests
* Support nested hierarchies
  • Loading branch information
ellneal committed Oct 6, 2016
1 parent abf5d65 commit 7d79c40
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
12 changes: 7 additions & 5 deletions lib/couchrest/model/utils/migrate.rb
Expand Up @@ -59,7 +59,7 @@ def load_all_models
def all_models(opts = {})
opts.reverse_merge!(activate: true, with_proxies: false)
callbacks = migrate_each_model(find_models)
callbacks += migrate_each_proxying_model(find_proxying_models) if opts[:with_proxies]
callbacks += migrate_each_proxying_model(find_proxying_base_models) if opts[:with_proxies]
activate_designs(callbacks) if opts[:activate]
end

Expand All @@ -74,8 +74,8 @@ def find_models
CouchRest::Model::Base.subclasses.reject{|m| m.proxy_owner_method.present?}
end

def find_proxying_models
CouchRest::Model::Base.subclasses.reject{|m| m.proxy_method_names.empty?}
def find_proxying_base_models
CouchRest::Model::Base.subclasses.reject{|m| m.proxy_method_names.empty? || m.proxy_owner_method.present?}
end

def migrate_each_model(models, db = nil)
Expand All @@ -91,12 +91,14 @@ def migrate_each_model(models, db = nil)
def migrate_each_proxying_model(models)
callbacks = [ ]
models.each do |model|
methods = model.proxy_method_names
model_class = model.is_a?(CouchRest::Model::Proxyable::ModelProxy) ? model.model : model
methods = model_class.proxy_method_names
methods.each do |method|
puts "Finding proxied models for #{model}##{method}"
puts "Finding proxied models for #{model_class}##{method}"
model.all.each do |obj|
proxy = obj.send(method)
callbacks += migrate_each_model([proxy.model], proxy.database)
callbacks += migrate_each_proxying_model([proxy]) unless model_class.proxy_method_names.empty?
end
end
end
Expand Down
81 changes: 80 additions & 1 deletion spec/unit/utils/migrate_spec.rb
@@ -1,6 +1,33 @@

require 'spec_helper'

class MigrateModel < CouchRest::Model::Base
use_database :migrations
proxy_database_method :id
proxy_for :migrate_proxy_models
property :name
design { view :by_name }
end

class MigrateProxyModel < CouchRest::Model::Base
proxied_by :migrate_model
proxy_database_method :id
proxy_for :migrate_proxy_nested_models
property :name
design { view :by_name }
end

class MigrateProxyNestedModel < CouchRest::Model::Base
proxied_by :migrate_proxy_model
property :name
design { view :by_name }
end

RSpec::Matchers.define :database_matching do |database|
match do |actual|
actual.server == database.server && actual.name == database.name
end
end

describe CouchRest::Model::Utils::Migrate do

before :each do
Expand All @@ -11,6 +38,7 @@
it "should not do anything if Rails is not available" do
@module.load_all_models
end

it "should detect if Rails is available and require models" do
Rails = double()
allow(Rails).to receive(:root).and_return("")
Expand All @@ -22,4 +50,55 @@
end
end

describe "migrations" do
let!(:stdout) { $stdout }
before :each do
allow(CouchRest::Model::Base).to receive(:subclasses).and_return([MigrateModel, MigrateProxyModel, MigrateProxyNestedModel])
$stdout = StringIO.new
end

after :each do
$stdout = stdout
end

describe "#all_models" do
it "should migrate root subclasses of CouchRest::Model::Base" do
expect(MigrateModel.design_docs.first).to receive(:migrate)
@module.all_models
end

it "shouldn't migrate proxied subclasses with of CouchRest::Model::Base" do
expect(MigrateProxyModel.design_docs.first).not_to receive(:migrate)
expect(MigrateProxyNestedModel.design_docs.first).not_to receive(:migrate)
@module.all_models
end
end

describe "#all_models_and_proxies" do
before :each do
# clear data from previous test runs
MigrateModel.all.each do |mm|
mm.migrate_proxy_models.all.each do |mpm|
mpm.migrate_proxy_nested_models.database.delete! rescue nil
end rescue nil
mm.migrate_proxy_models.database.delete!
mm.destroy
end
MigrateModel.database.recreate!
end

it "should migrate first level proxied subclasses of CouchRest::Model::Base" do
mm = MigrateModel.new(name: "Migration").save
expect(MigrateProxyModel.design_docs.first).to receive(:migrate).with(database_matching(mm.migrate_proxy_models.database))
@module.all_models_and_proxies
end

it "should migrate the second level proxied subclasses of CouchRest::Model::Base" do
mm = MigrateModel.new(name: "Migration").save
mpm = mm.migrate_proxy_models.new(name: "Migration Proxy").save
expect(MigrateProxyNestedModel.design_docs.first).to receive(:migrate).with(database_matching(mpm.migrate_proxy_nested_models.database))
@module.all_models_and_proxies
end
end
end
end

0 comments on commit 7d79c40

Please sign in to comment.