From 18842091a454fb676b061f27093b529dcb9437d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=97=D0=B0=D0=B8=D0=B4=20=D0=9E=D0=BC=D0=B0=D1=80=20?= =?UTF-8?q?=D0=9C=D0=B5=D0=B4=D1=85=D0=B0=D1=82=20=7C=20Zaid=20Omar=20Medh?= =?UTF-8?q?at?= Date: Tue, 16 Dec 2025 18:31:54 +0500 Subject: [PATCH] fix: add migrations to docker --- internal/pkg/infrastructure/db.go | 40 +++++++++++++++++++------------ services/api/Dockerfile | 3 +++ 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/internal/pkg/infrastructure/db.go b/internal/pkg/infrastructure/db.go index 54e9bbf..1f7d415 100644 --- a/internal/pkg/infrastructure/db.go +++ b/internal/pkg/infrastructure/db.go @@ -152,22 +152,32 @@ func GetMigrationVersion(db *sql.DB) (uint, bool, error) { } func getMigrationsPath() (string, error) { - // Get the directory of the current source file + if envPath := os.Getenv("MIGRATIONS_PATH"); envPath != "" { + if _, err := os.Stat(envPath); err == nil { + return envPath, nil + } + } + + paths := []string{ + "./internal/migrations", + "../internal/migrations", + "../../internal/migrations", + } + + for _, p := range paths { + if _, err := os.Stat(p); err == nil { + return p, nil + } + } + _, filename, _, ok := runtime.Caller(0) - if !ok { - return "", fmt.Errorf("failed to get current file path") + if ok { + internalDir := filepath.Dir(filepath.Dir(filepath.Dir(filename))) + migrationsPath := filepath.Join(internalDir, "migrations") + if _, err := os.Stat(migrationsPath); err == nil { + return migrationsPath, nil + } } - // Get the internal directory (go up from internal/pkg/infrastructure/ to internal/) - // Current path: .../internal/pkg/infrastructure/db.go - // Need to go up: infrastructure -> pkg -> internal - internalDir := filepath.Dir(filepath.Dir(filepath.Dir(filename))) - migrationsPath := filepath.Join(internalDir, "migrations") - - // Verify the migrations directory exists - if _, err := os.Stat(migrationsPath); err != nil { - return "", fmt.Errorf("migrations directory not found at %s: %w", migrationsPath, err) - } - - return migrationsPath, nil + return "", fmt.Errorf("migrations directory not found") } diff --git a/services/api/Dockerfile b/services/api/Dockerfile index 56c77da..4a42f20 100644 --- a/services/api/Dockerfile +++ b/services/api/Dockerfile @@ -27,6 +27,9 @@ WORKDIR /app # Copy the binary from builder COPY --from=builder /build/services/api/api . +# Copy migrations +COPY --from=builder /build/internal/migrations ./internal/migrations + EXPOSE 8080 # Run the API