2022-03-23 09:02:39 +01:00
|
|
|
package migration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-08 16:30:55 +02:00
|
|
|
"errors"
|
2022-11-23 16:31:59 +01:00
|
|
|
"time"
|
2022-03-23 09:02:39 +01:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/logging"
|
2022-03-23 09:02:39 +01:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-03-23 09:02:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-04-28 13:55:35 +02:00
|
|
|
StartedType = eventstore.EventType("system.migration.started")
|
2024-01-25 17:28:20 +01:00
|
|
|
DoneType = eventstore.EventType("system.migration.done")
|
2022-07-20 11:20:49 +02:00
|
|
|
failedType = eventstore.EventType("system.migration.failed")
|
|
|
|
repeatableDoneType = eventstore.EventType("system.migration.repeatable.done")
|
2024-01-25 17:28:20 +01:00
|
|
|
SystemAggregate = eventstore.AggregateType("system")
|
|
|
|
SystemAggregateID = "SYSTEM"
|
2022-03-23 09:02:39 +01:00
|
|
|
)
|
|
|
|
|
2022-11-23 16:31:59 +01:00
|
|
|
var (
|
2023-12-08 16:30:55 +02:00
|
|
|
errMigrationAlreadyStarted = errors.New("already started")
|
2022-11-23 16:31:59 +01:00
|
|
|
)
|
|
|
|
|
2022-03-23 09:02:39 +01:00
|
|
|
type Migration interface {
|
|
|
|
String() string
|
2024-01-25 17:28:20 +01:00
|
|
|
Execute(ctx context.Context, startedEvent eventstore.Event) error
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
type errCheckerMigration interface {
|
|
|
|
Migration
|
|
|
|
ContinueOnErr(err error) bool
|
|
|
|
}
|
|
|
|
|
2022-07-20 11:20:49 +02:00
|
|
|
type RepeatableMigration interface {
|
|
|
|
Migration
|
2024-01-05 10:01:48 +01:00
|
|
|
Check(lastRun map[string]interface{}) bool
|
2022-07-20 11:20:49 +02:00
|
|
|
}
|
|
|
|
|
2022-03-23 09:02:39 +01:00
|
|
|
func Migrate(ctx context.Context, es *eventstore.Eventstore, migration Migration) (err error) {
|
2023-04-28 13:55:35 +02:00
|
|
|
logging.WithFields("name", migration.String()).Info("verify migration")
|
2022-07-20 11:20:49 +02:00
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
continueOnErr := func(err error) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
errChecker, ok := migration.(errCheckerMigration)
|
|
|
|
if ok {
|
|
|
|
continueOnErr = errChecker.ContinueOnErr
|
|
|
|
}
|
|
|
|
|
|
|
|
should, err := checkExec(ctx, es, migration)
|
|
|
|
if err != nil && !continueOnErr(err) {
|
2022-03-23 09:02:39 +01:00
|
|
|
return err
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
if !should {
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-23 09:02:39 +01:00
|
|
|
|
2024-01-25 17:28:20 +01:00
|
|
|
startedEvent, err := es.Push(ctx, setupStartedCmd(ctx, migration))
|
|
|
|
if err != nil && !continueOnErr(err) {
|
2022-03-23 09:02:39 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-28 13:55:35 +02:00
|
|
|
logging.WithFields("name", migration.String()).Info("starting migration")
|
2024-01-25 17:28:20 +01:00
|
|
|
err = migration.Execute(ctx, startedEvent[0])
|
2023-10-19 12:19:10 +02:00
|
|
|
logging.WithFields("name", migration.String()).OnError(err).Error("migration failed")
|
2022-03-23 09:02:39 +01:00
|
|
|
|
2023-04-28 13:55:35 +02:00
|
|
|
_, pushErr := es.Push(ctx, setupDoneCmd(ctx, migration, err))
|
2023-10-19 12:19:10 +02:00
|
|
|
logging.WithFields("name", migration.String()).OnError(pushErr).Error("migration finish failed")
|
2022-04-21 12:37:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return pushErr
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
|
|
|
|
2024-01-05 10:01:48 +01:00
|
|
|
func LastStuckStep(ctx context.Context, es *eventstore.Eventstore) (*SetupStep, error) {
|
|
|
|
var states StepStates
|
|
|
|
err := es.FilterToQueryReducer(ctx, &states)
|
2023-04-28 13:55:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-05 10:01:48 +01:00
|
|
|
step := states.lastByState(StepStarted)
|
|
|
|
if step == nil {
|
|
|
|
return nil, nil
|
2023-04-28 13:55:35 +02:00
|
|
|
}
|
2024-01-05 10:01:48 +01:00
|
|
|
|
|
|
|
return step.SetupStep, nil
|
2023-04-28 13:55:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Migration = (*cancelMigration)(nil)
|
|
|
|
|
|
|
|
type cancelMigration struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute implements Migration
|
2024-01-25 17:28:20 +01:00
|
|
|
func (*cancelMigration) Execute(context.Context, eventstore.Event) error {
|
2023-04-28 13:55:35 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements Migration
|
|
|
|
func (m *cancelMigration) String() string {
|
|
|
|
return m.name
|
|
|
|
}
|
|
|
|
|
2023-12-08 16:30:55 +02:00
|
|
|
var errCancelStep = zerrors.ThrowError(nil, "MIGRA-zo86K", "migration canceled manually")
|
2023-04-28 13:55:35 +02:00
|
|
|
|
|
|
|
func CancelStep(ctx context.Context, es *eventstore.Eventstore, step *SetupStep) error {
|
|
|
|
_, err := es.Push(ctx, setupDoneCmd(ctx, &cancelMigration{name: step.Name}, errCancelStep))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-23 16:31:59 +01:00
|
|
|
// checkExec ensures that only one setup step is done concurrently
|
|
|
|
// if a setup step is already started, it calls shouldExec after some time again
|
|
|
|
func checkExec(ctx context.Context, es *eventstore.Eventstore, migration Migration) (bool, error) {
|
|
|
|
timer := time.NewTimer(0)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2023-12-08 16:30:55 +02:00
|
|
|
return false, zerrors.ThrowInternal(nil, "MIGR-as3f7", "Errors.Internal")
|
2022-11-23 16:31:59 +01:00
|
|
|
case <-timer.C:
|
|
|
|
should, err := shouldExec(ctx, es, migration)
|
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
if !errors.Is(err, errMigrationAlreadyStarted) {
|
2022-11-23 16:31:59 +01:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
logging.WithFields("migration step", migration.String()).
|
|
|
|
Warn("migration already started, will check again in 5 seconds")
|
|
|
|
timer.Reset(5 * time.Second)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return should, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-23 09:02:39 +01:00
|
|
|
func shouldExec(ctx context.Context, es *eventstore.Eventstore, migration Migration) (should bool, err error) {
|
2024-01-05 10:01:48 +01:00
|
|
|
var states StepStates
|
|
|
|
err = es.FilterToQueryReducer(ctx, &states)
|
2022-03-23 09:02:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2024-01-05 10:01:48 +01:00
|
|
|
step := states.byName(migration.String())
|
|
|
|
if step == nil {
|
|
|
|
return true, nil
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|
2024-01-05 10:01:48 +01:00
|
|
|
if step.state == StepFailed {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if step.state == StepStarted {
|
2022-11-23 16:31:59 +01:00
|
|
|
return false, errMigrationAlreadyStarted
|
2022-07-20 11:20:49 +02:00
|
|
|
}
|
2024-01-05 10:01:48 +01:00
|
|
|
|
2022-07-20 11:20:49 +02:00
|
|
|
repeatable, ok := migration.(RepeatableMigration)
|
|
|
|
if !ok {
|
2024-01-05 10:01:48 +01:00
|
|
|
return step.state != StepDone, nil
|
2022-07-20 11:20:49 +02:00
|
|
|
}
|
2024-01-05 10:01:48 +01:00
|
|
|
lastRun, _ := step.LastRun.(map[string]interface{})
|
|
|
|
return repeatable.Check(lastRun), nil
|
2022-03-23 09:02:39 +01:00
|
|
|
}
|