Files
go-admin/config/seed.go
T
zhangwenjian 3a96f37391 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.
2026-10-07 17:32:42 +08:00

32 lines
908 B
Go

package config
import (
"embed"
"errors"
"io/fs"
"os"
"path"
)
// The seed scripts ship inside the binary so a database can be created from
// wherever the binary runs. They used to be read from ./config/ only, which
// fails for anything not started from the repository root - a Docker image
// that carries just the binary and its settings file, for one.
//
//go:embed db.sql pg.sql db-begin-mysql.sql db-end-mysql.sql
var seedFS embed.FS
// ReadSeed returns the seed script at name (e.g. "config/db.sql").
// A file on disk wins, so a deployment that edits its own copy keeps doing
// so; the embedded copy is the fallback when there is none.
func ReadSeed(name string) ([]byte, error) {
b, err := os.ReadFile(name)
if err == nil || !errors.Is(err, fs.ErrNotExist) {
return b, err
}
if emb, embErr := seedFS.ReadFile(path.Base(name)); embErr == nil {
return emb, nil
}
return nil, err
}