2023-04-28 11:55:35 +00:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/zitadel/logging"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2023-12-20 16:13:04 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database/dialect"
|
2023-04-28 11:55:35 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 10:19:10 +00:00
|
|
|
old_es "github.com/zitadel/zitadel/internal/eventstore/repository/sql"
|
|
|
|
new_es "github.com/zitadel/zitadel/internal/eventstore/v3"
|
2023-04-28 11:55:35 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/migration"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewCleanup() *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "cleanup",
|
|
|
|
Short: "cleans up migration if they got stuck",
|
|
|
|
Long: `cleans up migration if they got stuck`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
config := MustNewConfig(viper.GetViper())
|
|
|
|
Cleanup(config)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Cleanup(config *Config) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
logging.Info("cleanup started")
|
|
|
|
|
2023-12-20 16:13:04 +00:00
|
|
|
queryDBClient, err := database.Connect(config.Database, false, dialect.DBPurposeQuery)
|
2023-10-19 10:19:10 +00:00
|
|
|
logging.OnError(err).Fatal("unable to connect to database")
|
2023-12-20 16:13:04 +00:00
|
|
|
esPusherDBClient, err := database.Connect(config.Database, false, dialect.DBPurposeEventPusher)
|
2023-04-28 11:55:35 +00:00
|
|
|
logging.OnError(err).Fatal("unable to connect to database")
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
config.Eventstore.Pusher = new_es.NewEventstore(esPusherDBClient)
|
2023-12-20 16:13:04 +00:00
|
|
|
config.Eventstore.Querier = old_es.NewCRDB(queryDBClient)
|
2023-10-19 10:19:10 +00:00
|
|
|
es := eventstore.NewEventstore(config.Eventstore)
|
2023-04-28 11:55:35 +00:00
|
|
|
|
2024-01-05 09:01:48 +00:00
|
|
|
step, err := migration.LastStuckStep(ctx, es)
|
2023-04-28 11:55:35 +00:00
|
|
|
logging.OnError(err).Fatal("unable to query latest migration")
|
|
|
|
|
2024-01-05 09:01:48 +00:00
|
|
|
if step == nil {
|
2023-04-28 11:55:35 +00:00
|
|
|
logging.Info("there is no stuck migration please run `zitadel setup`")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logging.WithFields("name", step.Name).Info("cleanup migration")
|
|
|
|
|
|
|
|
err = migration.CancelStep(ctx, es, step)
|
|
|
|
logging.OnError(err).Fatal("cleanup migration failed please retry")
|
|
|
|
}
|