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:
@@ -8,7 +8,6 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -18,15 +17,37 @@ import (
|
||||
var SchemaFile string
|
||||
|
||||
func init() {
|
||||
// Get the path to schema.sql relative to this file
|
||||
_, currentFile, _, _ := runtime.Caller(0)
|
||||
baseDir := filepath.Dir(currentFile)
|
||||
schemaPath := filepath.Join(baseDir, "..", "..", "database", "schema", "schema.sql")
|
||||
|
||||
content, err := os.ReadFile(schemaPath)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read schema.sql: %v", err)
|
||||
// Try multiple locations for schema.sql
|
||||
// 1. Relative to working directory (for local dev)
|
||||
// 2. Relative to binary (for containerized deployment)
|
||||
locations := []string{
|
||||
"database/schema/schema.sql",
|
||||
"/app/database/schema/schema.sql",
|
||||
"../database/schema/schema.sql",
|
||||
"../../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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user