mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 21:17:32 +00:00
Merge branch 'main' into merge-main
This commit is contained in:
@@ -76,6 +76,7 @@ func (mig *FirstInstance) Execute(ctx context.Context) error {
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
@@ -13,8 +13,12 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed 10.sql
|
||||
correctCreationDate10 string
|
||||
//go:embed 10_create_temp_table.sql
|
||||
correctCreationDate10CreateTable string
|
||||
//go:embed 10_fill_table.sql
|
||||
correctCreationDate10FillTable string
|
||||
//go:embed 10_update.sql
|
||||
correctCreationDate10Update string
|
||||
)
|
||||
|
||||
type CorrectCreationDate struct {
|
||||
@@ -34,7 +38,17 @@ func (mig *CorrectCreationDate) Execute(ctx context.Context) (err error) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
res, err := tx.ExecContext(ctx, correctCreationDate10)
|
||||
_, err := tx.ExecContext(ctx, correctCreationDate10CreateTable)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = tx.ExecContext(ctx, correctCreationDate10FillTable)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
res, err := tx.ExecContext(ctx, correctCreationDate10Update)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
6
cmd/setup/10_create_temp_table.sql
Normal file
6
cmd/setup/10_create_temp_table.sql
Normal file
@@ -0,0 +1,6 @@
|
||||
CREATE temporary TABLE IF NOT EXISTS wrong_events (
|
||||
instance_id TEXT
|
||||
, event_sequence BIGINT
|
||||
, current_cd TIMESTAMPTZ
|
||||
, next_cd TIMESTAMPTZ
|
||||
);
|
@@ -1,9 +1,4 @@
|
||||
CREATE temporary TABLE IF NOT EXISTS wrong_events (
|
||||
instance_id TEXT
|
||||
, event_sequence BIGINT
|
||||
, current_cd TIMESTAMPTZ
|
||||
, next_cd TIMESTAMPTZ
|
||||
);
|
||||
TRUNCATE wrong_events;
|
||||
|
||||
INSERT INTO wrong_events (
|
||||
SELECT * FROM (
|
||||
@@ -21,6 +16,4 @@ INSERT INTO wrong_events (
|
||||
current_cd < next_cd
|
||||
ORDER BY
|
||||
event_sequence DESC
|
||||
);
|
||||
|
||||
UPDATE eventstore.events e SET creation_date = we.next_cd FROM wrong_events we WHERE e.event_sequence = we.event_sequence and e.instance_id = we.instance_id;
|
||||
);
|
1
cmd/setup/10_update.sql
Normal file
1
cmd/setup/10_update.sql
Normal file
@@ -0,0 +1 @@
|
||||
UPDATE eventstore.events e SET creation_date = we.next_cd FROM wrong_events we WHERE e.event_sequence = we.event_sequence and e.instance_id = we.instance_id;
|
51
cmd/setup/cleanup.go
Normal file
51
cmd/setup/cleanup.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package setup
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/database"
|
||||
"github.com/zitadel/zitadel/internal/eventstore"
|
||||
"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")
|
||||
|
||||
dbClient, err := database.Connect(config.Database, false)
|
||||
logging.OnError(err).Fatal("unable to connect to database")
|
||||
|
||||
es, err := eventstore.Start(&eventstore.Config{Client: dbClient})
|
||||
logging.OnError(err).Fatal("unable to start eventstore")
|
||||
migration.RegisterMappers(es)
|
||||
|
||||
step, err := migration.LatestStep(ctx, es)
|
||||
logging.OnError(err).Fatal("unable to query latest migration")
|
||||
|
||||
if step.BaseEvent.EventType != migration.StartedType {
|
||||
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")
|
||||
}
|
@@ -33,7 +33,8 @@ func (mig *externalConfigChange) Check() bool {
|
||||
}
|
||||
|
||||
func (mig *externalConfigChange) Execute(ctx context.Context) error {
|
||||
cmd, err := command.StartCommands(mig.es,
|
||||
cmd, err := command.StartCommands(
|
||||
mig.es,
|
||||
systemdefaults.SystemDefaults{},
|
||||
nil,
|
||||
nil,
|
||||
@@ -50,6 +51,7 @@ func (mig *externalConfigChange) Execute(ctx context.Context) error {
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
|
@@ -45,6 +45,8 @@ Requirements:
|
||||
},
|
||||
}
|
||||
|
||||
cmd.AddCommand(NewCleanup())
|
||||
|
||||
Flags(cmd)
|
||||
|
||||
return cmd
|
||||
|
Reference in New Issue
Block a user