Current section

Files

Jump to
cigogne src cigogne.erl
Raw

src/cigogne.erl

-module(cigogne).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([get_migrations/0, get_schema/1, update_schema_file/1, print_error/1, apply_migration/2, apply_next_migration/1, migrate_up/0, execute_migrations_to_last/1, migrate_to_last/0, roll_back_migration/2, roll_back_previous_migration/1, migrate_down/0, execute_migrations_to/2, migrate_to/1, main/0]).
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 49).
-spec show_usage() -> {ok, nil} | {error, cigogne@types:migrate_error()}.
show_usage() ->
gleam@io:println(<<"======================================="/utf8>>),
gleam@io:println(<<"= GLITR MIGRATE ="/utf8>>),
gleam@io:println(<<"======================================="/utf8>>),
gleam@io:println(<<""/utf8>>),
gleam@io:println(<<"Usage: gleam run -m glitr/migrate [command]"/utf8>>),
gleam@io:println(<<""/utf8>>),
gleam@io:println(<<"List of commands:"/utf8>>),
gleam@io:println(
<<" - show: Show the last currently applied migration"/utf8>>
),
gleam@io:println(
<<" - up: Migrate up one version / Apply one migration"/utf8>>
),
gleam@io:println(
<<" - down: Migrate down one version / Rollback one migration"/utf8>>
),
gleam@io:println(
<<" - last: Apply all migrations until the last one defined"/utf8>>
),
gleam@io:println(
<<" - to N: Apply or roll back migrations until the migration N is reached"/utf8>>
),
{ok, nil}.
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 225).
-spec get_migrations() -> {ok, list(cigogne@types:migration())} |
{error, cigogne@types:migrate_error()}.
get_migrations() ->
cigogne@internal@fs:get_migrations().
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 230).
-spec get_schema(binary()) -> {ok, binary()} |
{error, cigogne@types:migrate_error()}.
get_schema(Url) ->
cigogne@internal@database:get_schema(Url).
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 236).
-spec update_schema_file(binary()) -> {ok, nil} |
{error, cigogne@types:migrate_error()}.
update_schema_file(Url) ->
gleam@result:'try'(
get_schema(Url),
fun(Schema) -> cigogne@internal@fs:write_schema_file(Schema) end
).
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 274).
-spec print_error(cigogne@types:migrate_error()) -> nil.
print_error(Error) ->
cigogne@types:print_migrate_error(Error).
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 241).
-spec get_last_applied_migration(pog:connection()) -> {ok,
{integer(), binary()}} |
{error, cigogne@types:migrate_error()}.
get_last_applied_migration(Conn) ->
_pipe = pog:'query'(
<<"SELECT id, name FROM _migrations ORDER BY appliedAt DESC LIMIT 1;"/utf8>>
),
_pipe@1 = pog:returning(
_pipe,
gleam@dynamic:tuple2(
fun gleam@dynamic:int/1,
fun gleam@dynamic:string/1
)
),
_pipe@2 = pog:execute(_pipe@1, Conn),
_pipe@3 = gleam@result:map_error(
_pipe@2,
fun(Field@0) -> {pgo_query_error, Field@0} end
),
gleam@result:then(_pipe@3, fun(Returned) -> case Returned of
{returned, 0, _} ->
{error, no_result_error};
{returned, _, []} ->
{error, no_result_error};
{returned, _, [Last | _]} ->
{ok, Last}
end end).
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 178).
-spec apply_migration(pog:connection(), cigogne@types:migration()) -> {ok, nil} |
{error, cigogne@types:migrate_error()}.
apply_migration(Connection, Migration) ->
gleam@io:println(
<<<<<<<<"\nApplying migration "/utf8,
(gleam@int:to_string(erlang:element(3, Migration)))/binary>>/binary,
"-"/utf8>>/binary,
(erlang:element(4, Migration))/binary>>/binary,
"\n"/utf8>>
),
Queries = begin
_pipe = gleam@list:map(erlang:element(5, Migration), fun pog:'query'/1),
lists:append(
_pipe,
[begin
_pipe@1 = pog:'query'(
<<"INSERT INTO _migrations VALUES ($1, $2);"/utf8>>
),
_pipe@2 = pog:parameter(
_pipe@1,
pog_ffi:coerce(erlang:element(3, Migration))
),
pog:parameter(
_pipe@2,
pog_ffi:coerce(erlang:element(4, Migration))
)
end]
)
end,
cigogne@internal@database:execute_batch(
Connection,
erlang:element(3, Migration),
Queries
).
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 115).
-spec apply_next_migration(pog:connection()) -> {ok, nil} |
{error, cigogne@types:migrate_error()}.
apply_next_migration(Connection) ->
gleam@result:'try'(
apply_migration(
Connection,
{migration,
<<""/utf8>>,
0,
<<"CreateMigrationsTable"/utf8>>,
[<<"CREATE TABLE IF NOT EXISTS _migrations(
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
appliedAt TIMESTAMP NOT NULL DEFAULT NOW()
);"/utf8>>],
[]}
),
fun(_) ->
gleam@result:'try'(
get_last_applied_migration(Connection),
fun(Last) ->
gleam@result:'try'(
cigogne@internal@fs:get_migrations(),
fun(Migrations) ->
gleam@result:'try'(
cigogne@internal@fs:find_migration(
Migrations,
erlang:element(1, Last) + 1
),
fun(Migration) ->
apply_migration(Connection, Migration)
end
)
end
)
end
)
end
).
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 72).
-spec migrate_up() -> {ok, nil} | {error, cigogne@types:migrate_error()}.
migrate_up() ->
gleam@result:'try'(
cigogne@internal@database:get_url(),
fun(Url) ->
gleam@result:'try'(
cigogne@internal@database:connect(Url),
fun(Conn) ->
gleam@result:'try'(
apply_next_migration(Conn),
fun(_) -> update_schema_file(Url) end
)
end
)
end
).
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 161).
-spec execute_migrations_to_last(pog:connection()) -> {ok, nil} |
{error, cigogne@types:migrate_error()}.
execute_migrations_to_last(Connection) ->
gleam@result:'try'(
apply_migration(
Connection,
{migration,
<<""/utf8>>,
0,
<<"CreateMigrationsTable"/utf8>>,
[<<"CREATE TABLE IF NOT EXISTS _migrations(
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
appliedAt TIMESTAMP NOT NULL DEFAULT NOW()
);"/utf8>>],
[]}
),
fun(_) ->
gleam@result:'try'(
get_last_applied_migration(Connection),
fun(Last) ->
gleam@result:'try'(
cigogne@internal@fs:get_migrations(),
fun(Migrations) ->
Max@1 = gleam@list:fold(
Migrations,
0,
fun(Max, Mig) ->
gleam@int:max(Max, erlang:element(3, Mig))
end
),
gleam@bool:guard(
Max@1 > erlang:element(1, Last),
{error, no_migration_to_apply_error},
fun() ->
_pipe = cigogne@internal@fs:find_migrations_between(
Migrations,
erlang:element(1, Last),
Max@1
),
gleam@result:then(
_pipe,
fun(_capture) ->
gleam@list:try_each(
_capture,
fun(Migration) ->
apply_migration(
Connection,
Migration
)
end
)
end
)
end
)
end
)
end
)
end
).
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 105).
-spec migrate_to_last() -> {ok, nil} | {error, cigogne@types:migrate_error()}.
migrate_to_last() ->
gleam@result:'try'(
cigogne@internal@database:get_url(),
fun(Url) ->
gleam@result:'try'(
cigogne@internal@database:connect(Url),
fun(Conn) ->
gleam@result:'try'(
execute_migrations_to_last(Conn),
fun(_) -> update_schema_file(Url) end
)
end
)
end
).
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 256).
-spec show() -> {ok, nil} | {error, cigogne@types:migrate_error()}.
show() ->
gleam@result:'try'(
cigogne@internal@database:get_url(),
fun(Url) ->
gleam@result:'try'(
cigogne@internal@database:connect(Url),
fun(Conn) ->
gleam@result:'try'(
apply_migration(
Conn,
{migration,
<<""/utf8>>,
0,
<<"CreateMigrationsTable"/utf8>>,
[<<"CREATE TABLE IF NOT EXISTS _migrations(
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
appliedAt TIMESTAMP NOT NULL DEFAULT NOW()
);"/utf8>>],
[]}
),
fun(_) ->
gleam@result:'try'(
get_last_applied_migration(Conn),
fun(Last) ->
gleam@io:println(
<<<<<<"Last applied migration: "/utf8,
(gleam@int:to_string(
erlang:element(1, Last)
))/binary>>/binary,
"-"/utf8>>/binary,
(erlang:element(2, Last))/binary>>
),
gleam@result:'try'(
get_schema(Url),
fun(Schema) ->
gleam@io:println(<<""/utf8>>),
gleam@io:println(Schema),
{ok, nil}
end
)
end
)
end
)
end
)
end
).
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 202).
-spec roll_back_migration(pog:connection(), cigogne@types:migration()) -> {ok,
nil} |
{error, cigogne@types:migrate_error()}.
roll_back_migration(Connection, Migration) ->
gleam@io:println(
<<<<<<<<"\nRolling back migration "/utf8,
(gleam@int:to_string(erlang:element(3, Migration)))/binary>>/binary,
"-"/utf8>>/binary,
(erlang:element(4, Migration))/binary>>/binary,
"\n"/utf8>>
),
Queries = begin
_pipe = gleam@list:map(erlang:element(6, Migration), fun pog:'query'/1),
lists:append(
_pipe,
[begin
_pipe@1 = pog:'query'(
<<"DELETE FROM _migrations WHERE id = $1;"/utf8>>
),
pog:parameter(
_pipe@1,
pog_ffi:coerce(erlang:element(3, Migration))
)
end]
)
end,
cigogne@internal@database:execute_batch(
Connection,
erlang:element(3, Migration),
Queries
).
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 128).
-spec roll_back_previous_migration(pog:connection()) -> {ok, nil} |
{error, cigogne@types:migrate_error()}.
roll_back_previous_migration(Connection) ->
gleam@result:'try'(
apply_migration(
Connection,
{migration,
<<""/utf8>>,
0,
<<"CreateMigrationsTable"/utf8>>,
[<<"CREATE TABLE IF NOT EXISTS _migrations(
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
appliedAt TIMESTAMP NOT NULL DEFAULT NOW()
);"/utf8>>],
[]}
),
fun(_) ->
gleam@result:'try'(
get_last_applied_migration(Connection),
fun(Last) ->
gleam@result:'try'(
cigogne@internal@fs:get_migrations(),
fun(Migrations) ->
gleam@result:'try'(
cigogne@internal@fs:find_migration(
Migrations,
erlang:element(1, Last)
),
fun(Migration) ->
roll_back_migration(Connection, Migration)
end
)
end
)
end
)
end
).
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 83).
-spec migrate_down() -> {ok, nil} | {error, cigogne@types:migrate_error()}.
migrate_down() ->
gleam@result:'try'(
cigogne@internal@database:get_url(),
fun(Url) ->
gleam@result:'try'(
cigogne@internal@database:connect(Url),
fun(Conn) ->
gleam@result:'try'(
roll_back_previous_migration(Conn),
fun(_) -> update_schema_file(Url) end
)
end
)
end
).
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 141).
-spec execute_migrations_to(pog:connection(), integer()) -> {ok, nil} |
{error, cigogne@types:migrate_error()}.
execute_migrations_to(Connection, Migration_number) ->
gleam@result:'try'(
apply_migration(
Connection,
{migration,
<<""/utf8>>,
0,
<<"CreateMigrationsTable"/utf8>>,
[<<"CREATE TABLE IF NOT EXISTS _migrations(
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
appliedAt TIMESTAMP NOT NULL DEFAULT NOW()
);"/utf8>>],
[]}
),
fun(_) ->
gleam@result:'try'(
get_last_applied_migration(Connection),
fun(Last) ->
gleam@result:'try'(
cigogne@internal@fs:get_migrations(),
fun(Migrations) ->
_pipe = cigogne@internal@fs:find_migrations_between(
Migrations,
erlang:element(1, Last),
Migration_number
),
gleam@result:then(
_pipe,
fun(_capture) ->
gleam@list:try_each(
_capture,
fun(Migration) ->
case Migration_number > erlang:element(
1,
Last
) of
true ->
apply_migration(
Connection,
Migration
);
false ->
roll_back_migration(
Connection,
Migration
)
end
end
)
end
)
end
)
end
)
end
).
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 94).
-spec migrate_to(integer()) -> {ok, nil} |
{error, cigogne@types:migrate_error()}.
migrate_to(Migration_number) ->
gleam@result:'try'(
cigogne@internal@database:get_url(),
fun(Url) ->
gleam@result:'try'(
cigogne@internal@database:connect(Url),
fun(Conn) ->
gleam@result:'try'(
execute_migrations_to(Conn, Migration_number),
fun(_) -> update_schema_file(Url) end
)
end
)
end
).
-file("/home/runner/work/cigogne/cigogne/src/cigogne.gleam", 33).
-spec main() -> {ok, nil} | {error, nil}.
main() ->
_pipe = case erlang:element(4, argv:load()) of
[<<"show"/utf8>>] ->
show();
[<<"up"/utf8>>] ->
migrate_up();
[<<"down"/utf8>>] ->
migrate_down();
[<<"last"/utf8>>] ->
migrate_to_last();
[<<"to"/utf8>>, X] ->
case gleam@int:parse(X) of
{error, _} ->
show_usage();
{ok, Mig} ->
migrate_to(Mig)
end;
_ ->
show_usage()
end,
gleam@result:map_error(_pipe, fun cigogne@types:print_migrate_error/1).