-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdbinit.go
104 lines (88 loc) · 2.7 KB
/
dbinit.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
99
100
101
102
103
104
package main
import "log"
func dbInit() {
db, err := openDB(".gcalsync.db")
if err != nil {
log.Fatalf("Error opening database: %v", err)
}
defer db.Close()
var dbVersion int
err = db.QueryRow("SELECT version FROM db_version WHERE name='gcalsync'").Scan(&dbVersion)
if err != nil {
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS db_version (
name TEXT PRIMARY KEY,
version INTEGER
)`)
if err != nil {
log.Fatalf("Error creating db_version table: %v", err)
}
_, err = db.Exec(`INSERT INTO db_version (name, version) VALUES ('gcalsync', 0)`)
if err != nil {
log.Fatalf("Error initializing db_version table: %v", err)
}
dbVersion = 0
}
if dbVersion == 0 {
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS tokens (
account_name TEXT PRIMARY KEY,
token TEXT)`)
if err != nil {
log.Fatalf("Error creating tokens table: %v", err)
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS calendars (
account_name TEXT,
calendar_id TEXT,
PRIMARY KEY (account_name, calendar_id))`)
if err != nil {
log.Fatalf("Error creating calendars table: %v", err)
}
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS blocker_events (
event_id TEXT,
calendar_id TEXT,
account_name TEXT,
origin_event_id TEXT,
PRIMARY KEY (calendar_id, origin_event_id)
)`)
if err != nil {
log.Fatalf("Error creating blocker_events table: %v", err)
}
dbVersion = 1
_, err = db.Exec(`UPDATE db_version SET version = 1 WHERE name = 'gcalsync'`)
if err != nil {
log.Fatalf("Error updating db_version table: %v", err)
}
}
if dbVersion == 1 {
_, err = db.Exec(`ALTER TABLE blocker_events ADD COLUMN last_updated TEXT`)
if err != nil {
log.Fatalf("Error adding last_updated column to blocker_events table: %v", err)
}
dbVersion = 2
_, err = db.Exec(`UPDATE db_version SET version = 2 WHERE name = 'gcalsync'`)
if err != nil {
log.Fatalf("Error updating db_version table: %v", err)
}
}
if dbVersion == 2 {
_, err = db.Exec(`ALTER TABLE blocker_events ADD COLUMN origin_calendar_id TEXT`)
if err != nil {
log.Fatalf("Error adding origin_calendar_id column to blocker_events table: %v", err)
}
dbVersion = 3
_, err = db.Exec(`UPDATE db_version SET version = 3 WHERE name = 'gcalsync'`)
if err != nil {
log.Fatalf("Error updating db_version table: %v", err)
}
}
if dbVersion == 3 {
_, err = db.Exec(`ALTER TABLE blocker_events ADD COLUMN response_status TEXT DEFAULT 'tentative'`)
if err != nil {
log.Fatalf("Error adding response_status column to blocker_events table: %v", err)
}
dbVersion = 4
_, err = db.Exec(`UPDATE db_version SET version = 4 WHERE name = 'gcalsync'`)
if err != nil {
log.Fatalf("Error updating db_version table: %v", err)
}
}
}