Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add migration_name and named logger to MigrationContext #347

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions lib/kafo/migration_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,35 @@
module Kafo
class MigrationContext < BaseContext

attr_accessor :scenario, :answers
attr_accessor :migration_name, :scenario, :answers

def self.execute(scenario, answers, &migration)
context = new(scenario, answers)
def self.execute(migration_name, scenario, answers, &migration)
context = new(migration_name, scenario, answers)
context.instance_eval(&migration)
return context.scenario, context.answers
end

def initialize(scenario, answers)
def initialize(migration_name, scenario, answers)
@migration_name = migration_name
@scenario = scenario
@answers = answers
end

def self.logger
# Allows us to defer initializing logger or updating its name
# until MigrationContext.logger is called. This is useful
# because most migrations do not log internal state.
logger ||= Kafo::Logger.new(self.migration_name)
logger.name = migration_name if (logger.name != migration_name)
logger
end

def self.migration_name
@migration_name
end

def logger
KafoConfigure.logger
@logger
end
end
end
11 changes: 6 additions & 5 deletions lib/kafo/migrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ def add_migration(name, &block)
end

def run(scenario, answers)
@migrations.keys.sort.each do |name|
KafoConfigure.logger.debug "Executing migration #{name}"
migration = @migrations[name]
scenario, answers = Kafo::MigrationContext.execute(scenario, answers, &migration)
applied << File.basename(name.to_s)
@migrations.keys.sort.each do |filename|
short_name = File.basename(filename.to_s)
KafoConfigure.logger.debug "Executing migration: #{scenario[:name]}::#{short_name}"
migration = @migrations[filename]
scenario, answers = Kafo::MigrationContext.execute(short_name, scenario, answers, &migration)
applied << short_name
end
return scenario, answers
end
Expand Down
10 changes: 5 additions & 5 deletions test/dummy_logger.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
class DummyLogger

attr_accessor :name

LEVELS = %w(fatal error warn info debug)

def initialize
def initialize(name = 'DummyLogger')
LEVELS.each { |l| instance_variable_set("@#{l}", StringIO.new) }
@name = name
end

LEVELS.each do |level|
Expand All @@ -19,8 +23,4 @@ def rewind
def dump_errors
true
end

def name
'DummyLogger'
end
end
3 changes: 2 additions & 1 deletion test/kafo/migration_context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

module Kafo
describe MigrationContext do
let(:context) { MigrationContext.new({}, {}) }
let(:context) { MigrationContext.new("DummyMigration", {}, {}) }

before(:each) { MigrationContext.clear_caches }

describe "api" do
specify { context.respond_to?(:logger) }
specify { context.respond_to?(:scenario) }
specify { context.respond_to?(:answers) }
specify { context.respond_to?(:migration_name) }
specify { context.respond_to?(:facts) }
end

Expand Down
26 changes: 18 additions & 8 deletions test/kafo/migrations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,34 @@
module Kafo
describe Migrations do
let(:migrations) { Migrations.new('some_directory') }
let(:dummy_logger) { DummyLogger.new }
let(:kafo_configure_logger) { DummyLogger.new('KafoConfigureLogger') }
let(:migration_context) { MigrationContext.new('previous_migration', {}, {}) }

describe '#run' do

before do
KafoConfigure.logger = dummy_logger
migrations.add_migration('no1') { logger.error 's1' }
migrations.add_migration('no2') { logger.error 's2' }
KafoConfigure.logger = kafo_configure_logger
migrations.add_migration('m1') do
self.logger.error "#{logger.name} - s1"
end
migrations.add_migration('m2') do
self.logger.error "#{logger.name} - s2"
end
migrations.run({}, {})
end

it 'executes all the migrations' do
dummy_logger.rewind
_(dummy_logger.error.read).must_match(/.*s1.*s2.*/m)
it 'executes all the migrations and logs to MigrationContext logger' do
migration_context_logger.rewind
_(migration_context_logger.error.read).must_match(/.*m1.*s1.*m2.*s2.*/m)
end

it 'MigrationContext does not log to KafoConfigure.logger' do
kafo_configure_logger.rewind
!_(kafo_configure_logger.error.read).must_match(/.*s1.*s2.*/m)
end

it 'knows the applied migrations' do
_(migrations.applied).must_equal ['no1', 'no2']
_(migrations.applied).must_equal ['m1', 'm2']
end
end
end
Expand Down