mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-06 10:00:24 +08:00
修改SQL对比算法
This commit is contained in:
77
cmd/sql-dump/main.go
Normal file
77
cmd/sql-dump/main.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/setup"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"go/format"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db, err := dbs.Default()
|
||||
if err != nil {
|
||||
logs.Println("[ERROR]" + err.Error())
|
||||
return
|
||||
}
|
||||
results, err := setup.NewSQLDump().Dump(db)
|
||||
if err != nil {
|
||||
logs.Println("[ERROR]" + err.Error())
|
||||
return
|
||||
}
|
||||
resultsJSON, err := json.Marshal(results)
|
||||
if err != nil {
|
||||
logs.Println("[ERROR]" + err.Error())
|
||||
return
|
||||
}
|
||||
dir, _ := os.Getwd()
|
||||
var sqlFile string
|
||||
for i := 0; i < 5; i++ {
|
||||
lookupFile := dir + "/internal/setup/sql.go"
|
||||
_, err = os.Stat(lookupFile)
|
||||
if err != nil {
|
||||
dir = filepath.Dir(dir)
|
||||
continue
|
||||
}
|
||||
sqlFile = lookupFile
|
||||
}
|
||||
|
||||
if len(sqlFile) == 0 {
|
||||
logs.Println("[ERROR]can not find sql.go")
|
||||
return
|
||||
}
|
||||
content := []byte(`package setup
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
)
|
||||
|
||||
// 最新的SQL语句
|
||||
// 由sql-dump/main.go自动生成
|
||||
|
||||
func init() {
|
||||
err := json.Unmarshal([]byte(` + strconv.Quote(string(resultsJSON)) + `), LatestSQLResult)
|
||||
if err != nil {
|
||||
logs.Println("[ERROR]load sql failed: " + err.Error())
|
||||
}
|
||||
}
|
||||
`)
|
||||
dst, err := format.Source(content)
|
||||
if err != nil {
|
||||
logs.Println("[ERROR]format code failed: " + err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(sqlFile, dst, 0666)
|
||||
if err != nil {
|
||||
logs.Println("[ERROR]write file failed: " + err.Error())
|
||||
return
|
||||
}
|
||||
logs.Println("ok")
|
||||
}
|
||||
Reference in New Issue
Block a user