修复访问日志表可能无法自动创建的Bug

This commit is contained in:
GoEdgeLab
2022-03-23 10:05:42 +08:00
parent 26f22a39c9
commit b4d0f49802
4 changed files with 46 additions and 12 deletions

View File

@@ -1,7 +1,10 @@
package models
import (
"github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
"strings"
"sync"
)
@@ -38,3 +41,23 @@ func NewQuery(tx *dbs.Tx, dao dbs.DAOWrapper, adminId int64, userId int64) *dbs.
}
return query
}
// CheckSQLErrCode 检查数据库错误代码
func CheckSQLErrCode(err error, code uint16) bool {
if err == nil {
return false
}
// 快速判断错误方法
mysqlErr, ok := err.(*mysql.MySQLError)
if ok && mysqlErr.Number == code { // Error 1050: Table 'xxx' already exists
return true
}
// 防止二次包装过程中错误丢失的保底错误判断方法
if strings.Contains(err.Error(), "Error "+types.String(code)) {
return true
}
return false
}