Files
2026-08-16 18:36:56 +02:00

92 lines
2.4 KiB
Go

package processing
import (
"log"
"os"
"path/filepath"
"strings"
)
// ResolveMySQLDumpPath picks an explicit path, else the first readable candidate
// under common local locations documented in scripts/seed/README.txt.
// Used by seed-a1 CLI and admin Sync A1 (API process filesystem).
func ResolveMySQLDumpPath(explicit string) string {
if p := strings.TrimSpace(explicit); p != "" {
if st, err := os.Stat(p); err == nil && !st.IsDir() {
return p
}
log.Printf("warning: mysql dump not found at %q — trying auto-detect", p)
}
for _, c := range MySQLDumpCandidates() {
if strings.TrimSpace(explicit) != "" && filepath.Clean(c) == filepath.Clean(strings.TrimSpace(explicit)) {
continue
}
if st, err := os.Stat(c); err == nil && !st.IsDir() {
return c
}
}
return ""
}
// MySQLDumpCandidates lists paths seed-a1 / Sync A1 try when SEED_A1_MYSQL_DUMP
// or -mysql-dump is unset. First readable file wins via ResolveMySQLDumpPath.
//
// ASSUMPTION: On servers (e.g. Git-Syncer) the dump must live on the API host
// filesystem — prefer scripts/seed/descrybe_new.sql under the deploy root, or set
// SEED_A1_MYSQL_DUMP in the API/worker environment.
func MySQLDumpCandidates() []string {
var out []string
if v := strings.TrimSpace(os.Getenv("SEED_A1_MYSQL_DUMP")); v != "" {
out = append(out, v)
}
home, _ := os.UserHomeDir()
names := []string{
"descrybe_new (1).sql",
"descrybe_new.sql",
"descrybe_new(1).sql",
}
if home != "" {
for _, n := range names {
out = append(out, filepath.Join(home, "Downloads", n))
out = append(out, filepath.Join(home, "downloads", n))
}
}
// Repo-relative guesses (cwd may be apps/api or repo root).
for _, n := range names {
out = append(out,
n,
filepath.Join("..", "..", n),
filepath.Join("scripts", "seed", n),
filepath.Join("..", "..", "scripts", "seed", n),
)
}
if root, ok := findMonorepoRootFromCwd(); ok {
for _, n := range names {
out = append(out, filepath.Join(root, "scripts", "seed", n))
}
}
return out
}
func findMonorepoRootFromCwd() (string, bool) {
cwd, err := os.Getwd()
if err != nil {
return "", false
}
dir := cwd
for {
api := filepath.Join(dir, "apps", "api")
web := filepath.Join(dir, "apps", "web")
if st, err := os.Stat(api); err == nil && st.IsDir() {
if st, err := os.Stat(web); err == nil && st.IsDir() {
return dir, true
}
}
parent := filepath.Dir(dir)
if parent == dir {
return "", false
}
dir = parent
}
}