feat(server): MIGRATE_ON_START; image CMD instead of ENTRYPOINT
ci / shared (pull_request) Successful in 13s
ci / test (pull_request) Successful in 22m5s
ci / image (pull_request) Skipped

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
This commit is contained in:
Sharang ParnerkarandClaude Fable 5.1 committed 2026-09-12 10:09:59 +02:00
1 parent fafec29cc4
commit 0cb0883846
4 files changed
+48 -1

No files matched your search

+3 -1
View File
@@ -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"]
+1
View File
@@ -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) |
+7
View File
@@ -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)
+37
View File
@@ -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
}