From 0902c359d5881096c5e7fa70202f0b1e09e23535 Mon Sep 17 00:00:00 2001 From: Ando Date: Thu, 29 Jun 2023 11:08:08 +1000 Subject: [PATCH] Update "force" to be option of Hanami::CLI::Commands::App::DB::Drop Give the DB drop command the option to force drop the Database --- lib/hanami/cli/commands/app/db/drop.rb | 4 ++-- lib/hanami/cli/commands/app/db/utils/database.rb | 2 +- lib/hanami/cli/commands/app/db/utils/postgres.rb | 12 ++++++++++-- lib/hanami/cli/commands/app/db/utils/sqlite.rb | 2 +- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/hanami/cli/commands/app/db/drop.rb b/lib/hanami/cli/commands/app/db/drop.rb index de17bc97..ee8ad55a 100644 --- a/lib/hanami/cli/commands/app/db/drop.rb +++ b/lib/hanami/cli/commands/app/db/drop.rb @@ -12,8 +12,8 @@ class Drop < App::Command desc "Delete database" # @api private - def call(**) - if database.drop_command + def call(force: false) + if database.drop_command(force) out.puts "=> database #{database.name} dropped" else out.puts "=> failed to drop #{database.name}" diff --git a/lib/hanami/cli/commands/app/db/utils/database.rb b/lib/hanami/cli/commands/app/db/utils/database.rb index 883d88db..1f56e758 100644 --- a/lib/hanami/cli/commands/app/db/utils/database.rb +++ b/lib/hanami/cli/commands/app/db/utils/database.rb @@ -68,7 +68,7 @@ def create_command end # @api private - def drop_command + def drop_command(force: false) raise Hanami::CLI::NotImplementedError end diff --git a/lib/hanami/cli/commands/app/db/utils/postgres.rb b/lib/hanami/cli/commands/app/db/utils/postgres.rb index ae435972..cd8a2abe 100644 --- a/lib/hanami/cli/commands/app/db/utils/postgres.rb +++ b/lib/hanami/cli/commands/app/db/utils/postgres.rb @@ -22,8 +22,16 @@ def create_command end # @api private - def drop_command - system(cli_env_vars, "dropdb --force #{escaped_name}") + def drop_command(force: false) + command = force ? "dropdb --force" : "dropdb" + + command = "dropdb" + + if force + command << " --force" + end + + system(cli_env_vars, "#{command} #{escaped_name}") end # @api private diff --git a/lib/hanami/cli/commands/app/db/utils/sqlite.rb b/lib/hanami/cli/commands/app/db/utils/sqlite.rb index c2bc465e..8bff8ee6 100644 --- a/lib/hanami/cli/commands/app/db/utils/sqlite.rb +++ b/lib/hanami/cli/commands/app/db/utils/sqlite.rb @@ -17,7 +17,7 @@ def create_command end # @api private - def drop_command + def drop_command(**) file_path.unlink true end