-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql-lite.go
65 lines (57 loc) · 1.26 KB
/
sql-lite.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
package storage
import (
"context"
"database/sql"
"log"
"path/filepath"
_ "github.com/mattn/go-sqlite3"
)
const databaseFile = "storage.db"
func GetConnection() *sql.Conn {
db, err := sql.Open("sqlite3", filepath.Join(getDirPath(), databaseFile))
if err != nil {
log.Fatalln("Could not open connection", err)
}
con, err := db.Conn(context.Background())
if err != nil {
log.Fatal("Could not connect", err)
}
return con
}
func Migrate() {
con := GetConnection()
authenticationTable := `
CREATE TABLE IF NOT EXISTS authentication(
type TEXT PRIMARY KEY,
json TEXT
);
`
_, err := con.ExecContext(context.Background(), authenticationTable)
if err != nil {
log.Fatal("Could not execute query", err)
}
configTable := `
CREATE TABLE IF NOT EXISTS config(
property_key TEXT,
property_value TEXT,
UNIQUE(property_key)
);
`
_, err = con.ExecContext(context.Background(), configTable)
if err != nil {
log.Fatal("Could not execute query", configTable, err)
}
favoritesTable := `
CREATE TABLE IF NOT EXISTS favorites(
id INT PRIMARY KEY,
name TEXT,
sheetId TEXT,
UNIQUE(name)
);
`
_, err = con.ExecContext(context.Background(), favoritesTable)
if err != nil {
log.Fatal("Could not execute query", favoritesTable, err)
}
return
}