-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.go
54 lines (46 loc) · 1.05 KB
/
migrate.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
package main
import (
"fmt"
"log"
"net"
"github.com/KellyLSB/demondin/models"
"github.com/jinzhu/gorm"
macaron "gopkg.in/macaron.v1"
)
func dbInit(
hostport, database,
username, password string,
) macaron.Handler {
return func(ctx *macaron.Context, logger *log.Logger) {
// Split the hostport
host, port, err := net.SplitHostPort(hostport)
if err != nil {
logger.Fatal(err)
}
// Connect to the database
db, err := gorm.Open("postgres", fmt.Sprintf(
"host=%s port=%s user=%s dbname=%s password=%s",
host, port, username, database, password,
))
if err != nil {
logger.Fatalf("DB Connection Failed: %s", err)
}
// Shutdown DB after request
defer db.Close()
// Set logger and migrate
db.LogMode(true)
db.SetLogger(logger)
dbMigrate(db)
// Map the DB Instance
ctx.Map(db)
ctx.Next()
}
}
func dbMigrate(db *gorm.DB) {
db.AutoMigrate(&models.M2M{})
db.AutoMigrate(&models.File{})
db.AutoMigrate(&models.Item{})
db.AutoMigrate(&models.Price{})
db.AutoMigrate(&models.Invoice{})
db.AutoMigrate(&models.Badge{})
}