1
0
mirror of https://github.com/UzixLS/migresia.git synced 2025-07-18 23:01:21 +03:00

Move to lager for logging

This commit is contained in:
Uzix
2017-11-16 11:58:12 +03:00
parent 92de01c8ea
commit 0e1a8eb25a
3 changed files with 21 additions and 18 deletions

View File

@ -30,6 +30,6 @@
{vsn, "0.0.4"},
{modules, [migresia, migresia_migrations]},
{registered, []},
{applications, [kernel, stdlib]}
{applications, [kernel, stdlib, lager]}
]
}.

View File

@ -24,6 +24,8 @@
-module(migresia).
-compile({parse_transform, lager_transform}).
-export([start_all_mnesia/0,
list_nodes/0,
list_migrations/0,
@ -37,12 +39,12 @@
-spec start_all_mnesia() -> ok | {error, any()}.
start_all_mnesia() ->
io:format("Starting Mnesia...~n", []),
lager:info("Starting Mnesia..."),
case ensure_started(mnesia) of
ok ->
ensure_started_on_remotes(list_nodes());
Err ->
io:format(" => Error:~p~n", [Err]),
lager:error(" => Error:~p", [Err]),
Err
end.
@ -53,16 +55,16 @@ list_migrations() ->
migresia_migrations:list_migrations().
ensure_started_on_remotes(Nodes) ->
io:format("Ensuring Mnesia is running on nodes:~n~p~n", [Nodes]),
lager:info("Ensuring Mnesia is running on nodes: ~p", [Nodes]),
{ResL, BadNodes} = rpc:multicall(Nodes, migresia, ensure_started, [mnesia]),
handle_err([X || X <- ResL, X /= ok], BadNodes).
handle_err([], []) ->
io:format(" => started~n", []),
lager:info(" => started"),
ok;
handle_err(Results, Bad) ->
if Results /= [] -> io:format(" => Error, received: ~p~n", [Results]) end,
if Bad /= [] -> io:format(" => Error, bad nodes: ~p~n", [Bad]) end,
if Results /= [] -> lager:error(" => Error, received: ~p", [Results]) end,
if Bad /= [] -> lager:error(" => Error, bad nodes: ~p", [Bad]) end,
{error, mnesia_not_started}.
-spec ensure_started(atom()) -> ok | {error, any()}.
@ -99,7 +101,7 @@ migrate(Srcs) ->
end.
migrate1(Srcs) ->
io:format("Waiting for tables (max timeout 2 minutes)...~n", []),
lager:debug("Waiting for tables (up to 2 minutes)..."),
ok = mnesia:wait_for_tables(mnesia:system_info(tables), 120000),
case migresia_migrations:list_unapplied_ups(Srcs) of
{error, _} = Err -> Err;
@ -124,7 +126,7 @@ rollback(Srcs, Time) ->
end.
rollback1(Srcs, Time) ->
io:format("Waiting for tables (max timeout 2 minutes)...~n", []),
lager:debug("Waiting for tables (up to 2 minutes)..."),
ok = mnesia:wait_for_tables(mnesia:system_info(tables), 120000),
case migresia_migrations:list_applied_ups(Srcs, Time) of
{error, _} = Err -> Err;

View File

@ -24,6 +24,8 @@
-module(migresia_migrations).
-compile({parse_transform, lager_transform}).
-export([init_migrations/0,
list_migrations/0,
list_unapplied_ups/1,
@ -47,10 +49,10 @@ init_migrations() ->
true ->
ok;
false ->
io:format("Table schema_migration not found, creating...~n", []),
lager:info("Table ~w not found, creating", [?TABLE]),
Attr = [{type, ordered_set}, {disc_copies, migresia:list_nodes()}],
case mnesia:create_table(?TABLE, Attr) of
{atomic, ok} -> io:format(" => created~n", []);
{atomic, ok} -> ok;
{aborted, Reason} -> throw({error, Reason})
end
end.
@ -159,10 +161,9 @@ check_table() ->
false ->
[];
true ->
io:format("Waiting for tables...~n", []),
lager:info("Waiting for tables"),
case mnesia:wait_for_tables([?TABLE], 5000) of
ok ->
io:format(" => done~n", []),
Select = [{{?TABLE,'_','_'},[],['$_']}],
List = mnesia:dirty_select(?TABLE, Select),
[ X || {?TABLE, X, true} <- List ];
@ -177,7 +178,7 @@ load_migration(Dir, Filename) ->
{module, Module} ->
{Module, code:get_object_code(Module)};
{error, _} = Err ->
io:format("Error when loading module ~p.~n", [Filepath]),
lager:error("Error when loading module ~p", [Filepath]),
throw(Err)
end.
@ -196,13 +197,13 @@ get_ts_bef_last1(List) -> hd(tl(lists:reverse(List))).
%%------------------------------------------------------------------------------
execute_up({Module, Ts}) ->
io:format("Executing up in ~s...~n", [Module]),
lager:info("Executing ~s:up", [Module]),
Module:up(),
mnesia:dirty_write(?TABLE, {?TABLE, Ts, true}),
io:format(" => done~n", []).
lager:info(" => done").
execute_down({Module, Ts}) ->
io:format("Executing down in ~s...~n", [Module]),
lager:info("Executing ~s:down", [Module]),
Module:down(),
mnesia:dirty_delete(?TABLE, Ts),
io:format(" => done~n", []).
lager:info(" => done").