2023-03-23 15:58:12 +08:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2023-03-24 08:49:41 +08:00
|
|
|
"fmt"
|
2023-03-23 15:58:12 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
"xorm.io/xorm"
|
|
|
|
"xorm.io/xorm/dialects"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
DB *xorm.Engine
|
|
|
|
)
|
|
|
|
|
|
|
|
func New(driveName, dsn string, showSql bool) {
|
|
|
|
DB, _ = NewDbConnection(driveName, dsn)
|
|
|
|
DB.ShowSQL(showSql)
|
|
|
|
DB.Dialect().SetQuotePolicy(dialects.QuotePolicyReserved)
|
|
|
|
err := DB.Sync2(
|
|
|
|
&IssueModel{},
|
|
|
|
&Model{},
|
|
|
|
&Node{},
|
|
|
|
&NodeState{},
|
|
|
|
&Task{},
|
2023-04-24 15:14:04 +08:00
|
|
|
&TaskLog{},
|
2023-03-23 15:58:12 +08:00
|
|
|
&TaskResult{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2023-03-24 08:49:41 +08:00
|
|
|
fmt.Println("同步数据库表结构", err)
|
2023-03-23 15:58:12 +08:00
|
|
|
zap.L().Error("同步数据库表结构", zap.Error(err))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDbConnection(driveName, dsn string) (db *xorm.Engine, err error) {
|
|
|
|
db, err = xorm.NewEngine(driveName, dsn)
|
|
|
|
if err != nil {
|
|
|
|
zap.L().Error("创建数据库连接", zap.Error(err))
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
db.SetMaxOpenConns(300)
|
|
|
|
return
|
|
|
|
}
|