2023-04-18 17:29:04 +00:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-04-25 08:12:53 +00:00
|
|
|
"database/sql"
|
2023-04-18 17:29:04 +00:00
|
|
|
_ "embed"
|
2023-04-25 08:12:53 +00:00
|
|
|
"time"
|
2023-04-18 17:29:04 +00:00
|
|
|
|
2023-04-25 08:12:53 +00:00
|
|
|
"github.com/cockroachdb/cockroach-go/v2/crdb"
|
2023-04-18 17:29:04 +00:00
|
|
|
"github.com/zitadel/logging"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-05-11 08:04:35 +00:00
|
|
|
//go:embed 10/10_create_temp_table.sql
|
2023-04-27 08:28:26 +00:00
|
|
|
correctCreationDate10CreateTable string
|
2023-05-11 08:04:35 +00:00
|
|
|
//go:embed 10/10_fill_table.sql
|
2023-04-27 08:28:26 +00:00
|
|
|
correctCreationDate10FillTable string
|
2023-05-11 08:04:35 +00:00
|
|
|
//go:embed 10/10_update.sql
|
2023-04-27 08:28:26 +00:00
|
|
|
correctCreationDate10Update string
|
2023-06-07 16:30:19 +00:00
|
|
|
//go:embed 10/10_count_wrong_events.sql
|
|
|
|
correctCreationDate10CountWrongEvents string
|
|
|
|
//go:embed 10/10_empty_table.sql
|
|
|
|
correctCreationDate10Truncate string
|
2023-04-18 17:29:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type CorrectCreationDate struct {
|
2023-04-25 08:12:53 +00:00
|
|
|
dbClient *database.DB
|
|
|
|
FailAfter time.Duration
|
2023-04-18 17:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mig *CorrectCreationDate) Execute(ctx context.Context) (err error) {
|
2023-04-25 08:12:53 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, mig.FailAfter)
|
|
|
|
defer cancel()
|
|
|
|
|
2023-04-18 17:29:04 +00:00
|
|
|
for {
|
2023-06-07 16:30:19 +00:00
|
|
|
affected := int64(0)
|
2023-04-25 08:12:53 +00:00
|
|
|
err = crdb.ExecuteTx(ctx, mig.dbClient.DB, nil, func(tx *sql.Tx) error {
|
|
|
|
if mig.dbClient.Type() == "cockroach" {
|
|
|
|
if _, err := tx.Exec("SET experimental_enable_temp_tables=on"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-04-27 08:28:26 +00:00
|
|
|
_, err := tx.ExecContext(ctx, correctCreationDate10CreateTable)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-07 16:30:19 +00:00
|
|
|
_, err = tx.ExecContext(ctx, correctCreationDate10Truncate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-27 08:28:26 +00:00
|
|
|
_, err = tx.ExecContext(ctx, correctCreationDate10FillTable)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-07 16:30:19 +00:00
|
|
|
res := tx.QueryRowContext(ctx, correctCreationDate10CountWrongEvents)
|
|
|
|
if err := res.Scan(&affected); err != nil || affected == 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = tx.ExecContext(ctx, correctCreationDate10Update)
|
2023-04-25 08:12:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logging.WithFields("count", affected).Info("creation dates changed")
|
2023-04-18 17:29:04 +00:00
|
|
|
return nil
|
2023-04-25 08:12:53 +00:00
|
|
|
})
|
|
|
|
if affected == 0 || err != nil {
|
|
|
|
return err
|
2023-04-18 17:29:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mig *CorrectCreationDate) String() string {
|
|
|
|
return "10_correct_creation_date"
|
|
|
|
}
|