From 18715064f83d3cb709194932efc7399125662f1e Mon Sep 17 00:00:00 2001 From: Ando Date: Tue, 4 Apr 2023 13:17:14 +1000 Subject: [PATCH 1/3] Update psql drop_command to force dropdb The reason behind it is to improve developer experience, as right now when running a command like `hanami db reset` it will fail if the user has any other ongoing connection, like a connection to db client(like dbeaver), or a `hanami console` session. --- lib/hanami/cli/commands/app/db/utils/postgres.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/hanami/cli/commands/app/db/utils/postgres.rb b/lib/hanami/cli/commands/app/db/utils/postgres.rb index 0f469536..ae435972 100644 --- a/lib/hanami/cli/commands/app/db/utils/postgres.rb +++ b/lib/hanami/cli/commands/app/db/utils/postgres.rb @@ -23,7 +23,7 @@ def create_command # @api private def drop_command - system(cli_env_vars, "dropdb #{escaped_name}") + system(cli_env_vars, "dropdb --force #{escaped_name}") end # @api private From 0fec2823285abc3de0c49e9a648dcb96c34223d3 Mon Sep 17 00:00:00 2001 From: Ando Date: Thu, 29 Jun 2023 11:08:08 +1000 Subject: [PATCH 2/3] 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 | 6 ++++-- lib/hanami/cli/commands/app/db/utils/sqlite.rb | 2 +- 4 files changed, 8 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..51d4f7e2 100644 --- a/lib/hanami/cli/commands/app/db/utils/postgres.rb +++ b/lib/hanami/cli/commands/app/db/utils/postgres.rb @@ -22,8 +22,10 @@ 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" + + 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 From af1c795b3c6019fb0fc435966f22e6e51afb3a9b Mon Sep 17 00:00:00 2001 From: Ando Date: Thu, 29 Jun 2023 12:05:16 +1000 Subject: [PATCH 3/3] Fix typo on database.drop_command --- lib/hanami/cli/commands/app/db/drop.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/hanami/cli/commands/app/db/drop.rb b/lib/hanami/cli/commands/app/db/drop.rb index ee8ad55a..b39a815f 100644 --- a/lib/hanami/cli/commands/app/db/drop.rb +++ b/lib/hanami/cli/commands/app/db/drop.rb @@ -13,7 +13,7 @@ class Drop < App::Command # @api private def call(force: false) - if database.drop_command(force) + if database.drop_command(force:) out.puts "=> database #{database.name} dropped" else out.puts "=> failed to drop #{database.name}"