-
Notifications
You must be signed in to change notification settings - Fork 21
/
db.go
98 lines (85 loc) · 1.65 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"database/sql"
"fmt"
"log"
"os"
_ "github.com/lib/pq"
)
var DB *sql.DB
func init() {
url := os.Getenv("DATABASE_URL")
if url == "" {
fmt.Println("WARNING: No database url specified, not connecting to postgres!")
return
}
var err error
DB, err = sql.Open("postgres", url)
if err != nil {
// ok if there IS a url then we're expected to be able to connect
// so if THAT fails, then that's a real error
panic(err)
}
err = DB.Ping()
if err != nil {
// apparently this DOUBLE CHECKS that it's up?
panic(err)
}
err = schema()
if err != nil {
// Failed to create table or something
panic(err)
}
}
func schema() (err error) {
_, err = DB.Exec(`
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
`)
if err != nil {
log.Println("Unable to load pgcrypto extension")
panic(err)
}
_, err = DB.Exec(`
CREATE TABLE IF NOT EXISTS mutes (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
discord_id TEXT NOT NULL,
channel_id TEXT,
expiration TIMESTAMP
);
`)
if err != nil {
log.Println("Unable to create mutes table")
panic(err)
}
_, err = DB.Exec(`
CREATE TABLE IF NOT EXISTS cringe (
image TEXT NOT NULL
)
`)
if err != nil {
log.Println("Unable to create cringe table")
panic(err)
}
_, err = DB.Exec(`
CREATE TABLE IF NOT EXISTS nicks (
id TEXT PRIMARY KEY,
nick SERIAL
)
`)
if err != nil {
log.Println("Unable to create nicks table")
panic(err)
}
_, err = DB.Exec(`
CREATE TABLE IF NOT EXISTS nicktrade (
id TEXT,
desirednick INTEGER,
UNIQUE(id, desirednick)
)
`)
if err != nil {
log.Println("Unable to create nicktrade table")
panic(err)
}
return
}