Class: Mongo::Migration
- Inherits:
-
Object
- Object
- Mongo::Migration
- Defined in:
- lib/mongo/migration/migration.rb,
lib/mongo/migration.rb more...
Instance Attribute Summary collapse
- #db ⇒ Object
-
#definitions ⇒ Object
Returns the value of attribute definitions.
Instance Method Summary collapse
- #add(version, &block) ⇒ Object
- #current_version ⇒ Object
-
#initialize ⇒ Migration
constructor
A new instance of Migration.
- #update(version = nil) ⇒ Object
- #update_version(new_version) ⇒ Object
Constructor Details
permalink #initialize ⇒ Migration
Returns a new instance of Migration.
2 3 4 |
# File 'lib/mongo/migration/migration.rb', line 2 def initialize @definitions = {} end |
Instance Attribute Details
Instance Method Details
permalink #add(version, &block) ⇒ Object
[View source]
6 7 8 9 10 11 |
# File 'lib/mongo/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 |
permalink #current_version ⇒ Object
[View source]
29 30 31 32 33 34 35 |
# File 'lib/mongo/migration/migration.rb', line 29 def current_version if doc = db..first(name: 'migration') doc[:version] || doc['version'] else 0 end end |
permalink #update(version = nil) ⇒ Object
[View source]
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/mongo/migration/migration.rb', line 13 def update version = nil version ||= definitions.keys.max version = version.to_i 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 |
permalink #update_version(new_version) ⇒ Object
[View source]
40 41 42 |
# File 'lib/mongo/migration/migration.rb', line 40 def update_version new_version db..update({name: 'migration'}, {name: 'migration', version: new_version}, {upsert: true, safe: true}) end |