Skip to content
Open
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
17 changes: 11 additions & 6 deletions exe/hanami
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ require "hanami/cli"
cli = Dry::CLI.new(Hanami::CLI)
Hanami::CLI::Bundler.require(:cli)

begin
cli.call
rescue Hanami::CLI::Error => exception
$stderr.puts(exception.message) # rubocop:disable Style/StderrPuts
exit(1)
end
code = catch(:exit) {
begin
cli.call
0
rescue Hanami::CLI::Error => exception
$stderr.puts(exception.message) # rubocop:disable Style/StderrPuts
1
end
}

exit(code)
4 changes: 2 additions & 2 deletions lib/hanami/cli/commands/app/db/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Create < DB::Command

option :gateway, required: false, desc: "Use database for gateway"

def call(app: false, slice: nil, gateway: nil, command_exit: method(:exit), **)
def call(app: false, slice: nil, gateway: nil, **)
exit_codes = []

databases(app: app, slice: slice, gateway: gateway).each do |database|
Expand All @@ -27,7 +27,7 @@ def call(app: false, slice: nil, gateway: nil, command_exit: method(:exit), **)
end

exit_codes.each do |code|
break command_exit.(code) if code > 0
throw(:exit, code) if code > 0
end

re_run_development_command_in_test
Expand Down
4 changes: 2 additions & 2 deletions lib/hanami/cli/commands/app/db/drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Drop < DB::Command

option :gateway, required: false, desc: "Use database for gateway"

def call(app: false, slice: nil, gateway: nil, command_exit: method(:exit), **)
def call(app: false, slice: nil, gateway: nil, **)
exit_codes = []

databases(app: app, slice: slice, gateway: gateway).each do |database|
Expand All @@ -27,7 +27,7 @@ def call(app: false, slice: nil, gateway: nil, command_exit: method(:exit), **)
end

exit_codes.each do |code|
break command_exit.(code) if code > 0
throw(:exit, code) if code > 0
end

re_run_development_command_in_test
Expand Down
5 changes: 2 additions & 3 deletions lib/hanami/cli/commands/app/db/migrate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Migrate < DB::Command
default: true,
desc: "Dump the database structure after migrating"

