/* @Time : 2020/6/28 21:46 @Author : xuyiqing @File : init.py */ package models import ( "fmt" "go_blog/conf" "go_blog/pkg/util" "time" "gorm.io/gorm" ) var DB *gorm.DB func SetUp(isOrmDebug bool) { conUri := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=True&loc=Local", conf.DataBase.User, conf.DataBase.Password, conf.DataBase.Host, conf.DataBase.Port, conf.DataBase.DB, conf.DataBase.Charset) db, err := gorm.Open(conf.DataBase.Type, conUri) if err != nil { panic(err) } DB = db DB.LogMode(isOrmDebug) gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string { return conf.DataBase.Prefix + defaultTableName } DB.AutoMigrate(&Account{}) DB.AutoMigrate(&Content{}) } type BaseModel struct { ID uint64 `gorm:"primary_key'" json:"id"` CreatedAt time.Time `json:"-"` UpdatedAt time.Time `json:"-"` DeletedAt *time.Time `sql:"index" json:"-"` } // 生成全局唯一ID func (m *BaseModel) BeforeCreate(scope *gorm.Scope) error { if m.ID == 0 { m.ID = util.GenSonyFlakeId() } return nil }