/
db.go
26 lines (23 loc) · 880 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
package heron
import (
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"github.com/shonenada/heron/models"
)
func GetDatabase() gorm.DB {
db, err := gorm.Open("mysql", Config.DatabaseURI)
if err != nil {
panic(err)
}
return db
}
func Migrate() {
db := GetDatabase()
db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&models.Account{})
db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&models.Event{})
db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&models.Follow{})
db.Model(&models.Event{}).AddForeignKey("account_id", "accounts(id)", "RESTRICT", "RESTRICT")
db.Model(&models.Follow{}).AddForeignKey("account_id", "accounts(id)", "RESTRICT", "RESTRICT")
db.Model(&models.Follow{}).AddForeignKey("follow_id", "accounts(id)", "RESTRICT", "RESTRICT")
db.AutoMigrate(&models.Account{}, &models.Follow{}, &models.Event{})
}