Skip to content

Commit

Permalink
Refactoring: introduce Hanami::Model::Configuration#migrations_logger…
Browse files Browse the repository at this point in the history
… as private API
  • Loading branch information
jodosha committed Jan 13, 2017
1 parent a7bdd4e commit 0870d26
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 24 deletions.
15 changes: 10 additions & 5 deletions lib/hanami/model/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ class Configuration < ROM::Configuration
# @api private
attr_reader :logger

# @since x.x.x
# @api private
attr_reader :migrations_logger

# @since 0.2.0
# @api private
def initialize(configurator)
super(configurator.backend, configurator.url)
@migrations = configurator._migrations
@schema = configurator._schema
@logger = configurator._logger
@mappings = {}
@entities = {}
@migrations = configurator._migrations
@schema = configurator._schema
@logger = configurator._logger
@migrations_logger = configurator._migrations_logger
@mappings = {}
@entities = {}
end

# NOTE: This must be changed when we want to support several adapters at the time
Expand Down
11 changes: 11 additions & 0 deletions lib/hanami/model/configurator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class Configurator
# @api private
attr_reader :_logger

# @since x.x.x
# @api private
attr_reader :_migrations_logger

# @since 0.7.0
# @api private
def self.build(&block)
Expand Down Expand Up @@ -70,6 +74,13 @@ def logger(stream, options = {})
opts = options.merge(stream: stream)
@_logger = Hanami::Logger.new('hanami.model', opts)
end

# @since x.x.x
# @api private
def migrations_logger(stream = $stdout)
require 'hanami/model/migrator/logger'
@_migrations_logger = Hanami::Model::Migrator::Logger.new(stream)
end
end
end
end
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, stream: $stdout)
def initialize(configuration: self.class.configuration)
@configuration = configuration
@adapter = Adapter.for(configuration, stream)
@adapter = Adapter.for(configuration)
end

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

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

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

# @since 0.7.0
# @api private
def connection_for(configuration, stream)
def connection_for(configuration)
Sequel.connect(
configuration.url,
loggers: [Hanami::Model::Migrator::Logger.new(stream)]
loggers: [configuration.migrations_logger]
)
rescue Sequel::AdapterNotFound
raise MigrationError.new("Current adapter (#{configuration.adapter.type}) doesn't support SQL database operations.")
Expand Down
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, TestIO.stream).dump
Hanami::Model::Migrator::Adapter.for(Hanami::Model.configuration).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, TestIO.stream).dump
Hanami::Model::Migrator::Adapter.for(Hanami::Model.configuration).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, TestIO.stream).dump
Hanami::Model::Migrator::Adapter.for(Hanami::Model.configuration).dump
end

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

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

let(:random) { SecureRandom.hex(4) }

# General variables
let(:migrations) { Pathname.new(__dir__ + '/../fixtures/migrations') }
let(:schema) { nil }
let(:config) { OpenStruct.new(backend: :sql, url: url, _migrations: migrations, _schema: schema) }
let(:config) { OpenStruct.new(backend: :sql, url: url, _migrations: migrations, _schema: schema, _migrations_logger: Hanami::Model::Migrator::Logger.new(ENV['HANAMI_DATABASE_LOGGER'])) }
let(:configuration) { Hanami::Model::Configuration.new(config) }

# Variables for `apply` and `prepare`
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, TestIO.stream).dump
Hanami::Model::Migrator::Adapter.for(configuration).dump

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

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

let(:random) { SecureRandom.hex(4) }

# General variables
let(:migrations) { Pathname.new(__dir__ + '/../fixtures/migrations') }
let(:schema) { nil }
let(:config) { OpenStruct.new(backend: :sql, url: url, _migrations: migrations, _schema: schema) }
let(:config) { OpenStruct.new(backend: :sql, url: url, _migrations: migrations, _schema: schema, _migrations_logger: Hanami::Model::Migrator::Logger.new(ENV['HANAMI_DATABASE_LOGGER'])) }
let(:configuration) { Hanami::Model::Configuration.new(config) }

# Variables for `apply` and `prepare`
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, TestIO.stream).dump
Hanami::Model::Migrator::Adapter.for(configuration).dump

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

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

let(:random) { SecureRandom.hex }

# General variables
let(:migrations) { Pathname.new(__dir__ + '/../fixtures/migrations') }
let(:schema) { nil }
let(:config) { OpenStruct.new(backend: :sql, url: url, _migrations: migrations, _schema: schema) }
let(:config) { OpenStruct.new(backend: :sql, url: url, _migrations: migrations, _schema: schema, _migrations_logger: Hanami::Model::Migrator::Logger.new(ENV['HANAMI_DATABASE_LOGGER'])) }
let(:configuration) { Hanami::Model::Configuration.new(config) }
let(:url) do
db = database
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, TestIO.stream).dump
Hanami::Model::Migrator::Adapter.for(configuration).dump

migration = target_migrations.join('20160831095616_create_abuses.rb')
File.open(migration, 'w+') do |f|
Expand Down
3 changes: 3 additions & 0 deletions test/support/database/strategies/sql.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require_relative 'abstract'
require 'hanami/utils/blank'
require 'pathname'
require 'stringio'

module Database
module Strategies
Expand Down Expand Up @@ -29,6 +30,8 @@ def configure
logger ENV['HANAMI_DATABASE_LOGGER'], level: :debug
migrations Dir.pwd + '/test/fixtures/database_migrations'
schema Dir.pwd + '/tmp/schema.sql'

migrations_logger ENV['HANAMI_DATABASE_LOGGER']
end
end

Expand Down

0 comments on commit 0870d26

Please sign in to comment.