fix🐛: embed the seed SQL so migrate works outside the repository root

The seed scripts were read from ./config/ only, so migrating an empty database
failed anywhere that directory was absent, such as a container holding just
the binary and its settings. Embed them and prefer a file on disk when there
is one.
This commit is contained in:
zhangwenjian
2026-10-07 17:32:42 +08:00
parent ea679bf00b
commit 3a96f37391
4 changed files with 68 additions and 4 deletions
+2 -2
View File
@@ -3,7 +3,7 @@ package models
import (
"fmt"
"go-admin/common/global"
"io/ioutil"
"go-admin/config"
"log"
"strings"
@@ -61,7 +61,7 @@ func ExecSql(db *gorm.DB, filePath string) error {
}
func Ioutil(filePath string) (string, error) {
if contents, err := ioutil.ReadFile(filePath); err == nil {
if contents, err := config.ReadSeed(filePath); err == nil {
//因为contents是[]byte类型,直接转换成string类型后会多一行空格,需要使用strings.Replace替换换行符
result := strings.Replace(string(contents), "\n", "", 1)
fmt.Println("Use ioutil.ReadFile to read a file:", result)