Skip to content

Commit

Permalink
Automatic logging to stdout when running migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jan 13, 2017
1 parent 0298ea6 commit eda8737
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/hanami/model/migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ def self.version
#
# @since 0.7.0
# @api private
def initialize(configuration: self.class.configuration)
def initialize(configuration: self.class.configuration, stream: $stdout)
@configuration = configuration
@adapter = Adapter.for(configuration)
@adapter = Adapter.for(configuration, stream)
end

# @since 0.7.0
Expand Down
10 changes: 6 additions & 4 deletions lib/hanami/model/migrator/adapter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'uri'
require 'shellwords'
require 'hanami/model/migrator/logger'

module Hanami
module Model
Expand All @@ -25,8 +26,8 @@ class Adapter
#
# @since 0.4.0
# @api private
def self.for(configuration) # rubocop:disable Metrics/MethodLength
connection = connection_for(configuration)
def self.for(configuration, stream) # rubocop:disable Metrics/MethodLength
connection = connection_for(configuration, stream)

case connection.database_type
when :sqlite
Expand All @@ -48,9 +49,10 @@ class << self

# @since 0.7.0
# @api private
def connection_for(configuration)
def connection_for(configuration, stream)
Sequel.connect(
configuration.url
configuration.url,
loggers: [Hanami::Model::Migrator::Logger.new(stream)]
)
rescue Sequel::AdapterNotFound
raise MigrationError.new("Current adapter (#{configuration.adapter.type}) doesn't support SQL database operations.")
Expand Down
33 changes: 33 additions & 0 deletions lib/hanami/model/migrator/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'hanami/logger'

module Hanami
module Model
class Migrator
# Automatic logger for migrations
#
# @since x.x.x
# @api private
class Logger < Hanami::Logger
# Formatter for migrations logger
#
# @since x.x.x
# @api private
class Formatter < Hanami::Logger::Formatter
private

# @since x.x.x
# @api private
def _format(hash)
"[migration] [#{hash.fetch(:severity)}] #{hash.fetch(:message)}\n"
end
end

# @since x.x.x
# @api private
def initialize(stream)
super(nil, stream: stream, formatter: Formatter.new)
end
end
end
end
end
2 changes: 1 addition & 1 deletion test/integration/migration/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@schema = Pathname.new("#{__dir__}/../../../tmp/schema.sql").expand_path
@connection = Sequel.connect(ENV['HANAMI_DATABASE_URL'])

Hanami::Model::Migrator::Adapter.for(Hanami::Model.configuration).dump
Hanami::Model::Migrator::Adapter.for(Hanami::Model.configuration, TestIO.stream).dump
end

describe 'columns' do
Expand Down
2 changes: 1 addition & 1 deletion test/integration/migration/postgresql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@schema = Pathname.new("#{__dir__}/../../../tmp/schema.sql").expand_path
@connection = Sequel.connect(ENV['HANAMI_DATABASE_URL'])

Hanami::Model::Migrator::Adapter.for(Hanami::Model.configuration).dump
Hanami::Model::Migrator::Adapter.for(Hanami::Model.configuration, TestIO.stream).dump
end

describe 'columns' do
Expand Down
2 changes: 1 addition & 1 deletion test/integration/migration/sqlite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@schema = Pathname.new("#{__dir__}/../../../tmp/schema.sql").expand_path
@connection = Sequel.connect(ENV['HANAMI_DATABASE_URL'])

Hanami::Model::Migrator::Adapter.for(Hanami::Model.configuration).dump
Hanami::Model::Migrator::Adapter.for(Hanami::Model.configuration, TestIO.stream).dump
end

describe 'columns' do
Expand Down
4 changes: 2 additions & 2 deletions test/migrator/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe 'MySQL Database migrations' do
let(:migrator) do
Hanami::Model::Migrator.new(configuration: configuration)
Hanami::Model::Migrator.new(configuration: configuration, stream: TestIO.stream)
end

let(:random) { SecureRandom.hex(4) }
Expand Down Expand Up @@ -250,7 +250,7 @@
it 'creates database, loads schema and migrate' do
# Simulate already existing schema.sql, without existing database and pending migrations
connection = Sequel.connect(url)
Hanami::Model::Migrator::Adapter.for(configuration).dump
Hanami::Model::Migrator::Adapter.for(configuration, TestIO.stream).dump

migration = target_migrations.join('20160831095616_create_abuses.rb')
File.open(migration, 'w+') do |f|
Expand Down
4 changes: 2 additions & 2 deletions test/migrator/postgresql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe 'PostgreSQL Database migrations' do
let(:migrator) do
Hanami::Model::Migrator.new(configuration: configuration)
Hanami::Model::Migrator.new(configuration: configuration, stream: TestIO.stream)
end

let(:random) { SecureRandom.hex(4) }
Expand Down Expand Up @@ -298,7 +298,7 @@
it 'creates database, loads schema and migrate' do
# Simulate already existing schema.sql, without existing database and pending migrations
connection = Sequel.connect(url)
Hanami::Model::Migrator::Adapter.for(configuration).dump
Hanami::Model::Migrator::Adapter.for(configuration, TestIO.stream).dump

migration = target_migrations.join('20160831095616_create_abuses.rb')
File.open(migration, 'w+') do |f|
Expand Down
4 changes: 2 additions & 2 deletions test/migrator/sqlite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe 'Filesystem SQLite Database migrations' do
let(:migrator) do
Hanami::Model::Migrator.new(configuration: configuration)
Hanami::Model::Migrator.new(configuration: configuration, stream: TestIO.stream)
end

let(:random) { SecureRandom.hex }
Expand Down Expand Up @@ -236,7 +236,7 @@
it 'creates database, loads schema and migrate' do
# Simulate already existing schema.sql, without existing database and pending migrations
connection = Sequel.connect(url)
Hanami::Model::Migrator::Adapter.for(configuration).dump
Hanami::Model::Migrator::Adapter.for(configuration, TestIO.stream).dump

migration = target_migrations.join('20160831095616_create_abuses.rb')
File.open(migration, 'w+') do |f|
Expand Down
6 changes: 4 additions & 2 deletions test/support/database/strategies/sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ def after
end

def migrate
require 'hanami/model/migrator'
Hanami::Model::Migrator.migrate
TestIO.with_stdout do
require 'hanami/model/migrator'
Hanami::Model::Migrator.migrate
end
end

def credentials
Expand Down
14 changes: 14 additions & 0 deletions test/support/test_io.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module TestIO
def self.with_stdout
stdout = $stdout
$stdout = stream
yield
ensure
$stdout.close
$stdout = stdout
end

def self.stream
File.new(ENV['HANAMI_DATABASE_LOGGER'], "a+")
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
$LOAD_PATH.unshift 'lib'
require 'hanami/model'

require_relative './support/test_io'
require_relative './support/platform'
require_relative './support/database'
require_relative './support/fixtures'

0 comments on commit eda8737

Please sign in to comment.