1
0
mirror of https://github.com/UzixLS/migresia.git synced 2025-07-19 07:11:28 +03:00

Massive overhaul to allow for multinode scenarios, and predictable return types.

This commit is contained in:
Christopher Phillips
2013-11-21 09:42:56 -05:00
parent f28ce61ad3
commit 36ed822453
2 changed files with 87 additions and 71 deletions

View File

@ -24,41 +24,46 @@
-module(migresia).
-export([create_new_migration/1, create_new_migration/2, check/0, check/1, migrate/0, migrate/1, list_nodes/0]).
-export([create_new_migration/2, check/1, migrate/1, list_disc_copy_nodes/0]).
-spec create_new_migration(string()) -> any().
create_new_migration(Description) ->
create_new_migration(undefined, Description).
-define(TABLE, schema_migrations).
-spec create_new_migration(atom(), string()) -> any().
create_new_migration(App, Description) ->
{{Year, Month, Day}, {Hour, Minute, Second}} = calendar:local_time(),
Filename = lists:flatten(io_lib:format("~w~2.2.0w~2.2.0w~2.2.0w~2.2.0w~2.2.0w_", [Year, Month, Day, Hour, Minute, Second]) ++ Description),
FullPathAndExtension = filename:join(migresia_migrations:get_priv_dir(App), Filename ++ ".erl"),
Path = migresia_migrations:get_priv_dir(App),
FullPathAndExtension = filename:join(Path, Filename ++ ".erl"),
io:format("Creating new migration: ~p~n", [FullPathAndExtension]),
file:write_file(FullPathAndExtension, io_lib:fwrite("-module(~p).~n-behavior(db_migration).~n-export([up/0, down/0]). ~n~nup() -> ok.~n~ndown() -> throw(<<\"Downgraders not implemented.\">>).", [list_to_atom(Filename)])),
ok = filelib:ensure_dir(<<Path/binary, <<"/">>/binary>>),
ok = file:write_file(FullPathAndExtension, io_lib:fwrite("-module(~p).~n-behavior(db_migration).~n-export([up/0, down/0]). ~n~nup() -> ok.~n~ndown() -> throw(<<\"Downgraders not implemented.\">>).", [list_to_atom(Filename)])),
io:format("Migration written.~n~n").
-spec check(atom()) -> any().
-spec check(atom()) -> ok | {error, any()}.
check(App) ->
Loaded = migresia_migrations:list_unapplied_ups(App),
if Loaded == [] ->
io:format("No migrations to apply.~n", []);
true ->
io:format("Migrations to apply: ~p~n", [ [X||{X,_} <- Loaded] ])
case start_mnesia(false) of
ok ->
Loaded = migresia_migrations:list_unapplied_ups(App),
if Loaded == [] ->
[];
true ->
[ [X||{X,_} <- Loaded] ]
end;
{error, Error} -> {error, Error}
end.
-spec check() -> any().
check() -> check(undefined).
-spec migrate(atom()) -> any().
-spec migrate(atom()) -> ok | {error, any()}.
migrate(App) ->
Loaded = migresia_migrations:list_unapplied_ups(App),
lists:foreach(fun execute_up/1, Loaded).
-spec migrate() -> any().
migrate() ->
migrate(undefined).
case start_mnesia(true) of
ok ->
case migresia_migrations:ensure_schema_table_exists() of
ok ->
Loaded = migresia_migrations:list_unapplied_ups(App),
lists:foreach(fun execute_up/1, Loaded);
{error, Error} -> Error
end;
{error, Error} -> {error, Error}
end.
execute_up({Module, Short}) ->
io:format("Executing up in ~s...~n", [Module]),
@ -66,6 +71,27 @@ execute_up({Module, Short}) ->
mnesia:dirty_write(schema_migrations, {schema_migrations, Short, true}),
io:format(" => done~n", []).
list_nodes() ->
mnesia:table_info(schema, disc_copies).
-spec start_mnesia(boolean()) -> ok | {error, any()}.
start_mnesia(RemoteToo) ->
io:format("Starting Mnesia...~n", []),
case application:ensure_started(mnesia) of
Other when Other /= ok -> io:format(" => Error:~p~n", [Other]), Other;
ok ->
case RemoteToo of
false -> ok;
true -> {ResultList, BadNodes} = rpc:multicall(list_all_nodes(), application, ensure_started, [mnesia]),
BadStatuses = [X || X <- ResultList, X /= ok],
if BadNodes /= [] -> io:format(" => Error:~p~n", [not_all_nodes_running]), {error, not_all_nodes_running};
BadStatuses /= [] -> io:format(" => Error:~p~n", [BadStatuses]), {error, BadStatuses};
true -> io:format(" => started~n", []), ok
end
end
end.
list_all_nodes() ->
mnesia:table_info(schema, all_nodes).
list_disc_copy_nodes() ->
mnesia:table_info(schema, disc_copies).