fix: handle schema.sql file path in containerized environment

- Try multiple locations for schema.sql file
- Support both local dev and containerized deployment paths
- Add informative logging when schema is loaded
- Prevent runtime.Caller issues in containers

Locations checked:
- database/schema/schema.sql (working directory)
- /app/database/schema/schema.sql (container)
- ../database/schema/schema.sql (relative)
- ../../database/schema/schema.sql (relative)

This fixes the 'no such file or directory' error in production containers.
This commit is contained in:
2026-02-10 16:52:12 -05:00
parent a289353b0b
commit 1f65933653
+30 -9
View File
@@ -8,7 +8,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime"
"strings" "strings"
"time" "time"
@@ -18,15 +17,37 @@ import (
var SchemaFile string var SchemaFile string
func init() { func init() {
// Get the path to schema.sql relative to this file // Try multiple locations for schema.sql
_, currentFile, _, _ := runtime.Caller(0) // 1. Relative to working directory (for local dev)
baseDir := filepath.Dir(currentFile) // 2. Relative to binary (for containerized deployment)
schemaPath := filepath.Join(baseDir, "..", "..", "database", "schema", "schema.sql") locations := []string{
"database/schema/schema.sql",
content, err := os.ReadFile(schemaPath) "/app/database/schema/schema.sql",
if err != nil { "../database/schema/schema.sql",
log.Fatalf("Failed to read schema.sql: %v", err) "../../database/schema/schema.sql",
} }
var content []byte
var err error
for _, path := range locations {
content, err = os.ReadFile(path)
if err == nil {
log.Printf("INFO: Loaded schema from %s", path)
break
}
}
if content == nil {
// Last resort: try to find it relative to this file using runtime.Caller
// This will work in development but not in containers
absPath, _ := filepath.Abs("../../database/schema/schema.sql")
content, err = os.ReadFile(absPath)
if err != nil {
log.Fatalf("Failed to read schema.sql from any location. Tried: %v. Last error: %v", locations, err)
}
}
SchemaFile = string(content) SchemaFile = string(content)
} }