SmartMurphytt/chainmaker_go/database/db.go

29 lines
645 B
Go

package database
import (
"chainmaker_go/model"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var DB *gorm.DB
func InitDB() *gorm.DB {
// "username:password@tcp(host:port)/database?charset=utf8&parseTime=True&loc=Local"
dsn := "root:12345678@tcp(localhost:3306)/chainmaker?charset=utf8&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&model.User{})
db.AutoMigrate(&model.Resource{})
db.AutoMigrate(&model.RightConfirmation{})
db.AutoMigrate(&model.TransferOwnership{})
DB = db
return db
}
func GetDB() *gorm.DB {
return DB
}