Module: MongoMapper::Migration

Defined in:
lib/mongo_mapper_ext/mongo_mapper/migration.rb

Defined Under Namespace

Classes: Logger, MigrationDefinition

Class Method Summary collapse

Class Method Details

.define(database_alias, version, &block) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/mongo_mapper_ext/mongo_mapper/migration.rb', line 41

def define database_alias, version, &block
  database_alias = database_alias.to_s
  
  raise "version should be an Integer! (but you provided '#{version}' instad)!" unless version.is_a? Integer
  definition = MigrationDefinition.new
  block.call definition
  definitions[database_alias][version] = definition
end

.definitionsObject



71
72
73
# File 'lib/mongo_mapper_ext/mongo_mapper/migration.rb', line 71

def definitions
  @definitions ||= Hash.new{|h, k| h[k] = []} 
end

.loggerObject



39
# File 'lib/mongo_mapper_ext/mongo_mapper/migration.rb', line 39

def logger; @logger ||= Logger.new end

.logger=(logger) ⇒ Object



35
36
37
# File 'lib/mongo_mapper_ext/mongo_mapper/migration.rb', line 35

def logger= logger
  @logger = logger ? logger : Logger.new(false)
end

.metadata(db) ⇒ Object



66
67
68
69
# File 'lib/mongo_mapper_ext/mongo_mapper/migration.rb', line 66

def  db
  col = db.collection 'db_metadata'
  col.find_one || {'version' => 0}
end

.update(database_alias, version) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mongo_mapper_ext/mongo_mapper/migration.rb', line 50

def update database_alias, version  
  database_alias = database_alias.to_s
  db = MongoMapper.databases[database_alias]
  
  if (db)['version'] == version
    logger.info "Database '#{database_alias}' already is of #{version} version, no migration needed."
    return false
  else
    logger.info "Migration for '#{database_alias}' Database:"
  end
  
  increase_db_version database_alias, db while (db)['version'] < version
  decrease_db_version database_alias, db while (db)['version'] > version
  true
end