From 1f659336531e29ef96e15dfe411ce314cae1be5a Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Tue, 10 Feb 2026 16:52:12 -0500 Subject: [PATCH] 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. --- internal/database/schema.go | 39 ++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/internal/database/schema.go b/internal/database/schema.go index c8d21e3..8a19e54 100644 --- a/internal/database/schema.go +++ b/internal/database/schema.go @@ -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) }