mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 07:50:25 +08:00 
			
		
		
		
	导出SQL结构时使用embed取代生成sql.go
This commit is contained in:
		@@ -4,12 +4,11 @@ import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/setup"
 | 
			
		||||
	"github.com/iwind/TeaGo/Tea"
 | 
			
		||||
	_ "github.com/iwind/TeaGo/bootstrap"
 | 
			
		||||
	"github.com/iwind/TeaGo/dbs"
 | 
			
		||||
	"go/format"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"strconv"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
@@ -23,11 +22,6 @@ func main() {
 | 
			
		||||
		fmt.Println("[ERROR]" + err.Error())
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	resultsJSON, err := json.Marshal(results)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("[ERROR]" + err.Error())
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	prettyResultsJSON, err := json.MarshalIndent(results, "", "  ")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@@ -35,52 +29,8 @@ func main() {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 写入到 sql.go 中
 | 
			
		||||
	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 {
 | 
			
		||||
		fmt.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 {
 | 
			
		||||
		fmt.Println("[ERROR]format code failed: " + err.Error())
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = os.WriteFile(sqlFile, dst, 0666)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("[ERROR]write file failed: " + err.Error())
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 写入到 sql.json 中
 | 
			
		||||
	var dir = filepath.Dir(Tea.Root)
 | 
			
		||||
	err = os.WriteFile(dir+"/internal/setup/sql.json", prettyResultsJSON, 0666)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("[ERROR]" + err.Error())
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user