Current section
Files
Jump to
Current section
Files
src/gorrion@tracker.erl
-module(gorrion@tracker).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gorrion/tracker.gleam").
-export([ensure_table/1, get_applied/1, record/3, remove/2, current_version/1]).
-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(" Manages the _schema_migrations tracking table.\n").
-file("src/gorrion/tracker.gleam", 12).
?DOC(" Create the _schema_migrations table if it doesn't exist.\n").
-spec ensure_table(pog:connection()) -> {ok, nil} |
{error, gorrion@types:migration_error()}.
ensure_table(Db) ->
Sql = <<"CREATE TABLE IF NOT EXISTS _schema_migrations (
version INTEGER PRIMARY KEY,
name TEXT NOT NULL,
applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)"/utf8>>,
Decoder = gleam@dynamic@decode:map(
{decoder, fun gleam@dynamic@decode:decode_dynamic/1},
fun(_) -> nil end
),
_pipe = Sql,
_pipe@1 = pog:'query'(_pipe),
_pipe@2 = pog:returning(_pipe@1, Decoder),
_pipe@3 = pog:execute(_pipe@2, Db),
_pipe@4 = gleam@result:map(_pipe@3, fun(_) -> nil end),
gleam@result:map_error(
_pipe@4,
fun(E) -> {query_error, gleam@string:inspect(E)} end
).
-file("src/gorrion/tracker.gleam", 31).
?DOC(" Get all applied migrations, sorted by version ascending.\n").
-spec get_applied(pog:connection()) -> {ok,
list(gorrion@types:applied_migration())} |
{error, gorrion@types:migration_error()}.
get_applied(Db) ->
Sql = <<"SELECT version, name, CAST(applied_at AS VARCHAR) as applied_at
FROM _schema_migrations
ORDER BY version ASC"/utf8>>,
Decoder = begin
gleam@dynamic@decode:field(
0,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Version) ->
gleam@dynamic@decode:field(
1,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Name) ->
gleam@dynamic@decode:field(
2,
{decoder, fun gleam@dynamic@decode:decode_string/1},
fun(Applied_at) ->
gleam@dynamic@decode:success(
{applied_migration,
Version,
Name,
Applied_at}
)
end
)
end
)
end
)
end,
_pipe = Sql,
_pipe@1 = pog:'query'(_pipe),
_pipe@2 = pog:returning(_pipe@1, Decoder),
_pipe@3 = pog:execute(_pipe@2, Db),
_pipe@4 = gleam@result:map(
_pipe@3,
fun(Response) -> erlang:element(3, Response) end
),
gleam@result:map_error(
_pipe@4,
fun(E) -> {query_error, gleam@string:inspect(E)} end
).
-file("src/gorrion/tracker.gleam", 55).
?DOC(" Record a migration as applied.\n").
-spec record(pog:connection(), integer(), binary()) -> {ok, nil} |
{error, gorrion@types:migration_error()}.
record(Db, Version, Name) ->
Sql = <<"INSERT INTO _schema_migrations (version, name) VALUES ($1, $2)"/utf8>>,
Decoder = gleam@dynamic@decode:map(
{decoder, fun gleam@dynamic@decode:decode_dynamic/1},
fun(_) -> nil end
),
_pipe = Sql,
_pipe@1 = pog:'query'(_pipe),
_pipe@2 = pog:parameter(_pipe@1, pog_ffi:coerce(Version)),
_pipe@3 = pog:parameter(_pipe@2, pog_ffi:coerce(Name)),
_pipe@4 = pog:returning(_pipe@3, Decoder),
_pipe@5 = pog:execute(_pipe@4, Db),
_pipe@6 = gleam@result:map(_pipe@5, fun(_) -> nil end),
gleam@result:map_error(
_pipe@6,
fun(E) -> {query_error, gleam@string:inspect(E)} end
).
-file("src/gorrion/tracker.gleam", 76).
?DOC(" Remove a migration record (used during rollback).\n").
-spec remove(pog:connection(), integer()) -> {ok, nil} |
{error, gorrion@types:migration_error()}.
remove(Db, Version) ->
Sql = <<"DELETE FROM _schema_migrations WHERE version = $1"/utf8>>,
Decoder = gleam@dynamic@decode:map(
{decoder, fun gleam@dynamic@decode:decode_dynamic/1},
fun(_) -> nil end
),
_pipe = Sql,
_pipe@1 = pog:'query'(_pipe),
_pipe@2 = pog:parameter(_pipe@1, pog_ffi:coerce(Version)),
_pipe@3 = pog:returning(_pipe@2, Decoder),
_pipe@4 = pog:execute(_pipe@3, Db),
_pipe@5 = gleam@result:map(_pipe@4, fun(_) -> nil end),
gleam@result:map_error(
_pipe@5,
fun(E) -> {query_error, gleam@string:inspect(E)} end
).
-file("src/gorrion/tracker.gleam", 95).
?DOC(" Get the highest applied migration version, or 0 if none.\n").
-spec current_version(pog:connection()) -> {ok, integer()} |
{error, gorrion@types:migration_error()}.
current_version(Db) ->
Sql = <<"SELECT COALESCE(MAX(version), 0) as version FROM _schema_migrations"/utf8>>,
Decoder = begin
gleam@dynamic@decode:field(
0,
{decoder, fun gleam@dynamic@decode:decode_int/1},
fun(Version) -> gleam@dynamic@decode:success(Version) end
)
end,
_pipe = Sql,
_pipe@1 = pog:'query'(_pipe),
_pipe@2 = pog:returning(_pipe@1, Decoder),
_pipe@3 = pog:execute(_pipe@2, Db),
_pipe@4 = gleam@result:map(
_pipe@3,
fun(Response) -> case erlang:element(3, Response) of
[Version@1] ->
Version@1;
_ ->
0
end end
),
gleam@result:map_error(
_pipe@4,
fun(E) -> {query_error, gleam@string:inspect(E)} end
).