使用upgrade命令升级数据库时增加日志显示

This commit is contained in:
GoEdgeLab
2021-08-02 15:23:02 +08:00
parent ee825f5d89
commit fad1ccd28f
4 changed files with 32 additions and 3 deletions

View File

@@ -44,6 +44,7 @@ func main() {
_, _ = os.Stdout.Write(resultJSON) _, _ = os.Stdout.Write(resultJSON)
}) })
app.On("upgrade", func() { app.On("upgrade", func() {
fmt.Println("start ...")
executor, err := setup.NewSQLExecutorFromCmd() executor, err := setup.NewSQLExecutorFromCmd()
if err != nil { if err != nil {
fmt.Println("ERROR: " + err.Error()) fmt.Println("ERROR: " + err.Error())

View File

@@ -1,6 +1,7 @@
package setup package setup
import ( import (
"fmt"
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
@@ -108,7 +109,7 @@ func (this *SQLDump) Dump(db *dbs.DB) (result *SQLDumpResult, err error) {
} }
// Apply 应用数据 // Apply 应用数据
func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult) (ops []string, err error) { func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult, showLog bool) (ops []string, err error) {
currentResult, err := this.Dump(db) currentResult, err := this.Dump(db)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -119,6 +120,9 @@ func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult) (ops []string,
oldTable := currentResult.FindTable(newTable.Name) oldTable := currentResult.FindTable(newTable.Name)
if oldTable == nil { if oldTable == nil {
ops = append(ops, "+ table "+newTable.Name) ops = append(ops, "+ table "+newTable.Name)
if showLog {
fmt.Println("+ table " + newTable.Name)
}
_, err = db.Exec(newTable.Definition) _, err = db.Exec(newTable.Definition)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -130,12 +134,18 @@ func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult) (ops []string,
oldField := oldTable.FindField(newField.Name) oldField := oldTable.FindField(newField.Name)
if oldField == nil { if oldField == nil {
ops = append(ops, "+ "+newTable.Name+" "+newField.Name) ops = append(ops, "+ "+newTable.Name+" "+newField.Name)
if showLog {
fmt.Println("+ " + newTable.Name + " " + newField.Name)
}
_, err = db.Exec("ALTER TABLE " + newTable.Name + " ADD `" + newField.Name + "` " + newField.Definition) _, err = db.Exec("ALTER TABLE " + newTable.Name + " ADD `" + newField.Name + "` " + newField.Definition)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else if !newField.EqualDefinition(oldField.Definition) { } else if !newField.EqualDefinition(oldField.Definition) {
ops = append(ops, "* "+newTable.Name+" "+newField.Name) ops = append(ops, "* "+newTable.Name+" "+newField.Name)
if showLog {
fmt.Println("* " + newTable.Name + " " + newField.Name)
}
_, err = db.Exec("ALTER TABLE " + newTable.Name + " MODIFY `" + newField.Name + "` " + newField.Definition) _, err = db.Exec("ALTER TABLE " + newTable.Name + " MODIFY `" + newField.Name + "` " + newField.Definition)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -149,12 +159,18 @@ func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult) (ops []string,
oldIndex := oldTable.FindIndex(newIndex.Name) oldIndex := oldTable.FindIndex(newIndex.Name)
if oldIndex == nil { if oldIndex == nil {
ops = append(ops, "+ index "+newTable.Name+" "+newIndex.Name) ops = append(ops, "+ index "+newTable.Name+" "+newIndex.Name)
if showLog {
fmt.Println("+ index " + newTable.Name + " " + newIndex.Name)
}
_, err = db.Exec("ALTER TABLE " + newTable.Name + " ADD " + newIndex.Definition) _, err = db.Exec("ALTER TABLE " + newTable.Name + " ADD " + newIndex.Definition)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else if oldIndex.Definition != newIndex.Definition { } else if oldIndex.Definition != newIndex.Definition {
ops = append(ops, "* index "+newTable.Name+" "+newIndex.Name) ops = append(ops, "* index "+newTable.Name+" "+newIndex.Name)
if showLog {
fmt.Println("* index " + newTable.Name + " " + newIndex.Name)
}
_, err = db.Exec("ALTER TABLE " + newTable.Name + " DROP KEY " + newIndex.Name) _, err = db.Exec("ALTER TABLE " + newTable.Name + " DROP KEY " + newIndex.Name)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -171,6 +187,9 @@ func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult) (ops []string,
newIndex := newTable.FindIndex(oldIndex.Name) newIndex := newTable.FindIndex(oldIndex.Name)
if newIndex == nil { if newIndex == nil {
ops = append(ops, "- index "+oldTable.Name+" "+oldIndex.Name) ops = append(ops, "- index "+oldTable.Name+" "+oldIndex.Name)
if showLog {
fmt.Println("- index " + oldTable.Name + " " + oldIndex.Name)
}
_, err = db.Exec("ALTER TABLE " + oldTable.Name + " DROP KEY " + oldIndex.Name) _, err = db.Exec("ALTER TABLE " + oldTable.Name + " DROP KEY " + oldIndex.Name)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -184,6 +203,9 @@ func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult) (ops []string,
newField := newTable.FindField(oldField.Name) newField := newTable.FindField(oldField.Name)
if newField == nil { if newField == nil {
ops = append(ops, "- field "+oldTable.Name+" "+oldField.Name) ops = append(ops, "- field "+oldTable.Name+" "+oldField.Name)
if showLog {
fmt.Println("- field " + oldTable.Name + " " + oldField.Name)
}
_, err = db.Exec("ALTER TABLE " + oldTable.Name + " DROP COLUMN `" + oldField.Name + "`") _, err = db.Exec("ALTER TABLE " + oldTable.Name + " DROP COLUMN `" + oldField.Name + "`")
if err != nil { if err != nil {
return nil, err return nil, err
@@ -209,6 +231,9 @@ func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult) (ops []string,
} }
if one == nil { if one == nil {
ops = append(ops, "+ record "+newTable.Name+" "+strings.Join(valueStrings, ", ")) ops = append(ops, "+ record "+newTable.Name+" "+strings.Join(valueStrings, ", "))
if showLog {
fmt.Println("+ record " + newTable.Name + " " + strings.Join(valueStrings, ", "))
}
params := []string{} params := []string{}
args := []string{} args := []string{}
values := []interface{}{} values := []interface{}{}
@@ -224,6 +249,9 @@ func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult) (ops []string,
} }
} else if !record.ValuesEquals(one) { } else if !record.ValuesEquals(one) {
ops = append(ops, "* record "+newTable.Name+" "+strings.Join(valueStrings, ", ")) ops = append(ops, "* record "+newTable.Name+" "+strings.Join(valueStrings, ", "))
if showLog {
fmt.Println("* record " + newTable.Name + " " + strings.Join(valueStrings, ", "))
}
args := []string{} args := []string{}
values := []interface{}{} values := []interface{}{}
for k, v := range record.Values { for k, v := range record.Values {

View File

@@ -64,7 +64,7 @@ func TestSQLDump_Apply(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
ops, err := dump.Apply(db2, result) ops, err := dump.Apply(db2, result, false)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@@ -52,7 +52,7 @@ func (this *SQLExecutor) Run() error {
} }
sqlDump := NewSQLDump() sqlDump := NewSQLDump()
_, err = sqlDump.Apply(db, LatestSQLResult) _, err = sqlDump.Apply(db, LatestSQLResult, true)
if err != nil { if err != nil {
return err return err
} }