Files
tenant-registry/migrations/up.go
T
Sharang ParnerkarandClaude Fable 5.1 0cb0883846
ci / shared (pull_request) Successful in 13s
ci / test (pull_request) Successful in 22m5s
ci / image (pull_request) Skipped
feat(server): MIGRATE_ON_START; image CMD instead of ENTRYPOINT
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
2026-09-12 10:09:59 +02:00

38 lines
1.1 KiB
Go

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
}