Class: Mongo::Migration
- Inherits:
-
Object
- Object
- Mongo::Migration
- Defined in:
- lib/mongo_migration/migration.rb,
lib/mongo_migration.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
Returns the value of attribute adapter.
Instance Method Summary collapse
- #define(version, database_name = :default, &block) ⇒ Object
- #definitions ⇒ Object
-
#initialize(adapter = nil) ⇒ Migration
constructor
A new instance of Migration.
- #metadata(db) ⇒ Object
- #update(version, database_name = :default) ⇒ Object
Constructor Details
#initialize(adapter = nil) ⇒ Migration
Returns a new instance of Migration.
4 5 6 |
# File 'lib/mongo_migration/migration.rb', line 4 def initialize adapter = nil @adapter = adapter end |
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
2 3 4 |
# File 'lib/mongo_migration/migration.rb', line 2 def adapter @adapter end |
Instance Method Details
#define(version, database_name = :default, &block) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/mongo_migration/migration.rb', line 8 def define version, database_name = :default, &block raise "version should be an Integer! (but you provided '#{version}' instad)!" unless version.is_a? Integer definition = Definition.new block.call definition definitions[database_name][version] = definition end |
#definitions ⇒ Object
35 36 37 |
# File 'lib/mongo_migration/migration.rb', line 35 def definitions @definitions ||= Hash.new{|h, k| h[k] = []} end |
#metadata(db) ⇒ Object
30 31 32 33 |
# File 'lib/mongo_migration/migration.rb', line 30 def db col = db.collection 'db_metadata' col.find_one || {'version' => 0} end |
#update(version, database_name = :default) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/mongo_migration/migration.rb', line 15 def update version, database_name = :default db = adapter.database database_name if (db)['version'] == version adapter.logger.info "Database '#{database_name}' already is of #{version} version, no migration needed" return false else adapter.logger.info "Migration for '#{database_name}', updating to #{version}" end increase_db_version database_name, db while (db)['version'] < version decrease_db_version database_name, db while (db)['version'] > version true end |