diff --git a/cmd/edge-api/main.go b/cmd/edge-api/main.go index dc044130..8d0d71b5 100644 --- a/cmd/edge-api/main.go +++ b/cmd/edge-api/main.go @@ -44,6 +44,7 @@ func main() { _, _ = os.Stdout.Write(resultJSON) }) app.On("upgrade", func() { + fmt.Println("start ...") executor, err := setup.NewSQLExecutorFromCmd() if err != nil { fmt.Println("ERROR: " + err.Error()) diff --git a/internal/setup/sql_dump.go b/internal/setup/sql_dump.go index 9b45fbe8..27218be0 100644 --- a/internal/setup/sql_dump.go +++ b/internal/setup/sql_dump.go @@ -1,6 +1,7 @@ package setup import ( + "fmt" "github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/types" @@ -108,7 +109,7 @@ func (this *SQLDump) Dump(db *dbs.DB) (result *SQLDumpResult, err error) { } // 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) if err != nil { return nil, err @@ -119,6 +120,9 @@ func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult) (ops []string, oldTable := currentResult.FindTable(newTable.Name) if oldTable == nil { ops = append(ops, "+ table "+newTable.Name) + if showLog { + fmt.Println("+ table " + newTable.Name) + } _, err = db.Exec(newTable.Definition) if err != nil { return nil, err @@ -130,12 +134,18 @@ func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult) (ops []string, oldField := oldTable.FindField(newField.Name) if oldField == nil { 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) if err != nil { return nil, err } } else if !newField.EqualDefinition(oldField.Definition) { 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) if err != nil { return nil, err @@ -149,12 +159,18 @@ func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult) (ops []string, oldIndex := oldTable.FindIndex(newIndex.Name) if oldIndex == nil { 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) if err != nil { return nil, err } } else if oldIndex.Definition != newIndex.Definition { 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) if err != nil { return nil, err @@ -171,6 +187,9 @@ func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult) (ops []string, newIndex := newTable.FindIndex(oldIndex.Name) if newIndex == nil { 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) if err != nil { return nil, err @@ -184,6 +203,9 @@ func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult) (ops []string, newField := newTable.FindField(oldField.Name) if newField == nil { 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 + "`") if err != nil { return nil, err @@ -209,6 +231,9 @@ func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult) (ops []string, } if one == nil { ops = append(ops, "+ record "+newTable.Name+" "+strings.Join(valueStrings, ", ")) + if showLog { + fmt.Println("+ record " + newTable.Name + " " + strings.Join(valueStrings, ", ")) + } params := []string{} args := []string{} values := []interface{}{} @@ -224,6 +249,9 @@ func (this *SQLDump) Apply(db *dbs.DB, newResult *SQLDumpResult) (ops []string, } } else if !record.ValuesEquals(one) { ops = append(ops, "* record "+newTable.Name+" "+strings.Join(valueStrings, ", ")) + if showLog { + fmt.Println("* record " + newTable.Name + " " + strings.Join(valueStrings, ", ")) + } args := []string{} values := []interface{}{} for k, v := range record.Values { diff --git a/internal/setup/sql_dump_test.go b/internal/setup/sql_dump_test.go index 76f480e3..cf86aaa7 100644 --- a/internal/setup/sql_dump_test.go +++ b/internal/setup/sql_dump_test.go @@ -64,7 +64,7 @@ func TestSQLDump_Apply(t *testing.T) { if err != nil { t.Fatal(err) } - ops, err := dump.Apply(db2, result) + ops, err := dump.Apply(db2, result, false) if err != nil { t.Fatal(err) } diff --git a/internal/setup/sql_executor.go b/internal/setup/sql_executor.go index be3c1e4c..73987a05 100644 --- a/internal/setup/sql_executor.go +++ b/internal/setup/sql_executor.go @@ -52,7 +52,7 @@ func (this *SQLExecutor) Run() error { } sqlDump := NewSQLDump() - _, err = sqlDump.Apply(db, LatestSQLResult) + _, err = sqlDump.Apply(db, LatestSQLResult, true) if err != nil { return err }