This repository has been archived by the owner on Jun 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdatabase.go
156 lines (132 loc) · 3.52 KB
/
database.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
const (
mySQLDateTimeFormat = "2006-01-02 15:04:05"
schema = `CREATE TABLE builds (
id varchar(255) NOT NULL DEFAULT '',
output text NOT NULL,
success tinyint(1) NOT NULL DEFAULT '0',
created_at datetime NOT NULL,
completed_at datetime NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY id_uniq (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;`
checkIfSchemaExists = `SELECT COUNT(*) as does_exist FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'builds';`
selectBuild = `SELECT * FROM builds WHERE id=?;`
insertBuild = `INSERT INTO builds (id, output, success, created_at, completed_at) VALUES (:id, :output, :success, :created_at, :completed_at);`
updateOutput = `UPDATE builds SET output = CONCAT_WS(CHAR(10 using utf8), output, :line) WHERE id = :id;`
updateBuild = `UPDATE builds SET output=:output, success=:success, created_at=:created_at, completed_at=:completed_at WHERE id=:id;`
completeBuild = `UPDATE builds SET completed_at = :completed_at WHERE id = :id;`
)
var (
db *sqlx.DB
dbConnString string
)
func mySQLFormattedTime() string {
return time.Now().UTC().Format(mySQLDateTimeFormat)
}
func init() {
flag.StringVar(&dbConnString, "db", os.Getenv("JEKYLL_BUILD_SERVER_DB_URL"), "Connection string for database. Leave blank to omit db logging. Default value is value of $JEKYLL_BUILD_SERVER_DB_URL in environment.")
}
type TableCheck struct {
DoesExist int `db:"does_exist"`
}
func InitDatabase() {
db = sqlx.MustConnect("mysql", dbConnString)
db.Ping()
var check TableCheck
err := db.Get(&check, checkIfSchemaExists)
if err != nil {
log.Fatalf("error connecting to the database: %v", err)
}
if check.DoesExist < 1 {
db.MustExec(schema)
}
}
type OutputUpdate struct {
Id string `db:"id"`
Line string `db:"line"`
}
type Build struct {
Id string `db:"id"`
Output string `db:"output"`
Success bool `db:"success"`
CreatedAt string `db:"created_at"`
CompletedAt string `db:"completed_at"`
saved bool
}
func (b *Build) Exists() bool {
return b.Get(b.Id) != nil || b.saved
}
func (b *Build) Get(id string) error {
if db == nil {
return nil
}
if id != "" {
b.Id = id
}
err := db.Get(b, selectBuild, id)
if err != nil {
log.Printf("[%s] db: Get() received an error: %v", b.Id, err)
b.saved = false
return err
}
b.saved = true
return nil
}
func (b *Build) UpdateOutput(line string) error {
if db == nil {
return nil
}
if b.saved {
_, err := db.NamedExec(updateOutput, &OutputUpdate{
Id: b.Id,
Line: line,
})
return err
} else {
return b.Save()
}
}
func (b *Build) Log(msg string) {
msg = fmt.Sprintf("%s %s", mySQLFormattedTime(), msg)
if b.Output == "" {
b.Output = msg
} else {
b.Output = b.Output + "\n" + msg
}
if err := b.UpdateOutput(msg); err != nil {
log.Printf("[%s] db: error saving log message: %v", b.Id, err)
}
}
func (b *Build) Save() error {
if db == nil {
return nil
}
if b.CompletedAt == "" {
b.CompletedAt = time.Unix(0, 0).UTC().Format(mySQLDateTimeFormat)
log.Printf("[%s]: db: new completed_at value: %s", b.Id, b.CompletedAt)
}
log.Printf("[%s] db: has been saved: %v", b.Id, b.saved)
var query string
if b.saved {
log.Printf("[%s] db: updating the build", b.Id)
query = updateBuild
} else {
log.Printf("[%s] db: inserting the build", b.Id)
query = insertBuild
}
_, err := db.NamedExec(query, b)
if err == nil {
b.saved = true
}
return err
}