Packages

Simple pure gleam database agnostic migrations library for gleam

Current section

Files

Jump to
gleager src gleager@internal@database.erl
Raw

src/gleager@internal@database.erl

-module(gleager@internal@database).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleager/internal/database.gleam").
-export([create_schema_table/1, select_migrations/1, insert_migration/2, delete_migration/2, run_stmts/2, up/2, down/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(false).
-file("src/gleager/internal/database.gleam", 5).
?DOC(false).
-spec create_schema_table(fun((binary()) -> {ok, any()} | {error, any()})) -> {ok,
nil} |
{error, gleager@types:migration_error()}.
create_schema_table(Exec) ->
_pipe = Exec(
<<"CREATE TABLE IF NOT EXISTS \"schema_migrations\" (version varchar(128) primary key);"/utf8>>
),
_pipe@1 = gleam@result:replace_error(_pipe, schema_table_failed),
gleam@result:replace(_pipe@1, nil).
-file("src/gleager/internal/database.gleam", 83).
?DOC(false).
-spec migration_row_decoder() -> gleam@dynamic@decode:decoder(gleager@types:migration_row()).
migration_row_decoder() ->
gleam@dynamic@decode:then(
gleam@dynamic@decode:one_of(
begin
gleam@dynamic@decode:field(
<<"version"/utf8>>,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(V) -> gleam@dynamic@decode:success(V) end
)
end,
[begin
gleam@dynamic@decode:field(
0,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(V@1) -> gleam@dynamic@decode:success(V@1) end
)
end]
),
fun(Version) ->
gleam@dynamic@decode:success({migration_row, Version})
end
).
-file("src/gleager/internal/database.gleam", 15).
?DOC(false).
-spec select_migrations(
fun((binary(), gleam@dynamic@decode:decoder(gleager@types:migration_row())) -> {ok,
list(gleager@types:migration_row())} |
{error, any()})
) -> {ok, list(gleager@types:migration_row())} |
{error, gleager@types:migration_error()}.
select_migrations(Query) ->
_pipe = Query(
<<"SELECT version FROM schema_migrations ORDER BY version ASC"/utf8>>,
migration_row_decoder()
),
gleam@result:replace_error(_pipe, select_migrations_failed).
-file("src/gleager/internal/database.gleam", 25).
?DOC(false).
-spec insert_migration(
gleager@types:migration(),
fun((binary(), list(binary())) -> {ok, any()} | {error, any()})
) -> {ok, nil} | {error, gleager@types:migration_error()}.
insert_migration(Migration, Exec) ->
_pipe = Exec(
<<"insert into schema_migrations (version) values (?)"/utf8>>,
[erlang:element(2, Migration)]
),
_pipe@1 = gleam@result:replace_error(
_pipe,
{insert_migration_failed, erlang:element(2, Migration)}
),
gleam@result:replace(_pipe@1, nil).
-file("src/gleager/internal/database.gleam", 36).
?DOC(false).
-spec delete_migration(
gleager@types:migration(),
fun((binary(), list(binary())) -> {ok, any()} | {error, any()})
) -> {ok, nil} | {error, gleager@types:migration_error()}.
delete_migration(Migration, Exec) ->
_pipe = Exec(
<<"delete from schema_migrations where version = ?"/utf8>>,
[erlang:element(2, Migration)]
),
_pipe@1 = gleam@result:replace_error(
_pipe,
{delete_migration_failed, erlang:element(2, Migration)}
),
gleam@result:replace(_pipe@1, nil).
-file("src/gleager/internal/database.gleam", 47).
?DOC(false).
-spec run_stmts(binary(), fun((binary()) -> {ok, any()} | {error, any()})) -> {ok,
nil} |
{error, gleager@types:migration_error()}.
run_stmts(Stmts, Exec) ->
Sql = <<<<"BEGIN TRANSACTION;\n"/utf8, Stmts/binary>>/binary,
"\nCOMMIT;"/utf8>>,
case Exec(Sql) of
{ok, _} ->
{ok, nil};
{error, _} ->
_ = Exec(<<"ROLLBACK;"/utf8>>),
{error, {migration_statement_failed, Stmts}}
end.
-file("src/gleager/internal/database.gleam", 61).
?DOC(false).
-spec up(
gleager@types:migration(),
fun((binary(), list(binary())) -> {ok, any()} | {error, any()})
) -> {ok, nil} | {error, gleager@types:migration_error()}.
up(Migration, Exec) ->
gleam@result:'try'(
run_stmts(
erlang:element(3, Migration),
fun(_capture) -> Exec(_capture, []) end
),
fun(_) -> _pipe = insert_migration(Migration, Exec),
_pipe@1 = gleam@result:replace(_pipe, nil),
gleam@result:replace_error(
_pipe@1,
{up_migration_failed, erlang:element(2, Migration)}
) end
).
-file("src/gleager/internal/database.gleam", 72).
?DOC(false).
-spec down(
gleager@types:migration(),
fun((binary(), list(binary())) -> {ok, any()} | {error, any()})
) -> {ok, nil} | {error, gleager@types:migration_error()}.
down(Migration, Exec) ->
gleam@result:'try'(
run_stmts(
erlang:element(4, Migration),
fun(_capture) -> Exec(_capture, []) end
),
fun(_) -> _pipe = delete_migration(Migration, Exec),
_pipe@1 = gleam@result:replace(_pipe, nil),
gleam@result:replace_error(
_pipe@1,
{down_migration_failed, erlang:element(2, Migration)}
) end
).