From 7dbb16426c2524f636ac18d16ba6af3109ab3020 Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Tue, 27 Oct 2020 16:52:43 +0100 Subject: [PATCH] Support force dropping database introduced in PostgreSQL 13 (#280) 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 | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/ecto/adapters/postgres.ex b/lib/ecto/adapters/postgres.ex index 092839f3..4d33e04a 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 (requires PostgreSQL 13+) ### After connect callback @@ -147,13 +149,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)