Class: CouchRest::Model::Migrate

Inherits:
Object
  • Object
show all
Defined in:
lib/couchrest/model/migrate.rb

Overview

Handle CouchDB migrations.

Actual migrations are handled by the Design document, this serves as a utility to find all the CouchRest Model submodels and perform the migration on them.

Also contains some more advanced support for handling proxied models.

Examples of usage:

# Ensure all models have been loaded (only Rails)
CouchRest::Model::Migrate.load_all_models

# Migrate all regular models (not proxied)
CouchRest::Model::Migrate.all_models

# Migrate all models and submodels of proxies
CouchRest::Model::Migrate.all_models_and_proxies

Class Method Summary (collapse)

Class Method Details

+ (Object) all_models



24
25
26
27
# File 'lib/couchrest/model/migrate.rb', line 24

def self.all_models
  callbacks = migrate_each_model(find_models)
  cleanup(callbacks)
end

+ (Object) all_models_and_proxies



29
30
31
32
33
# File 'lib/couchrest/model/migrate.rb', line 29

def self.all_models_and_proxies
  callbacks = migrate_each_model(find_models)
  callbacks += migrate_each_proxying_model(find_proxying_models)
  cleanup(callbacks)
end

+ (Object) cleanup(methods)



82
83
84
85
86
87
88
# File 'lib/couchrest/model/migrate.rb', line 82

def self.cleanup(methods)
  methods.compact.each do |cb|
    name = "/#{cb[:db].name}/#{cb[:design]['_id']}"
    puts "Activating new design: #{name}"
    cb[:proc].call
  end
end

+ (Object) find_models



43
44
45
# File 'lib/couchrest/model/migrate.rb', line 43

def self.find_models
  CouchRest::Model::Base.subclasses.reject{|m| m.proxy_owner_method.present?}
end

+ (Object) find_proxying_models



47
48
49
# File 'lib/couchrest/model/migrate.rb', line 47

def self.find_proxying_models
  CouchRest::Model::Base.subclasses.reject{|m| m.proxy_database_method.blank?}
end

+ (Object) load_all_models



35
36
37
38
39
40
41
# File 'lib/couchrest/model/migrate.rb', line 35

def self.load_all_models
  # Make a reasonable effort to load all models
  return unless defined?(Rails)
  Dir[Rails.root + 'app/models/**/*.rb'].each do |path|
    require path
  end
end

+ (Object) migrate_design(model, design, db = nil)



73
74
75
76
77
78
79
80
# File 'lib/couchrest/model/migrate.rb', line 73

def self.migrate_design(model, design, db = nil)
  print "Migrating #{model.to_s}##{design.method_name}... "
  callback = design.migrate(db) do |result|
    puts "#{result.to_s.gsub(/_/, ' ')}"
  end
  # Return the callback hash if there is one
  callback ? {:design => design, :proc => callback, :db => db || model.database} : nil
end

+ (Object) migrate_each_model(models, db = nil)



51
52
53
54
55
56
57
58
59
# File 'lib/couchrest/model/migrate.rb', line 51

def self.migrate_each_model(models, db = nil)
  callbacks = [ ]
  models.each do |model|
    model.design_docs.each do |design|
      callbacks << migrate_design(model, design, db)
    end
  end
  callbacks
end

+ (Object) migrate_each_proxying_model(models)



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/couchrest/model/migrate.rb', line 61

def self.migrate_each_proxying_model(models)
  callbacks = [ ]
  models.each do |model|
    submodels = model.proxied_model_names.map{|n| n.constantize}
    model.all.each do |base|
      puts "Finding proxied models for #{model}: \"#{base.send(model.proxy_database_method)}\""
      callbacks += migrate_each_model(submodels, base.proxy_database)
    end
  end
  callbacks
end