代码拉取完成,页面将自动刷新
package handler
import (
"database/sql"
"fmt"
"gitee.com/scottq/go-framework/src/utils"
"io/ioutil"
"log"
"strings"
)
type DbVersionInfo struct {
Version string
SqlContent string
Comment string
}
type IVersionMagV2 interface {
AppendVersionDir(sqlDir string) error
AppendVersion(v DbVersionInfo)
AppendSqlContent(version string, content string)
Upgrade(version string) error
UpgradeOne(vInfo DbVersionInfo) error
Version() (string, error)
SetLabel(label string)
Label() string
ILogger
}
type VersionMagV2 struct {
versions []DbVersionInfo
db *sql.DB
label string
optLock IOptimisticLock
_logger logFunc
}
func NewDBVersionMagV2(db *sql.DB) (IVersionMagV2, error) {
lock, err := NewOptimisticLock(db, 60)
if err != nil {
return nil, err
}
mag := &VersionMagV2{
db: db,
optLock: lock,
versions: []DbVersionInfo{},
}
if err := mag.init(); err != nil {
return nil, err
}
return mag, nil
}
func (this *VersionMagV2) init() error {
var err error
_, err = this.db.Exec(CREATE_VERSION_TABLE_SQL)
if err != nil {
this.logError(err)
return err
}
return nil
}
func (this *VersionMagV2) AddLogger(logger logFunc) {
this._logger = logger
}
func (this *VersionMagV2) AppendVersion(v DbVersionInfo) {
this.versions = append(this.versions, v)
}
func (this *VersionMagV2) AppendSqlContent(version string, content string) {
this.versions = append(this.versions, DbVersionInfo{
Version: version,
SqlContent: content,
})
}
func (this *VersionMagV2) AppendVersionDir(sqlDir string) error {
files, err := ioutil.ReadDir(sqlDir)
if err != nil {
return err
}
//
for _, x := range files {
if x.IsDir() {
continue
}
sqlFile := x.Name()
sqlFileFull := sqlDir + "/" + sqlFile
upVersion := strings.ReplaceAll(sqlFile, ".sql", "")
upVersion = strings.TrimLeft(upVersion, "v")
//执行升级文件
bytes, err := ioutil.ReadFile(sqlFileFull)
if err != nil {
return err
}
execSql := string(bytes)
this.AppendSqlContent(upVersion, execSql)
}
return nil
}
//升级版本
func (this *VersionMagV2) Upgrade(version string) error {
if version == "" {
version="v99.99.99"
}
this.logInfo("version upgrade to " + version)
lockName := this.lockName()
if !this.optLock.Lock(lockName) {
this.logInfo("upgrade job has locked: " + lockName)
return nil
}
defer this.optLock.UnLock(lockName)
//
versions:=this.versions
for index, v := range versions {
upVersion := v.Version
if utils.CompareVersion(upVersion, version) > 0 {
continue
}
err := this.UpgradeOne(versions[index])
if err != nil {
return err
}
}
return nil
}
func (this *VersionMagV2) SetLabel(label string) {
this.label = label
}
func (this *VersionMagV2) Label() string {
return this.label
}
func (this *VersionMagV2) lockName() string {
return "sql_upgrade." + this.label
}
func (this *VersionMagV2) logInfo(info string) {
if this._logger == nil {
log.Println(info)
return
}
this._logger(info, nil)
}
func (this *VersionMagV2) logError(err error) {
if this._logger == nil {
log.Println(err.Error())
return
}
this._logger("", err)
}
//升级一次版本
func (this *VersionMagV2) UpgradeOne(vInfo DbVersionInfo) error {
var err error
version, err := this.Version()
if err != nil {
return err
}
upVersion := vInfo.Version
//无需处理
if utils.CompareVersion(vInfo.Version, version) <= 0 {
this.logInfo("no need upgrade: " + upVersion)
return nil
}
err = this.upgrade(vInfo)
if err != nil {
return err
}
this.logInfo(fmt.Sprintf("upgrade success %s => %s", version, upVersion))
return nil
}
func (this *VersionMagV2) upgrade(vInfo DbVersionInfo) error {
var err error
sqlContent := vInfo.SqlContent
if sqlContent == "" {
return fmt.Errorf("exec upgrade [%s] content is empty", vInfo.Version)
}
_, err = this.db.Exec(sqlContent)
if err != nil {
this.logError(err)
return err
}
//保存version信息
err = this.addVersionRecord(this.label, vInfo.Version, vInfo.Comment)
if err != nil {
this.logError(err)
return err
}
return nil
}
//获取当前版本信息
func (this VersionMagV2) Version() (string, error) {
var err error
querySql := fmt.Sprintf("SELECT version FROM %s WHERE label=? ORDER BY id DESC LIMIT 1", VERSION_MAG_TABLE)
row := this.db.QueryRow(querySql, this.label)
var version string
err = row.Scan(&version)
switch err {
case nil:
case sql.ErrNoRows:
default:
this.logError(err)
return "", err
}
if version == "" {
version = "0.0.0"
}
return version, nil
}
func (this *VersionMagV2) addVersionRecord(label, version, sqlFile string) error {
//保存version信息
insertSql := fmt.Sprintf("INSERT INTO %s SET `label`=?,`version`=?,`upgrade_file`=?", VERSION_MAG_TABLE)
stmt, err := this.db.Prepare(insertSql)
if err != nil {
this.logError(err)
return err
}
_, err = stmt.Exec(label, version, sqlFile)
if err != nil {
this.logError(err)
return err
}
return nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。