mirror of
https://github.com/go-admin-team/go-admin.git
synced 2026-10-09 07:31:52 +00:00
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.
32 lines
908 B
Go
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
|
|
}
|