From 0cb08838465c8b33f441b9b1c1701bcbcfa2e38b Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Sat, 12 Sep 2026 10:09:59 +0200 Subject: [PATCH] feat(server): MIGRATE_ON_START; image CMD instead of ENTRYPOINT MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit orca's `cmd` sets the container CMD, so with an ENTRYPOINT the stage service ran `/tenant-registry /bin/sh -c …` (no migrations) and the erpmock service ran the registry. The image now has CMD only and the server applies embedded migrations itself when MIGRATE_ON_START=true. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB --- Dockerfile | 4 +++- README.md | 1 + cmd/server/main.go | 7 +++++++ migrations/up.go | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 migrations/up.go diff --git a/Dockerfile b/Dockerfile index 4faac04..207a936 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,4 +20,6 @@ COPY --from=build /out/migrate /migrate COPY --from=build /out/erpmock /erpmock USER nonroot:nonroot EXPOSE 8090 -ENTRYPOINT ["/tenant-registry"] +# CMD (not ENTRYPOINT) so orca's `cmd` can select /erpmock or /migrate in the +# same image. The server applies migrations itself with MIGRATE_ON_START=true. +CMD ["/tenant-registry"] diff --git a/README.md b/README.md index 3033692..325aea4 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,7 @@ Env vars (override at the shell): | `ADDR` | `:8090` | listen address (avoids Keycloak's :8080) | | `KEYCLOAK_ISSUER` | `http://localhost:8080/realms/breakpilot-dev` | OIDC issuer URL (the JWT signer) | | `DATABASE_URL` | empty (in-memory store fallback) | Postgres DSN; service uses Memory when empty | +| `MIGRATE_ON_START` | unset | `true` ⇒ apply pending migrations before serving (orca has no shell to chain `/migrate`) | | `KEYCLOAK_ADMIN_URL` | empty (Mock adapter used in dev) | KC base URL for the Admin API | | `KEYCLOAK_REALM` | `breakpilot-dev` | Realm name for Admin API calls | | `KEYCLOAK_CLIENT_ID` | empty | Service-account client id (Admin) | diff --git a/cmd/server/main.go b/cmd/server/main.go index 0f92b0b..dafe604 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -18,6 +18,7 @@ import ( "git.breakpilot.com/platform/tenant-registry/internal/product" "git.breakpilot.com/platform/tenant-registry/internal/server" "git.breakpilot.com/platform/tenant-registry/internal/store" + "git.breakpilot.com/platform/tenant-registry/migrations" ) func main() { @@ -38,6 +39,12 @@ func main() { slog.Warn("DATABASE_URL not set — running with in-memory store (dev only)") s = store.NewMemory() } else { + if os.Getenv("MIGRATE_ON_START") == "true" { + if err := migrations.Up(bootCtx, cfg.DatabaseURL); err != nil { + slog.Error("MIGRATE_ON_START failed", "err", err) + os.Exit(1) + } + } pg, err := store.NewPostgres(bootCtx, cfg.DatabaseURL) if err != nil { slog.Error("postgres connect failed", "err", err) diff --git a/migrations/up.go b/migrations/up.go new file mode 100644 index 0000000..7dc7427 --- /dev/null +++ b/migrations/up.go @@ -0,0 +1,37 @@ +package migrations + +import ( + "context" + "errors" + "fmt" + "log/slog" + + "github.com/golang-migrate/migrate/v4" + _ "github.com/golang-migrate/migrate/v4/database/postgres" // postgres driver + "github.com/golang-migrate/migrate/v4/source/iofs" +) + +// Up applies every pending migration against dbURL. Used by the server at +// start-up when MIGRATE_ON_START=true (the orca image has no shell to chain +// /migrate && /tenant-registry) and by cmd/migrate. +func Up(_ context.Context, dbURL string) error { + src, err := iofs.New(FS, ".") + if err != nil { + return fmt.Errorf("load embedded migrations: %w", err) + } + m, err := migrate.NewWithSourceInstance("iofs", src, dbURL) + if err != nil { + return fmt.Errorf("open migrate: %w", err) + } + defer func() { + if srcErr, dbErr := m.Close(); srcErr != nil || dbErr != nil { + slog.Warn("migrate close", "src_err", srcErr, "db_err", dbErr) + } + }() + if err := m.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) { + return fmt.Errorf("up: %w", err) + } + v, dirty, _ := m.Version() + slog.Info("schema migrated", "version", v, "dirty", dirty) + return nil +}