feat:完善信息
This commit is contained in:
44
app/api/manage/controller/tenant/sub.go
Normal file
44
app/api/manage/controller/tenant/sub.go
Normal file
@ -0,0 +1,44 @@
|
||||
package tenant
|
||||
|
||||
import (
|
||||
model2 "SciencesServer/app/common/model"
|
||||
|
||||
"SciencesServer/config"
|
||||
"fmt"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Sub struct{}
|
||||
|
||||
type SubHandle func() *Sub
|
||||
|
||||
// database 数据表
|
||||
func (c *Sub) database(key string) string {
|
||||
return model2.SubDatabase + "_" + key
|
||||
}
|
||||
|
||||
// sync 同步数据
|
||||
func (c *Sub) sync(tx *gorm.DB, database string) error {
|
||||
// TODO:生成租户对应数据库,并生成对应数据表
|
||||
err := tx.Exec(fmt.Sprintf("CREATE DATABASE %s;", database)).Error
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 使用生成后的数据库
|
||||
if err = tx.Exec(fmt.Sprintf("use %s;", database)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
iModels := []model2.IModel{}
|
||||
|
||||
for _, v := range iModels {
|
||||
if err = tx.Migrator().CreateTable(v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// 重新使用默认的数据库
|
||||
tx.Exec(fmt.Sprintf("use %s;", config.SettingInfo.Engine.Mysql.DBName))
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user