lib/mongo_mapper_ext/migration.rb in rails-ext-0.2.17 vs lib/mongo_mapper_ext/migration.rb in rails-ext-0.2.18

- old
+ new

@@ -18,92 +18,73 @@ end alias_method :down, :downgrade end class << self - def define_migration version, &block + def define database_alias, version, &block + database_alias = database_alias.to_s + version.should! :be_a, Integer definition = MigrationDefinition.new block.call definition - migration_definitions[version] = definition + definitions[database_alias][version] = definition end - - def define_global_migration version, &block - version.should! :be_a, Integer - definition = MigrationDefinition.new - block.call definition - global_migration_definitions[version] = definition - end - - def update_global_database_to version - puts "Migration for Global Database:" - update_database MongoMapper.global_database, global_migration_definitions, version - end - - def update_database_to version - puts "Migration for Accounts Databases:" - results = {} - MongoMapper.accounts_names.each do |account_name| - MongoMapper.account_database account_name do |db| - result = update_database db, migration_definitions, version - results[db.name] = result - end + + def update database_alias, version + database_alias = database_alias.to_s + db = MongoMapper.databases[database_alias] + + if metadata(db).version == version + MongoMapper.logger.info "Database #{database_alias} already is of #{version} version, no migration needed." + return false + else + MongoMapper.logger.info "Migration for '#{database_alias}' Database:" end - results + + increase_db_version database_alias, db while metadata(db).version < version + decrease_db_version database_alias, db while metadata(db).version > version + true end - + def metadata db col = db.collection 'db_metadata' metadata = (col.find_one || {:version => 0}).to_openobject end - def global_migration_definitions; @global_migration_definitions ||= [] end - def migration_definitions; @migration_definitions ||= [] end + def definitions + @definitions ||= Hash.new{|h, k| h[k] = []} + end protected - def increase_db_version migration_definitions, db + def increase_db_version database_alias, db m = metadata(db) - migration = migration_definitions[m.version + 1] - raise "No upgrade for version #{m.version + 1}" unless migration and migration.up + migration = definitions[database_alias][m.version + 1] + raise "No upgrade for version #{m.version + 1} of #{database_alias} Database!" unless migration and migration.up - migration.up.call + migration.up.call db - m.version += 1 + m.version += 1 update_metadata db, m - puts "Database #{db.name} upgraded to version #{m.version}" + MongoMapper.logger.info "Database #{database_alias} upgraded to version #{m.version}." end - def decrease_db_version migration_definitions, db + def decrease_db_version database_alias, db m = metadata(db) - migration = migration_definitions[m.version] - raise "No downgrade for version #{m.version}" unless migration and migration.down + migration = definitions[database_alias][m.version] + raise "No downgrade for version #{m.version} of #{database_alias} Database!" unless migration and migration.down - migration.down.call + migration.down.call db m.version -= 1 update_metadata db, m - puts "Database #{db.name} downgraded to version #{m.version}" + MongoMapper.logger.info "Database #{database_alias} downgraded to version #{m.version}." end - - def update_database db, migrations, version - if metadata(db).version == version - puts "Database #{db.name} already is of #{version} version, no migration needed" - return false - end - increase_db_version migrations, db while metadata(db).version < version - decrease_db_version migrations, db while metadata(db).version > version - true - end def update_metadata db, metadata col = db.collection 'db_metadata' col.save metadata.to_hash - end - - def reset - [global_migration_definitions, migration_definitions].every.clear end end end \ No newline at end of file