zitadel/internal/migration/command.go
Silvan 17953e9040
fix(setup): init projections (#7194)
Even though this is a feature it's released as fix so that we can back port to earlier revisions.

As reported by multiple users startup of ZITADEL after leaded to downtime and worst case rollbacks to the previously deployed version.

The problem starts rising when there are too many events to process after the start of ZITADEL. The root cause are changes on projections (database tables) which must be recomputed. This PR solves this problem by adding a new step to the setup phase which prefills the projections. The step can be enabled by adding the `--init-projections`-flag to `setup`, `start-from-init` and `start-from-setup`. Setting this flag results in potentially longer duration of the setup phase but reduces the risk of the problems mentioned in the paragraph above.
2024-01-25 17:28:20 +01:00

100 lines
2.9 KiB
Go

package migration
import (
"context"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/api/service"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/zerrors"
)
func init() {
eventstore.RegisterFilterEventMapper(SystemAggregate, StartedType, SetupMapper)
eventstore.RegisterFilterEventMapper(SystemAggregate, DoneType, SetupMapper)
eventstore.RegisterFilterEventMapper(SystemAggregate, failedType, SetupMapper)
eventstore.RegisterFilterEventMapper(SystemAggregate, repeatableDoneType, SetupMapper)
}
// SetupStep is the command pushed on the eventstore
type SetupStep struct {
eventstore.BaseEvent `json:"-"`
migration Migration
Name string `json:"name"`
Error any `json:"error,omitempty"`
LastRun any `json:"lastRun,omitempty"`
}
func setupStartedCmd(ctx context.Context, migration Migration) eventstore.Command {
ctx = authz.SetCtxData(service.WithService(ctx, "system"), authz.CtxData{UserID: "system", OrgID: "SYSTEM", ResourceOwner: "SYSTEM"})
return &SetupStep{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
eventstore.NewAggregate(ctx, SystemAggregateID, SystemAggregate, "v1"),
StartedType),
migration: migration,
Name: migration.String(),
}
}
func setupDoneCmd(ctx context.Context, migration Migration, err error) eventstore.Command {
ctx = authz.SetCtxData(service.WithService(ctx, "system"), authz.CtxData{UserID: "system", OrgID: "SYSTEM", ResourceOwner: "SYSTEM"})
typ := DoneType
var lastRun interface{}
if repeatable, ok := migration.(RepeatableMigration); ok {
typ = repeatableDoneType
lastRun = repeatable
}
s := &SetupStep{
migration: migration,
Name: migration.String(),
LastRun: lastRun,
}
if err != nil {
typ = failedType
s.Error = err.Error()
}
s.BaseEvent = *eventstore.NewBaseEventForPush(
ctx,
eventstore.NewAggregate(ctx, SystemAggregateID, SystemAggregate, "v1"),
typ)
return s
}
func (s *SetupStep) Payload() interface{} {
return s
}
func (s *SetupStep) UniqueConstraints() []*eventstore.UniqueConstraint {
switch s.Type() {
case StartedType:
return []*eventstore.UniqueConstraint{
eventstore.NewAddGlobalUniqueConstraint("migration_started", s.migration.String(), "Errors.Step.Started.AlreadyExists"),
}
case failedType,
repeatableDoneType:
return []*eventstore.UniqueConstraint{
eventstore.NewRemoveGlobalUniqueConstraint("migration_started", s.migration.String()),
}
default:
return []*eventstore.UniqueConstraint{
eventstore.NewAddGlobalUniqueConstraint("migration_done", s.migration.String(), "Errors.Step.Done.AlreadyExists"),
}
}
}
func SetupMapper(event eventstore.Event) (eventstore.Event, error) {
step := &SetupStep{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := event.Unmarshal(step)
if err != nil {
return nil, zerrors.ThrowInternal(err, "IAM-hYp7M", "unable to unmarshal step")
}
return step, nil
}