-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#63] Migrations schema creation. Migrate function.
- Loading branch information
Showing
8 changed files
with
145 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ examples/blog/log | |
*.o | ||
*.beam | ||
*.plt | ||
.rebar | ||
.rebar | ||
*.dump |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,8 @@ | |
{emysql, "0.*", {git, "[email protected]:Eonblast/Emysql.git", "master"}}, | ||
{emongo, ".*", {git, "[email protected]:marcelog/emongo.git", "marcelog_login_for_2_2_and_higher"}}, | ||
{'sqlite3', ".*", {git, "[email protected]:alexeyr/erlang-sqlite3.git", "HEAD"}}, | ||
{worker_pool, ".*", {git, "[email protected]:inaka/worker_pool.git", "master"}} | ||
{worker_pool, ".*", {git, "[email protected]:inaka/worker_pool.git", "master"}}, | ||
{sync, ".*", {git, "[email protected]:rustyio/sync.git", "master"}} | ||
]}. | ||
{xref_warnings, true}. | ||
{xref_checks, [undefined_function_calls, undefined_functions, locals_not_used, deprecated_function_calls, deprecated_functions]}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
-module(sumo_migration). | ||
|
||
-behavior(sumo_doc). | ||
|
||
-export([migrate/0, rollback/0]). | ||
|
||
-export([migration_update_list/1]). | ||
|
||
%%% sumo_db callbacks | ||
-export([sumo_schema/0, sumo_wakeup/1, sumo_sleep/1]). | ||
|
||
-callback up() -> string(). | ||
-callback down() -> string(). | ||
|
||
-type migration() :: atom(). | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%%% Exported | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec migrate() -> ok. | ||
migrate() -> | ||
ensure_sumo_migration_doc(), | ||
LastMigration = last_migration_update(), | ||
Migrations = migration_update_list(LastMigration), | ||
lists:foreach(fun(M) -> M:up() end, Migrations). | ||
|
||
-spec rollback() -> ok. | ||
rollback() -> | ||
ensure_sumo_migration_doc(), | ||
ok. | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%%% sumo_doc callbacks | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
-spec sumo_schema() -> sumo:schema(). | ||
sumo_schema() -> | ||
Fields = [sumo:new_field(version, string)], | ||
sumo:new_schema(?MODULE, Fields). | ||
|
||
-spec sumo_sleep(migration()) -> sumo:doc(). | ||
sumo_sleep(Migration) -> | ||
[{version, Migration}]. | ||
|
||
-spec sumo_wakeup(sumo:doc()) -> migration(). | ||
sumo_wakeup(Migration) -> | ||
VersionBin = proplists:get_value(version, Migration), | ||
binary_to_atom(VersionBin, utf8). | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%%% Internal | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
%% @doc Make sure the schema_version table is present in the datastore. | ||
-spec ensure_sumo_migration_doc() -> ok. | ||
ensure_sumo_migration_doc() -> | ||
sumo:create_schema(?MODULE). | ||
|
||
-spec last_migration_update() -> migration(). | ||
last_migration_update() -> | ||
Migrations = sumo:find_all(?MODULE), | ||
lists:max(Migrations). | ||
|
||
-spec migration_update_list(migration()) -> [migration()]. | ||
migration_update_list(LastMigration) -> | ||
MigrationsDir = migrations_dir(), | ||
Files = filelib:wildcard("*.erl", MigrationsDir), | ||
F = compose([fun filename:rootname/1, fun list_to_atom/1]), | ||
AvailableMigrations = lists:map(F, Files), | ||
lists:filter(fun(X) -> X > LastMigration end, AvailableMigrations). | ||
|
||
-spec migrations_dir() -> string(). | ||
migrations_dir() -> | ||
case application:get_env(migrations_dir, sumo_db) of | ||
undefined -> "src/migrations"; | ||
Dir -> Dir | ||
end. | ||
|
||
-spec compose([fun()]) -> fun(). | ||
compose(Funs) -> | ||
Compose = fun(F, X) -> F(X) end, | ||
fun(X) -> | ||
lists:foldl(Compose, X, Funs) | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-module('20140901_mig1'). | ||
|
||
-behavior(sumo_migration). | ||
|
||
-export([up/0, down/0]). | ||
|
||
up() -> | ||
io:format("=== ~p up/0~n", [?MODULE]), | ||
ok. | ||
|
||
down() -> | ||
io:format("=== ~p down/0~n", [?MODULE]), | ||
ok. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-module('20140902_mig'). | ||
|
||
-behavior(sumo_migration). | ||
|
||
-export([up/0, down/0]). | ||
|
||
up() -> | ||
io:format("=== ~p up/0~n", [?MODULE]), | ||
ok. | ||
|
||
down() -> | ||
io:format("=== ~p down/0~n", [?MODULE]), | ||
ok. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-module('20140903_mig'). | ||
|
||
-behavior(sumo_migration). | ||
|
||
-export([up/0, down/0]). | ||
|
||
up() -> | ||
io:format("=== ~p up/0~n", [?MODULE]), | ||
ok. | ||
|
||
down() -> | ||
io:format("=== ~p down/0~n", [?MODULE]), | ||
ok. |