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
|
|
|
|
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
|
2022-07-20 09:20:49 +00:00
|
|
|
Name string `json:"name"`
|
2024-01-05 06:29:57 +00:00
|
|
|
Error string `json:"error,omitempty"`
|
2022-07-20 09:20:49 +00:00
|
|
|
LastRun interface{} `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,
|
|
|
|
eventstore.NewAggregate(ctx, aggregateID, aggregateType, "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"})
|
2022-07-20 09:20:49 +00:00
|
|
|
typ := doneType
|
|
|
|
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,
|
|
|
|
eventstore.NewAggregate(ctx, aggregateID, aggregateType, "v1"),
|
|
|
|
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
|
|
|
|
|
|
|
func RegisterMappers(es *eventstore.Eventstore) {
|
2023-04-28 11:55:35 +00:00
|
|
|
es.RegisterFilterEventMapper(aggregateType, StartedType, SetupMapper)
|
2023-01-16 11:30:03 +00:00
|
|
|
es.RegisterFilterEventMapper(aggregateType, doneType, SetupMapper)
|
|
|
|
es.RegisterFilterEventMapper(aggregateType, failedType, SetupMapper)
|
|
|
|
es.RegisterFilterEventMapper(aggregateType, repeatableDoneType, SetupMapper)
|
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
|
|
|
|
}
|