Current section
Files
Jump to
Current section
Files
src/cigogne@internal@migration_parser.erl
-module(cigogne@internal@migration_parser).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/cigogne/internal/migration_parser.gleam").
-export([parse_file_name/1, parse_migration_file/1]).
-export_type([query_context/0]).
-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).
-type query_context() :: simple_context |
in_string_literal |
{in_dollar_quoted, binary()} |
{in_dollar_tag, list(binary()), gleam@option:option(binary())}.
-file("src/cigogne/internal/migration_parser.gleam", 16).
?DOC(false).
-spec parse_file_name(binary()) -> {ok,
{gleam@time@timestamp:timestamp(), binary()}} |
{error, cigogne@types:migrate_error()}.
parse_file_name(Path) ->
gleam@bool:guard(
not gleam_stdlib:string_ends_with(Path, <<".sql"/utf8>>),
{error, {file_name_error, Path}},
fun() ->
gleam@result:'try'(
begin
_pipe = gleam@string:split(Path, <<"/"/utf8>>),
_pipe@1 = gleam@list:last(_pipe),
_pipe@2 = gleam@result:map(
_pipe@1,
fun(_capture) -> gleam@string:drop_end(_capture, 4) end
),
_pipe@3 = gleam@result:'try'(
_pipe@2,
fun(_capture@1) ->
gleam@string:split_once(_capture@1, <<"-"/utf8>>)
end
),
gleam@result:replace_error(_pipe@3, {file_name_error, Path})
end,
fun(Ts_and_name) ->
gleam@result:'try'(
cigogne@internal@migrations_utils:parse_migration_timestamp(
erlang:element(1, Ts_and_name)
),
fun(Ts) ->
gleam@result:'try'(
cigogne@internal@migrations_utils:check_name(
erlang:element(2, Ts_and_name)
),
fun(Name) -> _pipe@4 = {Ts, Name},
{ok, _pipe@4} end
)
end
)
end
)
end
).
-file("src/cigogne/internal/migration_parser.gleam", 184).
?DOC(false).
-spec add_if_not_empty(list(binary()), list(binary())) -> list(binary()).
add_if_not_empty(String_list, Graphemes_to_add) ->
case Graphemes_to_add of
[] ->
String_list;
_ ->
[begin
_pipe = Graphemes_to_add,
_pipe@1 = lists:reverse(_pipe),
gleam@string:join(_pipe@1, <<""/utf8>>)
end |
String_list]
end.
-file("src/cigogne/internal/migration_parser.gleam", 97).
?DOC(false).
-spec analyse_sql_graphemes(
list(binary()),
list(binary()),
query_context(),
list(binary())
) -> {ok, list(binary())} | {error, binary()}.
analyse_sql_graphemes(Sql_graphemes, Previous_graphemes, Context, Queries) ->
case {Sql_graphemes, Context} of
{[], simple_context} ->
_pipe = Queries,
_pipe@1 = add_if_not_empty(_pipe, Previous_graphemes),
{ok, _pipe@1};
{[], _} ->
{error, <<"Migration badly formated: Unfinished literal"/utf8>>};
{[<<";"/utf8>> | Rest], simple_context} ->
analyse_sql_graphemes(
Rest,
[],
simple_context,
begin
_pipe@2 = Queries,
add_if_not_empty(_pipe@2, Previous_graphemes)
end
);
{[<<"'"/utf8>> | Rest@1], simple_context} ->
analyse_sql_graphemes(
Rest@1,
[<<"'"/utf8>> | Previous_graphemes],
in_string_literal,
Queries
);
{[<<"'"/utf8>> | Rest@2], in_string_literal} ->
analyse_sql_graphemes(
Rest@2,
[<<"'"/utf8>> | Previous_graphemes],
simple_context,
Queries
);
{[<<"$"/utf8>> | Rest@3], simple_context} ->
analyse_sql_graphemes(
Rest@3,
[<<"$"/utf8>> | Previous_graphemes],
{in_dollar_tag, [], none},
Queries
);
{[<<"$"/utf8>> | Rest@4], {in_dollar_tag, Tag, none}} ->
analyse_sql_graphemes(
Rest@4,
[<<"$"/utf8>> | Previous_graphemes],
{in_dollar_quoted,
begin
_pipe@3 = Tag,
_pipe@4 = lists:reverse(_pipe@3),
gleam@string:join(_pipe@4, <<""/utf8>>)
end},
Queries
);
{[<<"$"/utf8>> | Rest@5], {in_dollar_tag, Tag_graphemes, {some, Tag@1}}} ->
analyse_sql_graphemes(
Rest@5,
[<<"$"/utf8>> | Previous_graphemes],
case begin
_pipe@5 = Tag_graphemes,
_pipe@6 = lists:reverse(_pipe@5),
gleam@string:join(_pipe@6, <<""/utf8>>)
end of
A when A =:= Tag@1 ->
simple_context;
_ ->
{in_dollar_quoted, Tag@1}
end,
Queries
);
{[<<"$"/utf8>> | Rest@6], {in_dollar_quoted, Tag@2}} ->
analyse_sql_graphemes(
Rest@6,
[<<"$"/utf8>> | Previous_graphemes],
{in_dollar_tag, [], {some, Tag@2}},
Queries
);
{[G | Rest@7], {in_dollar_tag, [], Parent}} ->
analyse_sql_graphemes(Rest@7, [G | Previous_graphemes], case G of
<<"0"/utf8>> ->
case Parent of
none ->
simple_context;
{some, Tag@3} ->
{in_dollar_quoted, Tag@3}
end;
<<"1"/utf8>> ->
case Parent of
none ->
simple_context;
{some, Tag@3} ->
{in_dollar_quoted, Tag@3}
end;
<<"2"/utf8>> ->
case Parent of
none ->
simple_context;
{some, Tag@3} ->
{in_dollar_quoted, Tag@3}
end;
<<"3"/utf8>> ->
case Parent of
none ->
simple_context;
{some, Tag@3} ->
{in_dollar_quoted, Tag@3}
end;
<<"4"/utf8>> ->
case Parent of
none ->
simple_context;
{some, Tag@3} ->
{in_dollar_quoted, Tag@3}
end;
<<"5"/utf8>> ->
case Parent of
none ->
simple_context;
{some, Tag@3} ->
{in_dollar_quoted, Tag@3}
end;
<<"6"/utf8>> ->
case Parent of
none ->
simple_context;
{some, Tag@3} ->
{in_dollar_quoted, Tag@3}
end;
<<"7"/utf8>> ->
case Parent of
none ->
simple_context;
{some, Tag@3} ->
{in_dollar_quoted, Tag@3}
end;
<<"8"/utf8>> ->
case Parent of
none ->
simple_context;
{some, Tag@3} ->
{in_dollar_quoted, Tag@3}
end;
<<"9"/utf8>> ->
case Parent of
none ->
simple_context;
{some, Tag@3} ->
{in_dollar_quoted, Tag@3}
end;
_ ->
{in_dollar_tag, [G], Parent}
end, Queries);
{[G@1 | Rest@8], {in_dollar_tag, Tag_graphemes@1, Parent@1}} ->
analyse_sql_graphemes(
Rest@8,
[G@1 | Previous_graphemes],
{in_dollar_tag, [G@1 | Tag_graphemes@1], Parent@1},
Queries
);
{[G@2 | Rest@9], Ctx} ->
analyse_sql_graphemes(
Rest@9,
[G@2 | Previous_graphemes],
Ctx,
Queries
)
end.
-file("src/cigogne/internal/migration_parser.gleam", 75).
?DOC(false).
-spec split_queries(binary()) -> {ok, list(binary())} | {error, binary()}.
split_queries(Queries) ->
Graphemes = begin
_pipe = Queries,
gleam@string:to_graphemes(_pipe)
end,
gleam@result:'try'(
analyse_sql_graphemes(Graphemes, [], simple_context, []),
fun(Queries@1) -> _pipe@1 = Queries@1,
_pipe@2 = lists:reverse(_pipe@1),
_pipe@3 = gleam@list:map(_pipe@2, fun gleam@string:trim/1),
_pipe@4 = gleam@list:filter(
_pipe@3,
fun(Q) -> not gleam@string:is_empty(Q) end
),
_pipe@5 = gleam@list:map(
_pipe@4,
fun(Q@1) -> <<Q@1/binary, ";"/utf8>> end
),
{ok, _pipe@5} end
).
-file("src/cigogne/internal/migration_parser.gleam", 38).
?DOC(false).
-spec parse_migration_file(binary()) -> {ok, {list(binary()), list(binary())}} |
{error, binary()}.
parse_migration_file(Content) ->
case gleam@string:split_once(Content, <<"--- migration:up"/utf8>>) of
{error, _} ->
{error,
<<<<"File badly formatted: '"/utf8, "--- migration:up"/utf8>>/binary,
"' not found !"/utf8>>};
{ok, {_, Up_and_rest}} ->
case gleam@string:split_once(
Up_and_rest,
<<"--- migration:down"/utf8>>
) of
{error, _} ->
{error,
<<<<"File badly formatted: '"/utf8,
"--- migration:down"/utf8>>/binary,
"' not found !"/utf8>>};
{ok, {Up, Down_and_rest}} ->
case gleam@string:split_once(
Down_and_rest,
<<"--- migration:end"/utf8>>
) of
{error, _} ->
{error,
<<<<"File badly formatted: '"/utf8,
"--- migration:end"/utf8>>/binary,
"' not found !"/utf8>>};
{ok, {Down, _}} ->
gleam@result:'try'(
split_queries(Up),
fun(Queries_up) ->
gleam@result:'try'(
split_queries(Down),
fun(Queries_down) ->
case {Queries_up, Queries_down} of
{[], _} ->
{error,
<<"migration:up is empty !"/utf8>>};
{_, []} ->
{error,
<<"migration:down is empty !"/utf8>>};
{Ups, Downs} ->
{ok, {Ups, Downs}}
end
end
)
end
)
end
end
end.