From 5de0d32946303972d46078b3c12584bd08e3ddb5 Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Mon, 26 Oct 2020 09:43:05 +0100 Subject: [PATCH 1/3] Support force dropping a database This is introduced in PostgreSQL 13 (https://www.postgresql.org/docs/current/sql-dropdatabase.html) and will be enabled when `:force_drop` option is enabled. --- lib/ecto/adapters/postgres.ex | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/ecto/adapters/postgres.ex b/lib/ecto/adapters/postgres.ex index 2ab6fec7..1af024cc 100644 --- a/lib/ecto/adapters/postgres.ex +++ b/lib/ecto/adapters/postgres.ex @@ -147,13 +147,15 @@ defmodule Ecto.Adapters.Postgres do end end - defp concat_if(content, nil, _fun), do: content + defp concat_if(content, nil, _), do: content + defp concat_if(content, false, _), do: content defp concat_if(content, value, fun), do: content <> " " <> fun.(value) @impl true def storage_down(opts) do database = Keyword.fetch!(opts, :database) || raise ":database is nil in repository configuration" - command = "DROP DATABASE \"#{database}\"" + command = "DROP DATABASE \"#{database}\"" + |> concat_if(opts[:force_drop], fn _ -> "WITH (FORCE)" end) maintenance_database = Keyword.get(opts, :maintenance_database, @default_maintenance_database) opts = Keyword.put(opts, :database, maintenance_database) From 0a411c5bf90a09a00e04fbba1acc6d22bd16e62a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 27 Oct 2020 16:46:08 +0100 Subject: [PATCH 2/3] Update postgres.ex --- lib/ecto/adapters/postgres.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/ecto/adapters/postgres.ex b/lib/ecto/adapters/postgres.ex index 1af024cc..afb2ff0b 100644 --- a/lib/ecto/adapters/postgres.ex +++ b/lib/ecto/adapters/postgres.ex @@ -68,6 +68,8 @@ defmodule Ecto.Adapters.Postgres do * `:lc_collate` - the collation order * `:lc_ctype` - the character classification * `:dump_path` - where to place dumped structures + * `:force_drop` - force the database to be dropped even + if it has connections to it ### After connect callback From b35ed1f378915b922abfecdf18640cfd16978697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 27 Oct 2020 16:47:18 +0100 Subject: [PATCH 3/3] Update lib/ecto/adapters/postgres.ex --- lib/ecto/adapters/postgres.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ecto/adapters/postgres.ex b/lib/ecto/adapters/postgres.ex index afb2ff0b..0bc01b16 100644 --- a/lib/ecto/adapters/postgres.ex +++ b/lib/ecto/adapters/postgres.ex @@ -69,7 +69,7 @@ defmodule Ecto.Adapters.Postgres do * `:lc_ctype` - the character classification * `:dump_path` - where to place dumped structures * `:force_drop` - force the database to be dropped even - if it has connections to it + if it has connections to it (requires PostgreSQL 13+) ### After connect callback