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)