Current section
Files
Jump to
Current section
Files
src/cigogne@internal@database.erl
-module(cigogne@internal@database).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([get_url/0, connect/1, execute_batch/3, get_schema/1]).
-file("/home/runner/work/cigogne/cigogne/src/cigogne/internal/database.gleam", 8).
-spec get_url() -> {ok, binary()} | {error, cigogne@types:migrate_error()}.
get_url() ->
_pipe = envoy_ffi:get(<<"DATABASE_URL"/utf8>>),
gleam@result:replace_error(_pipe, {env_var_error, <<"DATABASE_URL"/utf8>>}).
-file("/home/runner/work/cigogne/cigogne/src/cigogne/internal/database.gleam", 18).
-spec get_config(binary()) -> {ok, pog:config()} |
{error, cigogne@types:migrate_error()}.
get_config(Url) ->
_pipe = pog:url_config(Url),
gleam@result:replace_error(_pipe, {url_error, Url}).
-file("/home/runner/work/cigogne/cigogne/src/cigogne/internal/database.gleam", 13).
-spec connect(binary()) -> {ok, pog:connection()} |
{error, cigogne@types:migrate_error()}.
connect(Url) ->
gleam@result:'try'(get_config(Url), fun(Config) -> _pipe = Config,
_pipe@1 = pog_ffi:connect(_pipe),
{ok, _pipe@1} end).
-file("/home/runner/work/cigogne/cigogne/src/cigogne/internal/database.gleam", 32).
-spec transaction(
pog:connection(),
fun((pog:connection()) -> {ok, nil} | {error, binary()})
) -> {ok, nil} | {error, cigogne@types:migrate_error()}.
transaction(Connection, Callback) ->
_pipe = pog_ffi:transaction(
Connection,
fun(Transaction) -> Callback(Transaction) end
),
gleam@result:map_error(
_pipe,
fun(Field@0) -> {pgo_transaction_error, Field@0} end
).
-file("/home/runner/work/cigogne/cigogne/src/cigogne/internal/database.gleam", 53).
-spec exec_queries(pog:connection(), list(pog:'query'(any()))) -> {ok, nil} |
{error, pog:query_error()}.
exec_queries(Conn, Queries) ->
case Queries of
[] ->
{ok, nil};
[Q | Rest] ->
Res = begin
_pipe = Q,
pog:execute(_pipe, Conn)
end,
case Res of
{error, Err} ->
{error, Err};
{ok, _} ->
exec_queries(Conn, Rest)
end
end.
-file("/home/runner/work/cigogne/cigogne/src/cigogne/internal/database.gleam", 40).
-spec handle_queries(pog:connection(), list(pog:'query'(any())), boolean()) -> {ok,
nil} |
{error, binary()}.
handle_queries(Conn, Queries, Ignore_errors) ->
Result = exec_queries(Conn, Queries),
case {Ignore_errors, Result} of
{true,
{error, {constraint_violated, _, <<"_migrations_pkey"/utf8>>, _}}} ->
{ok, nil};
{_, _} ->
_pipe = Result,
gleam@result:map_error(
_pipe,
fun cigogne@internal@utils:describe_query_error/1
)
end.
-file("/home/runner/work/cigogne/cigogne/src/cigogne/internal/database.gleam", 23).
-spec execute_batch(pog:connection(), list(pog:'query'(any())), boolean()) -> {ok,
nil} |
{error, cigogne@types:migrate_error()}.
execute_batch(Connection, Queries, Ignore_errors) ->
transaction(
Connection,
fun(Transaction) ->
handle_queries(Transaction, Queries, Ignore_errors)
end
).
-file("/home/runner/work/cigogne/cigogne/src/cigogne/internal/database.gleam", 74).
-spec get_schema(binary()) -> {ok, binary()} |
{error, cigogne@types:migrate_error()}.
get_schema(Url) ->
_pipe = shellout:command(
<<"psql"/utf8>>,
[Url, <<"-c"/utf8>>, <<"\\d public.*"/utf8>>],
<<"."/utf8>>,
[]
),
gleam@result:map_error(
_pipe,
fun(Err) -> {schema_query_error, erlang:element(2, Err)} end
).