Skip to content

Commit

Permalink
Use logger instead of puts in non-CLI code
Browse files Browse the repository at this point in the history
  • Loading branch information
juanedi committed Jul 2, 2016
1 parent 30afef4 commit 8cf5237
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
20 changes: 16 additions & 4 deletions src/micrate.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require "./micrate/*"

module Micrate
@@logger : Logger?

def self.db_dir
"db"
end
Expand Down Expand Up @@ -93,11 +95,11 @@ module Micrate
plan = migration_plan(status, current, target, direction)

if plan.empty?
puts "No migrations to run. current version: #{current}"
logger.info "No migrations to run. current version: #{current}"
return
end

puts "Migrating db, current version: #{current}, target: #{target}"
logger.info "Migrating db, current version: #{current}, target: #{target}"

plan.each do |version|
migration = all_migrations[version]
Expand All @@ -108,9 +110,9 @@ module Micrate

DB.record_migration(migration, direction, db)

puts "OK #{migration.name}"
logger.info "OK #{migration.name}"
rescue e : Exception
puts "An error ocurred executing migration #{migration.version}. Error message is: #{e.message}"
logger.info "An error ocurred executing migration #{migration.version}. Error message is: #{e.message}"
return
end
end
Expand Down Expand Up @@ -193,6 +195,16 @@ module Micrate
return 0
end

def self.logger
@@logger ||= Logger.new(STDOUT).tap do |l|
l.level = Logger::UNKNOWN
end
end

def self.logger=(logger)
@@logger = logger
end

class UnorderedMigrationsException < Exception

getter :versions
Expand Down
29 changes: 20 additions & 9 deletions src/micrate/cli.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "logger"

module Micrate
module Cli
def self.run_up
Expand Down Expand Up @@ -53,13 +55,13 @@ module Micrate
conflicting.each do |version|
puts " #{Migration.from_version(version).name}"
end
puts
puts "Micrate will not run these migrations because they may have been written with an older database model in mind."
puts "You should probably check if they need to be updated and rename them so they are considered a newer version."
puts "
Micrate will not run these migrations because they may have been written with an older database model in mind.
You should probably check if they need to be updated and rename them so they are considered a newer version."
end

def self.help
"micrate is a database migration management system for Crystal projects, *heavily* inspired by Goose (https://bitbucket.org/liamstask/goose/).
def self.print_help
puts "micrate is a database migration management system for Crystal projects, *heavily* inspired by Goose (https://bitbucket.org/liamstask/goose/).
Usage:
micrate [options] <subcommand> [subcommand options]
Expand All @@ -70,13 +72,14 @@ Commands:
redo Re-run the latest migration
status dump the migration status for the current DB
create Create the scaffolding for a new migration
dbversion Print the current version of the database
"
dbversion Print the current version of the database"
end

def self.run
setup_logger

if ARGV.empty?
puts help
print_help
return
end

Expand All @@ -95,7 +98,7 @@ Commands:
when "dbversion"
run_dbversion
else
puts help
print_help
end
rescue e: UnorderedMigrationsException
report_unordered_migrations(e.versions)
Expand All @@ -104,7 +107,15 @@ Commands:
puts e.message
exit 1
end
end

def self.setup_logger
Micrate.logger = Logger.new(STDOUT).tap do |l|
l.level = Logger::INFO
l.formatter = Logger::Formatter.new do |severity, datetime, progname, message, io|
io << message
end
end
end
end
end
Expand Down

0 comments on commit 8cf5237

Please sign in to comment.