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
1 change: 0 additions & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ Metrics/AbcSize:
- 'lib/hanami/cli/commands/app/db/prepare.rb'
- 'lib/hanami/cli/commands/app/db/rollback.rb'
- 'lib/hanami/cli/commands/app/db/structure/dump.rb'
- 'lib/hanami/cli/commands/app/db/structure/load.rb'
- 'lib/hanami/cli/commands/app/db/utils/postgres.rb'
- 'lib/hanami/cli/generators/app/slice.rb'

Expand Down
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 @@ -16,9 +16,9 @@ def call(app: false, slice: nil, gateway: nil, command_exit: method(:exit), **)

databases(app: app, slice: slice, gateway: gateway).each do |database|
result = database.exec_create_command
exit_codes << result.exit_code if result.respond_to?(:exit_code)
exit_codes << result.exit_code

if result == true || result.successful?
if result.successful?
out.puts "=> database #{database.name} created"
else
out.puts "=> failed to create database #{database.name}"
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 @@ -16,9 +16,9 @@ def call(app: false, slice: nil, gateway: nil, command_exit: method(:exit), **)

databases(app: app, slice: slice, gateway: gateway).each do |database|
result = database.exec_drop_command
exit_codes << result.exit_code if result.respond_to?(:exit_code)
exit_codes << result.exit_code

if result == true || result.successful?
if result.successful?
out.puts "=> database #{database.name} dropped"
else
out.puts "=> failed to drop database #{database.name}"
Expand Down
2 changes: 1 addition & 1 deletion lib/hanami/cli/commands/app/db/structure/dump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def call(app: false, slice: nil, gateway: nil, command_exit: method(:exit), **)
measure("#{database.name} structure dumped to #{relative_structure_path}") do
catch :dump_failed do
result = database.structure_sql_dump
exit_codes << result.exit_code if result.respond_to?(:exit_code)
exit_codes << result.exit_code

unless result.successful?
out.puts result.err
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, command_exit: method(:exit), **)
exit_codes = []

databases(app: app, slice: slice, gateway: gateway).each do |database|
Expand All @@ -29,7 +29,7 @@ def call(app: false, slice: nil, gateway: nil, command_exit: method(:exit), **)
measure("#{database.name} structure loaded from #{relative_structure_path}") do
catch :load_failed do
result = database.exec_load_command
exit_codes << result.exit_code if result.respond_to?(:exit_code)
exit_codes << result.exit_code

unless result.successful?
out.puts result.err
Expand Down
8 changes: 6 additions & 2 deletions lib/hanami/cli/commands/app/db/utils/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ module Utils
class Mysql < Database
# @api private
def exec_create_command
return true if exists?
return success_result if exists?

exec_cli("mysql", %(-e "CREATE DATABASE #{escaped_name}"))
end

# @api private
# @since 2.2.0
def exec_drop_command
return true unless exists?
return success_result unless exists?

exec_cli("mysql", %(-e "DROP DATABASE #{escaped_name}"))
end
Expand Down Expand Up @@ -56,6 +56,10 @@ def exec_load_command

private

def success_result
@success_result ||= SystemCall::Result.new(exit_code: 0, out: "", err: "")
end

def escaped_name
Shellwords.escape(name)
end
Expand Down
8 changes: 6 additions & 2 deletions lib/hanami/cli/commands/app/db/utils/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ class Postgres < Database
# @api private
# @since 2.2.0
def exec_create_command
return true if exists?
return success_result if exists?

system_call.call("createdb #{escaped_name}", env: cli_env_vars)
end

# @api private
# @since 2.2.0
def exec_drop_command
return true unless exists?
return success_result unless exists?

system_call.call("dropdb #{escaped_name}", env: cli_env_vars)
end
Expand Down Expand Up @@ -76,6 +76,10 @@ def schema_migrations_sql_dump

private

def success_result
@success_result ||= SystemCall::Result.new(exit_code: 0, out: "", err: "")
end

def post_process_dump(sql)
sql.lines.reject do |line|
SCHEMA_DUMP_FILTERS.any? { |filter| line =~ filter }
Expand Down
25 changes: 17 additions & 8 deletions lib/hanami/cli/commands/app/db/utils/sqlite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,28 @@ module Utils
class Sqlite < Database
# @api private
# @since 2.2.0
Failure = Struct.new(:err) do
def successful?
false
class Failure
def initialize(err)
@err = err
end

def exit_code
1
end
attr_reader :err

def successful? = false
def exit_code = 1
end

# @api private
# @since 2.2.0
class Success
def successful? = true
def exit_code = 0
end

# @api private
# @since 2.2.0
def exec_create_command
return true if exists?
return Success.new if exists?

FileUtils.mkdir_p(File.dirname(file_path))

Expand All @@ -43,7 +51,8 @@ def exec_drop_command
return Failure.new(exception.message)
end

true
# Mimic a system_call result
Success.new
end

# @api private
Expand Down
Loading