c8367761101c368360edaba78f169fd4249b961c
[euphorik.git] / euphorik_bd.erl
1 % Module de création de la base de données euphorik.
2 % @author G.Burri
3
4 -module(euphorik_bd).
5 -export([
6 create/0,
7 create_tables/0,
8 vers_version/1,
9 peupler/0
10 ]).
11
12 -include("../include/euphorik_bd.hrl").
13
14
15 % Instructions pour créer une nouvelle base :
16 % $erl -sname yaws -mnesia dir '"/projets/euphorik/BD"'
17 % voir doc/installation.txt
18 % >l(euphorik_bd).
19 % >euphorik_bd:create().
20 create() ->
21 mnesia:stop(),
22 mnesia:delete_schema([node()]),
23 mnesia:create_schema([node()]), % nécessaire pour les tables sur disc
24 mnesia:start(),
25 create_tables().
26
27
28 create_tables() ->
29 mnesia:create_table(counter, [
30 {attributes, record_info(fields, counter)},
31 {disc_copies, [node()]}
32 ]),
33 mnesia:create_table(minichat, [
34 {attributes, record_info(fields, minichat)},
35 {index, [auteur_id]},
36 {disc_copies, [node()]}
37 ]),
38 mnesia:create_table(reponse_minichat, [
39 {type, bag},
40 {attributes, record_info(fields, reponse_minichat)},
41 {index, [cible]},
42 {disc_copies, [node()]}
43 ]),
44 mnesia:create_table(user, [
45 {attributes, record_info(fields, user)},
46 {index, [cookie, login]},
47 {disc_copies, [node()]}
48 ]).
49
50
51 vers_version(2) ->
52 mnesia:transform_table(
53 user,
54 fun({user, Id, Cookie, Pseudo, Date_creation, Date_derniere_connexion, Css}) ->
55 {user, Id, Cookie, Pseudo, "", "", "", Date_creation, Date_derniere_connexion, Css}
56 end,
57 record_info(fields, user),
58 user
59 );
60 vers_version(3) ->
61 mnesia:transform_table(
62 user,
63 fun({user, Id, Cookie, Pseudo, Login, Password, Email, Date_creation, Date_derniere_connexion, Css}) ->
64 {user, Id, Cookie, Pseudo, Login, Password, Email, Date_creation, Date_derniere_connexion, Css, 0}
65 end,
66 record_info(fields, user),
67 user
68 );
69 vers_version(4) ->
70 mnesia:transform_table(
71 user,
72 fun({user, Id, Cookie, Pseudo, Login, Password, Email, Date_creation, Date_derniere_connexion, Css, Indice_flood}) ->
73 {user, Id, Cookie, Pseudo, Login, Password, Email, Date_creation, Date_derniere_connexion, Css, Indice_flood, []}
74 end,
75 record_info(fields, user),
76 user
77 );
78 % Ajout du numéro de page de la conv principale
79 vers_version(5) ->
80 mnesia:transform_table(
81 user,
82 fun({user, Id, Cookie, Pseudo, Login, Password, Email, Date_creation, Date_derniere_connexion, Css, Indice_flood, Conversations}) ->
83 {user, Id, Cookie, Pseudo, Login, Password, Email, Date_creation, Date_derniere_connexion, Css, Indice_flood, 1, Conversations}
84 end,
85 record_info(fields, user),
86 user
87 );
88 % Ajout le format d'affichage des pseudos
89 vers_version(6) ->
90 mnesia:transform_table(
91 user,
92 fun({user, Id, Cookie, Pseudo, Login, Password, Email, Date_creation, Date_derniere_connexion, Css, Indice_flood, Page_Principale, Conversations}) ->
93 {user, Id, Cookie, Pseudo, Login, Password, Email, Date_creation, Date_derniere_connexion, Css, nick, Indice_flood, Page_Principale, Conversations}
94 end,
95 record_info(fields, user),
96 user
97 ).
98
99
100 % exemple de peuplage de la BD, utilisé pour les tests
101 peupler() ->
102 mnesia:transaction(
103 fun() ->
104 mnesia:write({minichat, now(), "Pierre", "Salut tout le monde"}),
105 mnesia:write({minichat, now(), "Paul", "Salut à toi !"})
106 end
107 ).