-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.go
63 lines (53 loc) · 1.88 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mssql"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/sirupsen/logrus"
)
//User as in database
type User struct {
Name string `gorm:"size:60; not null"`
Email string `gorm:"primary_key"`
PasswordHash string `gorm:"size:100; not null"`
PasswordDate time.Time `gorm:"not null"`
ActivationDate *time.Time
WrongPasswordCount uint8 `gorm:"not null; default:0"`
WrongPasswordDate *time.Time
PasswordValidUntil *time.Time
CreationDate time.Time `gorm:"not null; default:CURRENT_TIMESTAMP"`
LastTokenType *string
LastTokenDate *time.Time
Enabled uint8 `gorm:"not null; default:1"`
}
func initDB() (*gorm.DB, error) {
connectString := opt.dbSqliteFile
switch opt.dbDialect {
case "mysql":
connectString = fmt.Sprintf("%s:%s@(%s)/%s?charset=utf8&parseTime=True&loc=Local",
opt.dbUsername, opt.dbPassword, opt.dbHost, opt.dbName)
case "postgres":
connectString = fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s",
opt.dbHost, opt.dbPort, opt.dbUsername, opt.dbName, opt.dbPassword)
case "mssql":
connectString = fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s",
opt.dbUsername, opt.dbPassword, opt.dbHost, opt.dbPort, opt.dbName)
}
logrus.Infof("Initializing database. dialect=%s; dbname=%s; dbhost=%s; dbport=%d", opt.dbDialect, opt.dbName, opt.dbHost, opt.dbPort)
db0, err := gorm.Open(opt.dbDialect, connectString)
if err != nil {
logrus.Errorf("Couldn't connect to database. err=%s", err)
return db0, err
}
if opt.logLevel == "debug" {
db0.LogMode(true)
}
db0.Set("gorm:table_options", "charset=utf8")
logrus.Infof("Checking database schema")
db0.AutoMigrate(&User{})
return db0, nil
}