Packages
eventstore
0.7.3
1.4.8
1.4.7
1.4.6
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.2
1.3.1
1.3.0
1.2.3
1.2.2
1.2.1
1.2.0
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
1.0.0-rc.0
0.17.0
0.16.2
0.16.1
0.16.0
0.15.1
0.15.0
0.14.0
0.14.0-rc.0
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.11.0-rc.0
0.10.1
0.10.0
0.9.0
0.8.1
0.8.0
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.2
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.0
0.2.1
0.2.0
0.1.0
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1
Event store using PostgreSQL for persistence.
Current section
Files
Jump to
Current section
Files
lib/event_store/storage/database.ex
defmodule EventStore.Storage.Database do
require Logger
def create(config) do
database = Keyword.fetch!(config, :database)
encoding = Keyword.get(config, :encoding, "UTF8")
{output, status} = run_with_psql(config, "CREATE DATABASE \"#{database}\" ENCODING='#{encoding}'")
cond do
status == 0 -> :ok
String.contains?(output, "42P04") -> {:error, :already_up}
true -> {:error, output}
end
end
def drop(config) do
database = Keyword.fetch!(config, :database)
{output, status} = run_with_psql(config, "DROP DATABASE \"#{database}\"")
cond do
status == 0 -> :ok
String.contains?(output, "3D000") -> {:error, :already_down}
true -> {:error, output}
end
end
defp run_with_psql(config, sql_command) do
unless System.find_executable("psql") do
raise "could not find executable `psql` in path, " <>
"please guarantee it is available before running event_store mix commands"
end
env =
if password = config[:password] do
[{"PGPASSWORD", password}]
else
[]
end
env = [{"PGCONNECT_TIMEOUT", "10"} | env]
args = []
args = case config[:username] do
nil -> args
username -> ["-U", username | args]
end
args = case config[:port] do
nil -> args
port -> ["-p", to_string(port) | args]
end
host = config[:hostname] || System.get_env("PGHOST") || "localhost"
args = args ++ ["--quiet",
"--host", host,
"--no-password",
"--set", "ON_ERROR_STOP=1",
"--set", "VERBOSITY=verbose",
"--no-psqlrc",
"-d", "template1",
"-c", sql_command]
System.cmd("psql", args, env: env, stderr_to_stdout: true)
end
end