diff --git a/server/apps/server/src/server.app.src b/server/apps/server/src/server.app.src index 68896ff..6ee2153 100644 --- a/server/apps/server/src/server.app.src +++ b/server/apps/server/src/server.app.src @@ -9,7 +9,7 @@ sasl, csv, epgsql, - erlando + erlandono ]}, {env, []}, {modules, []}, diff --git a/server/lib/database/rebar.config b/server/lib/database/rebar.config index 9e90b0c..02048c4 100644 --- a/server/lib/database/rebar.config +++ b/server/lib/database/rebar.config @@ -2,5 +2,5 @@ {deps, [ {pure_migrations, "1.3.0"}, {epgsql, "4.7.1"}, - {erlando, "3.1.1"} + {erlandono, "3.1.2"} ]}. diff --git a/server/lib/database/src/database.app.src b/server/lib/database/src/database.app.src index 3182a93..02bfc6c 100644 --- a/server/lib/database/src/database.app.src +++ b/server/lib/database/src/database.app.src @@ -8,7 +8,7 @@ pure_migrations, epgsql, sasl, - erlando + erlandono ]}, {env, []}, {modules, [database, database_migrations, database_utils, postgres_m]}, diff --git a/server/lib/database/src/database_migration.erl b/server/lib/database/src/database_migration.erl deleted file mode 100644 index c83808f..0000000 --- a/server/lib/database/src/database_migration.erl +++ /dev/null @@ -1,92 +0,0 @@ -%%%------------------------------------------------------------------- -%% @doc Migration-related code -%% @end -%%%------------------------------------------------------------------- --module(database_migration). - --export([migrate/1]). - -%% Converts a relative directory path to an absolute path -to_absolute_path(RelativePath) -> - case filelib:is_dir(RelativePath) of - true -> - AbsolutePath = filename:absname(RelativePath), - {ok, AbsolutePath}; - false -> - io:format("Invalid directory path: ~s~n", [RelativePath]), - {error, invalid_directory} - end. - -all_sql_scripts_are_valid(List) -> - lists:all(fun(E) -> - case E of - {ok, _} -> true; - {ok, _, _} -> true; - _ -> false - end - end, - List). - -%% https://github.com/bearmug/erlang-pure-migrations?tab=readme-ov-file#postgresql-and-epgsqlepgsql -%% + This PR -%% https://github.com/bearmug/erlang-pure-migrations/pull/46 -epgsql_query_fun(Conn) -> - fun(Q) -> - case epgsql:squery(Conn, Q) of - {ok, - [{column, <<"version">>, _, _, _, _, _, _, _}, - {column, <<"filename">>, _, _, _, _, _, _, _}], - []} -> - []; - {ok, - [{column, <<"version">>, _, _, _, _, _, _, _}, - {column, <<"filename">>, _, _, _, _, _, _, _}], - Data} -> - [{list_to_integer(binary_to_list(BinV)), binary_to_list(BinF)} - || {BinV, BinF} <- Data]; - {ok, [{column, <<"max">>, _, _, _, _, _, _, _}], [{null}]} -> - % TODO: The comment below is not mine - % It has to be -1 or it will get an error during initialization - % TODO (Mine): I've set this crap to zero and it somewhat works on a clean pg db... - 0; - {ok, [{column, <<"max">>, _, _, _, _, _, _, _}], [{N}]} -> - % The version number is stored in the int4 type and ranges from -2,147,483,648 to 2,147,483,647 - list_to_integer(binary_to_list(N)); - {ok, - [{column, <<"version">>, _, _, _, _, _}, {column, <<"filename">>, _, _, _, _, _}], - Data} -> - [{list_to_integer(binary_to_list(BinV)), binary_to_list(BinF)} - || {BinV, BinF} <- Data]; - {ok, [{column, <<"max">>, _, _, _, _, _}], [{null}]} -> -1; - {ok, [{column, <<"max">>, _, _, _, _, _}], [{N}]} -> list_to_integer(binary_to_list(N)); - {ok, _, _} -> ok; - {ok, _} -> ok; - Default -> - % Match multiple SQL statements in a script - Res = all_sql_scripts_are_valid(Default), - case Res of - true -> ok; - _ -> Default - end - end - end. - -migrate(Conn) -> - io:format("Finding migration scripts... ~n"), - {ok, RelativePath} = application:get_env(migrations_path), - {ok, MigrationPath} = to_absolute_path(RelativePath), - io:format("Migration Path: ~p~n", [MigrationPath]), - TxFun = fun(F) -> epgsql:with_transaction(Conn, fun(_) -> F() end) end, - QxFun = epgsql_query_fun(Conn), - MigrationCall = pure_migrations:migrate(MigrationPath, TxFun, QxFun), - io:format("Running DB migrations.~n"), - case MigrationCall() of - ok -> - io:format("Migrations completed successfully.~n"); - {rollback, Details} -> - io:format("Rollback: ~p~n", [Details]); - {error, Type, Details} -> - io:format("Generic Error: ~p~n~p~n", [Type, Details]); - Other -> - io:format("Warning: Something else is going on ~p~n", [Other]) - end. diff --git a/server/lib/database/src/postgres_m.erl b/server/lib/database/src/postgres_m.erl index 6acf3d7..dc41b44 100644 --- a/server/lib/database/src/postgres_m.erl +++ b/server/lib/database/src/postgres_m.erl @@ -1,25 +1,40 @@ -module(postgres_m). -behaviour(monad). + -export(['>>='/2, return/1, fail/1]). --type(monad(_A) :: {{'ok', any(), any()}, 'select'} - | {{'ok', any()}, 'delete'} - | {{'ok', any()}, 'insert'} - | {{'ok', any(), any(), any()}, 'insert'} - | {{'ok', any()}, 'update'} - | {{'error', any()}, any()}). --include_lib("erlando/include/monad_specs.hrl"). +% This only makes sense if we export it or use dialyzer +%-type monad(_A) :: +% {{ok, any(), any()}, select} | +% {{ok, any()}, delete} | +% {{ok, any()}, insert} | +% {{ok, any(), any(), any()}, insert} | +% {{ok, any()}, update} | +% {{error, any()}, any()}. + +-spec return(X) -> Result + when X :: any(), + Result :: {ok, any()}. +return(X) -> + {ok, X}. -return(X) -> {ok, X}. -fail(X) -> {error, X}. +-spec fail(X) -> Result + when X :: any(), + Result :: {error, any()}. +fail(X) -> + {error, X}. -'>>='({{ok, FullColumns, Values}, select}, Fun) -> Fun({FullColumns, Values}); -'>>='({{ok, Count}, delete}, Fun) -> Fun(Count); -'>>='({{ok, Count}, insert}, Fun) -> Fun(Count); -'>>='({{ok, _, _, _}, insert}, _) -> fail("Unexpected use of Insert on Server side"); -'>>='({{ok, Count}, update}, Fun) -> Fun(Count); -'>>='({{error, Error}, Tag}, _) -> +'>>='({{ok, FullColumns, Values}, select}, Fun) -> + Fun({FullColumns, Values}); +'>>='({{ok, Count}, delete}, Fun) -> + Fun(Count); +'>>='({{ok, Count}, insert}, Fun) -> + Fun(Count); +'>>='({{ok, _, _, _}, insert}, _) -> + fail("Unexpected use of Insert on Server side"); +'>>='({{ok, Count}, update}, Fun) -> + Fun(Count); +'>>='({{error, Error}, Tag}, _) -> io:format("Tag: ~p\nError: ~p\n", [Error, Tag]), fail("Unexpected error (operation or PSQL) on Server side"). - diff --git a/server/rebar-deps.nix b/server/rebar-deps.nix index 771a3cb..f394e82 100644 --- a/server/rebar-deps.nix +++ b/server/rebar-deps.nix @@ -24,13 +24,13 @@ let }; beamDeps = [ ]; }; - erlando = builder { - name = "erlando"; - version = "3.1.1"; + erlandono = builder { + name = "erlandono"; + version = "3.1.2"; src = fetchHex { - pkg = "erlando"; - version = "3.1.1"; - sha256 = "sha256-ljPLSXjb14gn+nbqTDsL0kz71N2a9ICI6SLnEVSUo0Q="; + pkg = "erlandono"; + version = "3.1.2"; + sha256 = "sha256-ED+m50Ky73TIeBmi6P06r2O2tvjixibrEEZIhWVj514="; }; beamDeps = [ ]; }; diff --git a/server/rebar.config b/server/rebar.config index d337238..12e4a8d 100644 --- a/server/rebar.config +++ b/server/rebar.config @@ -12,7 +12,7 @@ {deps, [ {epgsql, "4.7.1"}, {pc, "1.15.0"}, - {erlando, "3.1.1"}, + {erlandono, "3.1.2"}, {csv, "3.0.3", {pkg, csve}} ]}. diff --git a/server/rebar.lock b/server/rebar.lock index 4f55803..bf0f306 100644 --- a/server/rebar.lock +++ b/server/rebar.lock @@ -1,20 +1,20 @@ {"1.2.0", [{<<"csv">>,{pkg,<<"csve">>,<<"3.0.3">>},0}, {<<"epgsql">>,{pkg,<<"epgsql">>,<<"4.7.1">>},0}, - {<<"erlando">>,{pkg,<<"erlando">>,<<"3.1.1">>},0}, + {<<"erlandono">>,{pkg,<<"erlandono">>,<<"3.1.2">>},0}, {<<"pc">>,{pkg,<<"pc">>,<<"1.15.0">>},0}, {<<"pure_migrations">>,{pkg,<<"pure_migrations">>,<<"1.3.0">>},0}]}. [ {pkg_hash,[ {<<"csv">>, <<"69E7D9B3FDC72016644368762C6A3E6CBFEB85BCCADBF1BD99AB6C827E360E04">>}, {<<"epgsql">>, <<"D4E47CAE46C18C8AFA88E34D59A9B4BAE16368D7CE1EB3DA24FA755EB28393EB">>}, - {<<"erlando">>, <<"50E54F9A9DFEC564DDFEB7FC7129F80F491856F5E07686F405CEF4E970BDFE46">>}, + {<<"erlandono">>, <<"65C546236E33946573C76349954D142DE8ED4986AC3D04BD1DBA7676E1E64F38">>}, {<<"pc">>, <<"0DE7D4DF1BB23897E27965FC134DC63156DD2E5B512A9536CB7004D984400A45">>}, {<<"pure_migrations">>, <<"B758A62A9EDD6C1B40B668F4D6CE9E5588DBD4D99A4469BCE46BC7089B594AF7">>}]}, {pkg_hash_ext,[ {<<"csv">>, <<"741D1A55AABADAA3E0FE13051050101A73E90C4570B9F9403A939D9546813521">>}, {<<"epgsql">>, <<"B6D86B7DC42C8555B1D4E20880E5099D6D6D053148000E188E548F98E4E01836">>}, - {<<"erlando">>, <<"9633CB4978DBD78827FA76EA4C3B0BD24CFBD4DD9AF48088E922E7115494A344">>}, + {<<"erlandono">>, <<"103FA6E742B2EF74C87819A2E8FD3AAF63B6B6F8E2C626EB104648856563E75E">>}, {<<"pc">>, <<"4C0FAD4F6437CAE353D517DA218FE78347B8FFA44B9817887494CAAAE54595B3">>}, {<<"pure_migrations">>, <<"00269DE0A1A9BBE1B7681EC1F3FA59EE9D8081582226A94C633E6F94B5C88BC3">>}]} ].