mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-08 10:30:24 +08:00
修复unique key无法升级的问题
This commit is contained in:
@@ -164,7 +164,10 @@ func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult, showLog bool) (
|
||||
}
|
||||
_, err = db.Exec("ALTER TABLE " + newTable.Name + " ADD " + newIndex.Definition)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
err = this.tryCreateIndex(err, db, newTable.Name, newIndex.Definition)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
} else if oldIndex.Definition != newIndex.Definition {
|
||||
ops = append(ops, "* index "+newTable.Name+" "+newIndex.Name)
|
||||
@@ -177,7 +180,10 @@ func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult, showLog bool) (
|
||||
}
|
||||
_, err = db.Exec("ALTER TABLE " + newTable.Name + " ADD " + newIndex.Definition)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
err = this.tryCreateIndex(err, db, newTable.Name, newIndex.Definition)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -291,3 +297,34 @@ func (this *SQLDump) findRecordsTable(tableName string) *SQLRecordsTable {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 创建索引
|
||||
func (this *SQLDump) tryCreateIndex(err error, db *dbs.DB, tableName string, indexDefinition string) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 处理Duplicate entry
|
||||
if strings.Index(err.Error(), "Error 1062: Duplicate entry") >= 0 && (strings.HasSuffix(tableName, "Stats") || strings.HasSuffix(tableName, "Values")) {
|
||||
var tries = 5 // 尝试次数
|
||||
for i := 0; i < tries; i++ {
|
||||
_, err = db.Exec("TRUNCATE TABLE " + tableName)
|
||||
if err != nil {
|
||||
if i == tries-1 {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
_, err = db.Exec("ALTER TABLE " + tableName + " ADD " + indexDefinition)
|
||||
if err != nil {
|
||||
if i == tries-1 {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ func TestSQLExecutor_Run(t *testing.T) {
|
||||
Prefix: "edge",
|
||||
Dsn: "root:123456@tcp(127.0.0.1:3306)/db_edge_new?charset=utf8mb4&multiStatements=true",
|
||||
})
|
||||
err := executor.Run()
|
||||
err := executor.Run(false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
@@ -39,6 +41,9 @@ var upgradeFuncs = []*upgradeVersion{
|
||||
{
|
||||
"0.2.5", upgradeV0_2_5,
|
||||
},
|
||||
{
|
||||
"0.2.9", upgradeV0_2_9,
|
||||
},
|
||||
}
|
||||
|
||||
// UpgradeSQLData 升级SQL数据
|
||||
@@ -249,3 +254,30 @@ func upgradeV0_2_5(db *dbs.DB) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// v0.2.9
|
||||
func upgradeV0_2_9(db *dbs.DB) error {
|
||||
// 访问日志
|
||||
{
|
||||
one, err := db.FindOne("SELECT id FROM edgeSysSettings WHERE code=? LIMIT 1", systemconfigs.SettingCodeNSAccessLogSetting)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(one) == 0 {
|
||||
ref := &dnsconfigs.NSAccessLogRef{
|
||||
IsPrior: false,
|
||||
IsOn: true,
|
||||
LogMissingDomains: false,
|
||||
}
|
||||
refJSON, err := json.Marshal(ref)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = db.Exec("INSERT edgeSysSettings (code, value) VALUES (?, ?)", systemconfigs.SettingCodeNSAccessLogSetting, refJSON)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user