Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support force dropping database introduced in PostgreSQL 13 #280

Merged
merged 3 commits into from
Oct 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/ecto/adapters/postgres.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down