Skip to content

Commit 17b5d2c

Browse files
committed
fix(deploy): use raw Postgrex conn for schema drop in reset
DROP SCHEMA CASCADE terminates all active connections to that schema, including Ecto pool connections. Use a standalone Postgrex connection for the drop/recreate, then let with_repo start fresh for migrations.
1 parent 54349cf commit 17b5d2c

1 file changed

Lines changed: 22 additions & 10 deletions

File tree

lib/craftplan/release.ex

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,29 @@ defmodule Craftplan.Release do
2020
def reset do
2121
load_app()
2222

23-
# Use a temporary connection to drop/recreate the schema, avoiding
24-
# pool connection issues from DROP SCHEMA CASCADE.
25-
for repo <- repos() do
26-
{:ok, _, _} =
27-
Ecto.Migrator.with_repo(repo, fn repo ->
28-
repo.query!("DROP SCHEMA public CASCADE")
29-
repo.query!("CREATE SCHEMA public")
30-
Ecto.Migrator.run(repo, :up, all: true)
31-
end)
32-
end
23+
# Parse the DATABASE_URL and open a raw Postgrex connection to
24+
# drop/recreate the schema. This avoids pool disconnection issues
25+
# since DROP SCHEMA CASCADE terminates all other connections.
26+
db_url = System.get_env("DATABASE_URL") || raise "DATABASE_URL not set"
27+
uri = URI.parse(db_url)
28+
[username, password] = String.split(uri.userinfo || "postgres:", ":")
29+
30+
{:ok, conn} =
31+
Postgrex.start_link(
32+
hostname: uri.host,
33+
port: uri.port || 5432,
34+
username: username,
35+
password: password,
36+
database: String.trim_leading(uri.path, "/"),
37+
ssl: false,
38+
socket_options: [:inet6]
39+
)
40+
41+
Postgrex.query!(conn, "DROP SCHEMA public CASCADE", [])
42+
Postgrex.query!(conn, "CREATE SCHEMA public", [])
43+
GenServer.stop(conn)
3344

45+
migrate()
3446
seed()
3547
end
3648

0 commit comments

Comments
 (0)