From 2abe2e5ef5f0bfa1ce526055cbb4ee64e075eea3 Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Wed, 30 Apr 2014 14:24:35 +0200 Subject: Add init functions, for testing. --- src/db.erl | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/db.erl b/src/db.erl index ef9c536..b534757 100644 --- a/src/db.erl +++ b/src/db.erl @@ -2,7 +2,8 @@ -behaviour(gen_server). %% API. --export([start_link/0, stop/0, init_tables/0]). +-export([start_link/0, stop/0]). +-export([init_db/0, init_db/1, init_tables/0, init_tables/1]). -export([add/1, find/1]). %% API for testing. -export([dump/1, destroy_tables/0, info_tables/0, dump_to_file/1]). @@ -13,14 +14,25 @@ -include_lib("stdlib/include/qlc.hrl"). -include("plop.hrl"). +%% @doc Set up a database schema on all nodes that are to be included +%% in the "database cluster". Has to be run _before_ mnesia has been +%% started. +init_db() -> + init_db([]). +init_db(Nodes) -> + mnesia:create_schema([node()] ++ Nodes), + init_tables(Nodes). + %% @doc Run once, or rather every time you start on a new database. %% If run more than once, we'll get {aborted, {already_exists, TABLE}}. init_tables() -> + init_tables([]). +init_tables(Nodes) -> %% We've once upon a time invoked mnesia:create_schema/1 with the %% nodes that will be part of the database. RamCopies = [], DiscCopies = [], - DiscOnlyCopies = [node()], + DiscOnlyCopies = [node()] ++ Nodes, mnesia:start(), mnesia:create_table(plop, [{type, set}, {ram_copies, RamCopies}, -- cgit v1.1