Files
2021-12-28 10:38:02 +08:00

45 lines
901 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.EngineInfo.Engines.Mysql.Database))
return nil
}