def call(target: nil, app: false, slice: nil, gateway: nil, dump: true, command_exit: method(:exit), **)
def call(target: nil, app: false, slice: nil, gateway: nil, dump: true, **)
databases(app: app, slice: slice, gateway: gateway).each do |database|
if migrations_dir_missing?(database)
warn_on_missing_migrations_dir(database)
Expand All @@ -32,8 +32,7 @@ def call(target: nil, app: false, slice: nil, gateway: nil, dump: true, command_
if dump && !re_running_in_test?
run_command(
Structure::Dump,
app: app, slice: slice, gateway: gateway,
command_exit: command_exit
app: app, slice: slice, gateway: gateway
)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/hanami/cli/commands/app/db/prepare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def call(app: false, slice: nil, **) # rubocop:disable Metrics/AbcSize
nil
end

return exit exit_code if exit_code.to_i > 1
throw(:exit, exit_code) if exit_code.to_i > 1
end

# Once all databases are created, the migrator will properly load for each slice, and
Expand All @@ -54,7 +54,7 @@ def call(app: false, slice: nil, **) # rubocop:disable Metrics/AbcSize
nil
end

return exit exit_code if exit_code.to_i > 1
throw(:exit, exit_code) if exit_code.to_i > 1
end

# Finally, load the seeds for the slice overall, which is a once-per-slice operation.
Expand Down
48 changes: 20 additions & 28 deletions lib/hanami/cli/commands/app/db/rollback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def call(
gateway: nil,
target: nil,
dump: true,
command_exit: method(:exit),
**
)
# We allow either a number of steps or a target migration number to be provided
Expand All @@ -33,7 +32,7 @@ def call(
target = steps if steps && !target && !code_is_number?(steps)
steps_count = steps && code_is_number?(steps) ? Integer(steps) : 1

database = resolve_target_database(app: app, slice: slice, gateway: gateway, command_exit: command_exit)
database = resolve_target_database(app: app, slice: slice, gateway: gateway)
return unless database

migration_code, migration_name = find_migration_target(target, steps_count, database)
Expand Down Expand Up @@ -61,8 +60,7 @@ def call(
Structure::Dump,
app: database.slice == self.app,
slice: database.slice == self.app ? nil : database.slice.slice_name.to_s,
gateway: database.gateway_name == :default ? nil : database.gateway_name.to_s,
command_exit: command_exit
gateway: database.gateway_name == :default ? nil : database.gateway_name.to_s
)
end

Expand All @@ -72,24 +70,23 @@ def call(

private

def resolve_target_database(app:, slice:, gateway:, command_exit:)
def resolve_target_database(app:, slice:, gateway:)
if gateway && !app && !slice
err.puts "When specifying --gateway, an --app or --slice must also be given"
command_exit.(1)
return
throw :exit, 1
end

if slice
resolve_slice_database(slice, gateway, command_exit)
resolve_slice_database(slice, gateway)
elsif app
resolve_app_database(gateway, command_exit)
resolve_app_database(gateway)
else
resolve_default_database(command_exit)
resolve_default_database
end
end

def resolve_slice_database(slice_name, gateway, command_exit)
slice = resolve_slice(slice_name, command_exit)
def resolve_slice_database(slice_name, gateway)
slice = resolve_slice(slice_name)
return unless slice

databases = build_databases(slice)
Expand All @@ -98,43 +95,41 @@ def resolve_slice_database(slice_name, gateway, command_exit)
database = databases[gateway.to_sym]
unless database
err.puts %(No gateway "#{gateway}" found in slice "#{slice_name}")
command_exit.(1)
return
throw :exit, 1
end
database
elsif databases.size == 1
databases.values.first
else
err.puts "Multiple gateways found in slice #{slice_name}. Please specify --gateway option."
command_exit.(1)
throw :exit, 1
end
end

def resolve_app_database(gateway, command_exit)
def resolve_app_database(gateway)
databases = build_databases(app)

if gateway
database = databases[gateway.to_sym]
unless database
err.puts %(No gateway "#{gateway}" found in app)
command_exit.(1)
return
throw :exit, 1
end
database
elsif databases.size == 1
databases.values.first
else
err.puts "Multiple gateways found in app. Please specify --gateway option."
command_exit.(1)
throw :exit, 1
end
end

def resolve_default_database(command_exit)
def resolve_default_database
all_dbs = all_databases

if all_dbs.empty?
err.puts "No databases found"
command_exit.(1)
throw :exit, 1
elsif all_dbs.size == 1
all_dbs.first
else
Expand All @@ -143,24 +138,21 @@ def resolve_default_database(command_exit)
app_databases.values.first
elsif app_databases.size > 1
err.puts "Multiple gateways found in app. Please specify --gateway option."
command_exit.(1)
nil
throw :exit, 1
else
err.puts "Multiple database contexts found. Please specify --app or --slice option."
command_exit.(1)
nil
throw :exit, 1
end
end
end

def resolve_slice(slice_name, command_exit)
def resolve_slice(slice_name)
slice_name_sym = inflector.underscore(Shellwords.shellescape(slice_name)).to_sym
slice = app.slices[slice_name_sym]

unless slice
err.puts %(Slice "#{slice_name}" not found)
command_exit.(1)
return
throw :exit, 1
end

ensure_database_slice(slice)
Expand Down
4 changes: 2 additions & 2 deletions lib/hanami/cli/commands/app/db/structure/dump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Dump < DB::Command
option :gateway, required: false, desc: "Use database for gateway"

# @api private
def call(app: false, slice: nil, gateway: nil, command_exit: method(:exit), **) # rubocop:disable Metrics/AbcSize
def call(app: false, slice: nil, gateway: nil, **) # rubocop:disable Metrics/AbcSize
exit_codes = []

databases(app: app, slice: slice, gateway: gateway).each do |database|
Expand Down Expand Up @@ -48,7 +48,7 @@ def call(app: false, slice: nil, gateway: nil, command_exit: method(:exit), **)
end

exit_codes.each do |code|
break command_exit.(code) if code > 0
throw(:exit, code) if code > 0
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/hanami/cli/commands/app/db/structure/load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Load < DB::Command
option :gateway, required: false, desc: "Use database for gateway"

# @api private
def call(app: false, slice: nil, gateway: nil, command_exit: method(:exit), **) # rubocop:disable Metrics/AbcSize
def call(app: false, slice: nil, gateway: nil, **) # rubocop:disable Metrics/AbcSize
exit_codes = []

databases(app: app, slice: slice, gateway: gateway).each do |database|
Expand All @@ -42,7 +42,7 @@ def call(app: false, slice: nil, gateway: nil, command_exit: method(:exit), **)
end

exit_codes.each do |code|
break command_exit.(code) if code > 0
throw(:exit, code) if code > 0
end

re_run_development_command_in_test
Expand Down
7 changes: 1 addition & 6 deletions lib/hanami/cli/commands/app/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ class Run < Hanami::CLI::Command

argument :code_or_path, required: true, desc: "Path to a Ruby file or inline Ruby code to be executed"

def initialize(command_exit: method(:exit), **opts)
super(**opts)
@command_exit = command_exit
end

# rubocop:disable Metrics/AbcSize
def call(code_or_path:, **)
require "hanami/prepare"
Expand All @@ -60,7 +55,7 @@ def call(code_or_path:, **)
end
end
rescue RunError
@command_exit.call(1)
throw :exit, 1
end
# rubocop:enable Metrics/AbcSize

Expand Down
25 changes: 12 additions & 13 deletions spec/unit/hanami/cli/commands/app/db/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@

let(:system_call) { Hanami::CLI::SystemCall.new }
let(:test_env_executor) { instance_spy(Hanami::CLI::InteractiveSystemCall) }
let(:exit_double) { double(:exit_method) }

let(:out) { StringIO.new }
def output = out.string

before do
# Prevent the command from exiting the spec run in the case of unexpected system call failures
allow(command).to receive(:exit)
allow(exit_double).to receive(:call)
end

before do
@env = ENV.to_h
allow(Hanami::Env).to receive(:loaded?).and_return(false)
Expand Down Expand Up @@ -95,7 +88,7 @@ def before_prepare
end

it "does not create the database if it already exists" do
command.run_command(Hanami::CLI::Commands::App::DB::Create, command_exit: exit_double)
command.run_command(Hanami::CLI::Commands::App::DB::Create)
out.truncate(0)

command.call
Expand All @@ -120,7 +113,7 @@ def before_prepare
end

it "does not create the database if it already exists" do
command.run_command(Hanami::CLI::Commands::App::DB::Create, command_exit: exit_double)
command.run_command(Hanami::CLI::Commands::App::DB::Create)
out.truncate(0)

command.call
Expand Down Expand Up @@ -190,7 +183,10 @@ def before_prepare
.with(a_string_matching(/sqlite3.+app.sqlite3/))
.and_return Hanami::CLI::SystemCall::Result.new(exit_code: 2, out: "", err: "app-db-err")

command.call
exit_code = catch(:exit) {
command.call
0
}

expect { Main::Slice["db.gateway"] }.not_to raise_error

Expand All @@ -202,7 +198,7 @@ def before_prepare

expect(output).to include "database db/main.sqlite3 created"

expect(command).to have_received(:exit).with(2).once
expect(exit_code).to eq(2)
end

context "app with gateways" do
Expand Down Expand Up @@ -283,7 +279,10 @@ def before_prepare
.with(a_string_matching(/createdb.+_app/), anything)
.and_return Hanami::CLI::SystemCall::Result.new(exit_code: 2, out: "", err: "app-db-err")

command.call
exit_code = catch(:exit) {
command.call
0
}

expect { Hanami.app["db.gateway"] }.to raise_error Sequel::DatabaseConnectionError
expect { Main::Slice["db.gateway"] }.not_to raise_error
Expand All @@ -293,7 +292,7 @@ def before_prepare

expect(output).to include "database #{POSTGRES_BASE_DB_NAME}_main created"

expect(command).to have_received(:exit).with(2).once
expect(exit_code).to eq(2)
end
end

Expand Down
Loading
Loading