Skip to content

Commit

Permalink
Merge pull request #55 from DaemonSnake/fix_pg_restart_docker_compose
Browse files Browse the repository at this point in the history
pg_restart: handle case of using docker-compose.dbs.yaml
  • Loading branch information
cpursley committed May 3, 2024
2 parents b6804d9 + 4e18581 commit a1b454a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/mix/tasks/helpers.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
defmodule Mix.Tasks.Walex.Helpers do
@config %{
hostname: "localhost",
username: "postgres",
password: "postgres",
port: 5432
}

@moduledoc false
def create_database(database_name) do
"-c \"CREATE DATABASE #{database_name};\""
Expand All @@ -17,9 +24,16 @@ defmodule Mix.Tasks.Walex.Helpers do
end

def database_cmd(cmd) do
db_cmd = "psql -U postgres " <> cmd
db_cmd = "psql " <> cmd

env = [
{"PGHOST", @config.hostname},
{"PGUSER", @config.username},
{"PGPASSWORD", @config.password},
{"PGPORT", Integer.to_string(@config.port)}
]

case System.shell(db_cmd) do
case System.shell(db_cmd, env: env) do
{output, 0} ->
output

Expand Down
28 changes: 28 additions & 0 deletions test/walex/database_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,34 @@ defmodule WalEx.DatabaseTest do
@mac_app_path "/Applications/Postgres.app/Contents/Versions"

def pg_restart do
if uses_docker_compose() do
Logger.debug("Restarting docker postgres.")
pg_restart(:docker)
else
Logger.debug("Restarting system postgres.")
pg_restart(:system)
end
end

def uses_docker_compose do
case(System.shell("docker compose -f docker-compose.dbs.yml ps db")) do
{_, 0} -> true
_ -> false
end
end

def pg_restart(:docker) do
case(System.shell("docker compose -f docker-compose.dbs.yml restart db")) do
{_, 0} ->
:ok

{output, _} ->
Logger.error("Error restarting PostgreSQL via docker-compose: #{inspect(output)}")
raise "Error restarting PostgreSQL via docker-compose."
end
end

def pg_restart(:system) do
case :os.type() do
{:unix, :darwin} ->
Logger.debug("MacOS detected.")
Expand Down

0 comments on commit a1b454a

Please sign in to comment.