forked from ChicagoBoss/ChicagoBoss
-
Notifications
You must be signed in to change notification settings - Fork 0
Database Setup
timClicks edited this page Aug 18, 2012
·
2 revisions
Once you move a system from the mock
database adapter, you will need to create the tables for the database of your choice.
Here is a script that will inspect your models and build tables for you. It lives in /priv/init/
, but requires that you change the -module()
declaration and the APPNAME
definition to meet your application's name. Note that migrate/0
, rebuild/0
and recreate_table/1
are helper commands that may be useful during development.
-module(greetings_02_db).
-export([init/0, migrate/0, rebuild/0, recreate_table/1]).
-define(APPNAME, greetings).
-define(MODELS, [list_to_atom(M) || M <- boss_files:model_list(?APPNAME)]).
-define(NODES, [node()]).
init() ->
mnesia:stop(),
mnesia:create_schema(?NODES),
mnesia:change_table_copy_type(schema, node(), disc_copies),
mnesia:start(),
ExistingTables = mnesia:system_info(tables),
TablesToCreate = (?MODELS ++ ['_ids_']) -- ExistingTables,
[create_table(T) || T <- TablesToCreate],
{ok, []}.
create_table('_ids_') ->
create_table(?NODES, '_ids_', [type, id, count]);
create_table(Model) ->
io:format("Installing table ~p~n",[Model]),
DummyRecord = boss_record_lib:dummy_record(Model),
Attributes = DummyRecord:attribute_names(),
create_table(?NODES, Model, Attributes).
create_table(Nodes, Table, Attributes) ->
mnesia:create_table(Table, [{attributes, Attributes},
{disc_copies, Nodes}]).
migrate() ->
mnesia:stop(),
init().
rebuild() ->
mnesia:stop(),
mnesia:delete_schema(?NODES),
init().
recreate_table(Model) ->
mnesia:delete_table(Model),
create_table(Model).