Class: Mongo::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_db/migration/migration.rb,
lib/mongo_db/migration.rb

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ Migration

Returns a new instance of Migration.



2
3
4
# File 'lib/mongo_db/migration/migration.rb', line 2

def initialize db
  @db, @definitions = db, {}
end

Instance Method Details

#add(version, &block) ⇒ Object



6
7
8
9
10
11
# File 'lib/mongo_db/migration/migration.rb', line 6

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

#current_versionObject



28
29
30
31
32
33
34
# File 'lib/mongo_db/migration/migration.rb', line 28

def current_version
  if doc = db..first(name: 'migration')
    doc[:version] || doc['version']
  else
    0
  end
end

#update(version = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/mongo_db/migration/migration.rb', line 13

def update version = nil
  version ||= definitions.keys.max

  if current_version == version
    info "database '#{db.name}' already is of #{version} version, no migration needed"
    return false
  else
    info "updating '#{db.name}' to #{version}"
  end

  increase_db_version while current_version < version
  decrease_db_version while current_version > version
  true
end