mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-06 01:50:25 +08:00
实现数据库版本升级命令
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/apps"
|
"github.com/TeaOSLab/EdgeAPI/internal/apps"
|
||||||
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/nodes"
|
"github.com/TeaOSLab/EdgeAPI/internal/nodes"
|
||||||
@@ -21,7 +22,7 @@ func main() {
|
|||||||
app := apps.NewAppCmd()
|
app := apps.NewAppCmd()
|
||||||
app.Version(teaconst.Version)
|
app.Version(teaconst.Version)
|
||||||
app.Product(teaconst.ProductName)
|
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() {
|
app.On("setup", func() {
|
||||||
setupCmd := setup.NewSetupFromCmd()
|
setupCmd := setup.NewSetupFromCmd()
|
||||||
err := setupCmd.Run()
|
err := setupCmd.Run()
|
||||||
@@ -42,6 +43,19 @@ func main() {
|
|||||||
|
|
||||||
_, _ = os.Stdout.Write(resultJSON)
|
_, _ = 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() {
|
app.Run(func() {
|
||||||
nodes.NewAPINode().Start()
|
nodes.NewAPINode().Start()
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -5,11 +5,14 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/setup/sqls"
|
"github.com/TeaOSLab/EdgeAPI/internal/setup/sqls"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "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/dbs"
|
||||||
"github.com/iwind/TeaGo/lists"
|
"github.com/iwind/TeaGo/lists"
|
||||||
"github.com/iwind/TeaGo/rands"
|
"github.com/iwind/TeaGo/rands"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||||
|
"io/ioutil"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"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 {
|
func (this *SQLExecutor) Run() error {
|
||||||
db, err := dbs.NewInstanceFromConfig(this.dbConfig)
|
db, err := dbs.NewInstanceFromConfig(this.dbConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user