lib/mongo_mapper_ext/migration.rb in rails-ext-0.2.21 vs lib/mongo_mapper_ext/migration.rb in rails-ext-0.3

- old
+ new

@@ -17,11 +17,29 @@ end end alias_method :down, :downgrade end + class Logger + attr_accessor :enabled + + def initialize enabled = true + self.enabled = enabled + end + + def info message + puts message if enabled + end + end + class << self + def logger= logger + @logger = logger ? logger : Logger.new(false) + end + + def logger; @logger ||= Logger.new end + def define database_alias, version, &block database_alias = database_alias.to_s version.should! :be_a, Integer definition = MigrationDefinition.new @@ -32,14 +50,14 @@ def update database_alias, version database_alias = database_alias.to_s db = MongoMapper.databases[database_alias] if metadata(db).version == version - puts "Database '#{database_alias}' already is of #{version} version, no migration needed." + logger.info "Database '#{database_alias}' already is of #{version} version, no migration needed." return false else - puts "Migration for '#{database_alias}' Database:" + logger.info "Migration for '#{database_alias}' Database:" end increase_db_version database_alias, db while metadata(db).version < version decrease_db_version database_alias, db while metadata(db).version > version true @@ -63,11 +81,11 @@ migration.up.call db m.version += 1 update_metadata db, m - puts "Database '#{database_alias}' upgraded to version #{m.version}." + logger.info "Database '#{database_alias}' upgraded to version #{m.version}." end def decrease_db_version database_alias, db m = metadata(db) migration = definitions[database_alias][m.version] @@ -76,10 +94,10 @@ migration.down.call db m.version -= 1 update_metadata db, m - puts "Database '#{database_alias}' downgraded to version #{m.version}." + logger.info "Database '#{database_alias}' downgraded to version #{m.version}." end def update_metadata db, metadata col = db.collection 'db_metadata' \ No newline at end of file