2022-03-23 08:02:39 +00:00
|
|
|
package migration
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/service"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-03-28 08:05:09 +00:00
|
|
|
)
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2024-01-25 16:28:20 +00:00
|
|
|
func init() {
|
|
|
|
eventstore.RegisterFilterEventMapper(SystemAggregate, StartedType, SetupMapper)
|
|
|
|
eventstore.RegisterFilterEventMapper(SystemAggregate, DoneType, SetupMapper)
|
|
|
|
eventstore.RegisterFilterEventMapper(SystemAggregate, failedType, SetupMapper)
|
|
|
|
eventstore.RegisterFilterEventMapper(SystemAggregate, repeatableDoneType, SetupMapper)
|
|
|
|
}
|
|
|
|
|
2023-01-16 11:30:03 +00:00
|
|
|
// SetupStep is the command pushed on the eventstore
|
2022-03-23 08:02:39 +00:00
|
|
|
type SetupStep struct {
|
2022-03-28 08:05:09 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
migration Migration
|
2024-01-05 09:01:48 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Error any `json:"error,omitempty"`
|
|
|
|
LastRun any `json:"lastRun,omitempty"`
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func setupStartedCmd(ctx context.Context, migration Migration) eventstore.Command {
|
|
|
|
ctx = authz.SetCtxData(service.WithService(ctx, "system"), authz.CtxData{UserID: "system", OrgID: "SYSTEM", ResourceOwner: "SYSTEM"})
|
2022-03-23 08:02:39 +00:00
|
|
|
return &SetupStep{
|
2022-03-28 08:05:09 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2024-01-25 16:28:20 +00:00
|
|
|
eventstore.NewAggregate(ctx, SystemAggregateID, SystemAggregate, "v1"),
|
2023-04-28 11:55:35 +00:00
|
|
|
StartedType),
|
2022-03-23 08:02:39 +00:00
|
|
|
migration: migration,
|
|
|
|
Name: migration.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 11:55:35 +00:00
|
|
|
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"})
|
2024-01-25 16:28:20 +00:00
|
|
|
typ := DoneType
|
2022-07-20 09:20:49 +00:00
|
|
|
var lastRun interface{}
|
|
|
|
if repeatable, ok := migration.(RepeatableMigration); ok {
|
|
|
|
typ = repeatableDoneType
|
|
|
|
lastRun = repeatable
|
|
|
|
}
|
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
s := &SetupStep{
|
|
|
|
migration: migration,
|
|
|
|
Name: migration.String(),
|
2022-07-20 09:20:49 +00:00
|
|
|
LastRun: lastRun,
|
2022-03-23 08:02:39 +00:00
|
|
|
}
|
2024-01-05 06:29:57 +00:00
|
|
|
if err != nil {
|
|
|
|
typ = failedType
|
|
|
|
s.Error = err.Error()
|
|
|
|
}
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
s.BaseEvent = *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2024-01-25 16:28:20 +00:00
|
|
|
eventstore.NewAggregate(ctx, SystemAggregateID, SystemAggregate, "v1"),
|
2022-03-28 08:05:09 +00:00
|
|
|
typ)
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
return s
|
2022-03-23 08:02:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (s *SetupStep) Payload() interface{} {
|
2022-03-23 08:02:39 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (s *SetupStep) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2022-03-28 08:05:09 +00:00
|
|
|
switch s.Type() {
|
2023-04-28 11:55:35 +00:00
|
|
|
case StartedType:
|
2023-10-19 10:19:10 +00:00
|
|
|
return []*eventstore.UniqueConstraint{
|
|
|
|
eventstore.NewAddGlobalUniqueConstraint("migration_started", s.migration.String(), "Errors.Step.Started.AlreadyExists"),
|
2022-03-23 08:02:39 +00:00
|
|
|
}
|
2022-07-20 09:20:49 +00:00
|
|
|
case failedType,
|
|
|
|
repeatableDoneType:
|
2023-10-19 10:19:10 +00:00
|
|
|
return []*eventstore.UniqueConstraint{
|
|
|
|
eventstore.NewRemoveGlobalUniqueConstraint("migration_started", s.migration.String()),
|
2022-03-23 08:02:39 +00:00
|
|
|
}
|
|
|
|
default:
|
2023-10-19 10:19:10 +00:00
|
|
|
return []*eventstore.UniqueConstraint{
|
|
|
|
eventstore.NewAddGlobalUniqueConstraint("migration_done", s.migration.String(), "Errors.Step.Done.AlreadyExists"),
|
2022-03-23 08:02:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func SetupMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-03-28 08:05:09 +00:00
|
|
|
step := &SetupStep{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(step)
|
2022-03-28 08:05:09 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "IAM-hYp7M", "unable to unmarshal step")
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return step, nil
|
|
|
|
}
|