mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-03 15:00:27 +08:00
实现数据库版本升级命令
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/apps"
|
||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/nodes"
|
||||
@@ -21,7 +22,7 @@ func main() {
|
||||
app := apps.NewAppCmd()
|
||||
app.Version(teaconst.Version)
|
||||
app.Product(teaconst.ProductName)
|
||||
app.Usage(teaconst.ProcessName + " [start|stop|restart|setup]")
|
||||
app.Usage(teaconst.ProcessName + " [start|stop|restart|setup|upgrade]")
|
||||
app.On("setup", func() {
|
||||
setupCmd := setup.NewSetupFromCmd()
|
||||
err := setupCmd.Run()
|
||||
@@ -42,6 +43,19 @@ func main() {
|
||||
|
||||
_, _ = os.Stdout.Write(resultJSON)
|
||||
})
|
||||
app.On("upgrade", func() {
|
||||
executor, err := setup.NewSQLExecutorFromCmd()
|
||||
if err != nil {
|
||||
fmt.Println("ERROR: " + err.Error())
|
||||
return
|
||||
}
|
||||
err = executor.Run()
|
||||
if err != nil {
|
||||
fmt.Println("ERROR: " + err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Println("finished!")
|
||||
})
|
||||
app.Run(func() {
|
||||
nodes.NewAPINode().Start()
|
||||
})
|
||||
|
||||
@@ -5,11 +5,14 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/setup/sqls"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/go-yaml/yaml"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||
"io/ioutil"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -25,6 +28,20 @@ func NewSQLExecutor(dbConfig *dbs.DBConfig) *SQLExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
func NewSQLExecutorFromCmd() (*SQLExecutor, error) {
|
||||
// 执行SQL
|
||||
config := &dbs.Config{}
|
||||
configData, err := ioutil.ReadFile(Tea.ConfigFile("db.yaml"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = yaml.Unmarshal(configData, config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewSQLExecutor(config.DBs[Tea.Env]), nil
|
||||
}
|
||||
|
||||
func (this *SQLExecutor) Run() error {
|
||||
db, err := dbs.NewInstanceFromConfig(this.dbConfig)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user