-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.go
40 lines (30 loc) · 757 Bytes
/
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
package db
import (
"log"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
)
// Model definition
type TodoItem struct {
ID int `sql:"AUTO_INCREMENT" gorm:"primary_key"`
Description string `sql:"size:255;index"`
Deadline string
Progress int
}
// Database functions
// Create connection to sqlite3 database storing the todos.
func InitDB(databaseName string) *gorm.DB {
// Tries to connect to specified sqlite3 database.
db, err := gorm.Open("sqlite3", databaseName)
if err != nil {
log.Fatal(err)
}
// Check connection to database in order to be sure.
err = db.DB().Ping()
if err != nil {
log.Fatal(err)
}
// Check if todo table is present. If not, create it.
db.CreateTable(&TodoItem{})
return db
}