-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
70 lines (57 loc) · 1.41 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"context"
"os"
)
// TABLES
// users - recipients of alerts
// tokens - session tokens and their owners
// channels - message channels to listen and send to
// subs - user->channel relations to determine where messages go
var setupSQL string = `begin;
create table if not exists users (
id serial primary key,
name text unique not null,
email text unique not null
);
create table if not exists tokens (
hash text primary key,
uid int,
expires timestamp,
constraint fk_tokens_uid foreign key(uid) references users(id) on delete cascade
);
create table if not exists channels (
id bigserial primary key,
name text unique not null
);
insert into channels(name) values('admin');
create table if not exists subs (
id bigint serial primary key,
uid int,
cid int,
constraint fk_subs_uid foreign key(uid) references users(id) on delete cascade,
constraint fk_subs_cid foreign key(cid) references channels(id) on delete cascade
);
create index idx_subs on subs(uid,cid);
commit;
`
func (srv *Shoutyface) createTables() error {
srv.L("Initialising database.")
_, err := srv.dbp.Exec(context.Background(), setupSQL)
if err != nil {
return err
}
admintoken := os.Getenv("ADMIN_TOKEN")
if admintoken != "" {
println(admintoken)
err = srv.addUser("admin", os.Getenv("ADMIN_EMAIL"))
if err != nil {
return err
}
err = srv.AddToken(admintoken, "admin")
if err != nil {
return err
}
}
return nil
}