Packages
eventstore
0.14.0-rc.0
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
@moduledoc false
require Logger
def create(config) do
database = Keyword.fetch!(config, :database)
encoding = Keyword.get(config, :encoding, "UTF8")
{output, status} =
run_with_psql(config, "template1", "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, "template1", "DROP DATABASE \"#{database}\"")
cond do
status == 0 -> :ok
String.contains?(output, "3D000") -> {:error, :already_down}
true -> {:error, output}
end
end
def migrate(config, migration) do
database = Keyword.fetch!(config, :database)
case run_with_psql(config, database, migration) do
{_output, 0} -> :ok
{output, _status} -> {:error, output}
end
end
def dump(config, target_path) do
database = Keyword.fetch!(config, :database)
unless System.find_executable("pg_dump") do
raise "could not find executable `pg_dump` in path, " <>
"please guarantee it is available before running event_store mix commands"
end
args = [database | parse_default_args(config)]
env = parse_env(config)
System.cmd("pg_dump", args, env: env, into: File.stream!(target_path))
end
defp run_with_psql(config, database, 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
args =
parse_default_args(config) ++
[
"--quiet",
"--set",
"ON_ERROR_STOP=1",
"--set",
"VERBOSITY=verbose",
"--no-psqlrc",
"-d",
database,
"-c",
sql_command
]
env = parse_env(config)
System.cmd("psql", args, env: env, stderr_to_stdout: true)
end
defp parse_env(config) do
env =
if password = config[:password] do
[{"PGPASSWORD", password}]
else
[]
end
[{"PGCONNECT_TIMEOUT", "10"} | env]
end
defp parse_default_args(config) do
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 ++
[
"--host",
host,
"--no-password"
]
end
end