Skip to content

Commit

Permalink
Support force dropping database introduced in PostgreSQL 13 (#280)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ruudk authored and josevalim committed Oct 27, 2020
1 parent 051b735 commit 7dbb164
Showing 1 changed file with 6 additions and 2 deletions.
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

0 comments on commit 7dbb164

Please sign in to comment.