This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialize.go
86 lines (71 loc) · 1.46 KB
/
initialize.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
package storage
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
// Initialize runs initialization scripts to create the tables necessary for the blog
// functionality. These scripts are non-destructive if data already exists.
func Initialize(db *sql.DB) error {
writeDB, err := db.Begin()
if err != nil {
writeDB.Rollback()
return err
}
if err = initializePosts(writeDB); err != nil {
writeDB.Rollback()
return err
}
if err = initializeUsers(writeDB); err != nil {
writeDB.Rollback()
return err
}
if err = initializeImages(writeDB); err != nil {
writeDB.Rollback()
return err
}
return writeDB.Commit()
}
func initializePosts(db *sql.Tx) error {
statement := `
CREATE TABLE IF NOT EXISTS posts (
id BLOB PRIMARY KEY,
tag TEXT,
title TEXT,
content_md TEXT,
content_html TEXT,
published TIMESTAMP,
edited TIMESTAMP,
is_favorite BOOLEAN,
author BLOB
);
`
_, err := db.Exec(statement)
return err
}
func initializeUsers(db *sql.Tx) error {
statement := `
CREATE TABLE IF NOT EXISTS users (
id BLOB PRIMARY KEY,
first_name TEXT,
last_name TEXT,
email TEXT,
location TEXT,
catch_phrase TEXT,
login TEXT,
hashed_pw TEXT
);
`
_, err := db.Exec(statement)
return err
}
func initializeImages(db *sql.Tx) error {
statement := `
CREATE TABLE IF NOT EXISTS images (
id BLOB PRIMARY KEY,
uploaded TIMESTAMP,
path TEXT
);
`
_, err := db.Exec(statement)
return err
}