mirror of
https://github.com/zitadel/zitadel.git
synced 2025-05-01 13:11:07 +00:00
fix(setup): steps 10 and 11 (#5987)
* fix(step10): count amount of wrong_events manually * fix(step11): create index for better performance
This commit is contained in:
parent
f456168a74
commit
6be41ea257
@ -19,6 +19,10 @@ var (
|
|||||||
correctCreationDate10FillTable string
|
correctCreationDate10FillTable string
|
||||||
//go:embed 10/10_update.sql
|
//go:embed 10/10_update.sql
|
||||||
correctCreationDate10Update string
|
correctCreationDate10Update string
|
||||||
|
//go:embed 10/10_count_wrong_events.sql
|
||||||
|
correctCreationDate10CountWrongEvents string
|
||||||
|
//go:embed 10/10_empty_table.sql
|
||||||
|
correctCreationDate10Truncate string
|
||||||
)
|
)
|
||||||
|
|
||||||
type CorrectCreationDate struct {
|
type CorrectCreationDate struct {
|
||||||
@ -31,7 +35,7 @@ func (mig *CorrectCreationDate) Execute(ctx context.Context) (err error) {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
var affected int64
|
affected := int64(0)
|
||||||
err = crdb.ExecuteTx(ctx, mig.dbClient.DB, nil, func(tx *sql.Tx) error {
|
err = crdb.ExecuteTx(ctx, mig.dbClient.DB, nil, func(tx *sql.Tx) error {
|
||||||
if mig.dbClient.Type() == "cockroach" {
|
if mig.dbClient.Type() == "cockroach" {
|
||||||
if _, err := tx.Exec("SET experimental_enable_temp_tables=on"); err != nil {
|
if _, err := tx.Exec("SET experimental_enable_temp_tables=on"); err != nil {
|
||||||
@ -43,16 +47,24 @@ func (mig *CorrectCreationDate) Execute(ctx context.Context) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, err = tx.ExecContext(ctx, correctCreationDate10Truncate)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
_, err = tx.ExecContext(ctx, correctCreationDate10FillTable)
|
_, err = tx.ExecContext(ctx, correctCreationDate10FillTable)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := tx.ExecContext(ctx, correctCreationDate10Update)
|
res := tx.QueryRowContext(ctx, correctCreationDate10CountWrongEvents)
|
||||||
|
if err := res.Scan(&affected); err != nil || affected == 0 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = tx.ExecContext(ctx, correctCreationDate10Update)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
affected, _ = res.RowsAffected()
|
|
||||||
logging.WithFields("count", affected).Info("creation dates changed")
|
logging.WithFields("count", affected).Info("creation dates changed")
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
1
cmd/setup/10/10_count_wrong_events.sql
Normal file
1
cmd/setup/10/10_count_wrong_events.sql
Normal file
@ -0,0 +1 @@
|
|||||||
|
SELECT COUNT(*) FROM wrong_events
|
1
cmd/setup/10/10_empty_table.sql
Normal file
1
cmd/setup/10/10_empty_table.sql
Normal file
@ -0,0 +1 @@
|
|||||||
|
TRUNCATE wrong_events
|
@ -1,5 +1,3 @@
|
|||||||
TRUNCATE wrong_events;
|
|
||||||
|
|
||||||
INSERT INTO wrong_events (
|
INSERT INTO wrong_events (
|
||||||
SELECT * FROM (
|
SELECT * FROM (
|
||||||
SELECT
|
SELECT
|
||||||
|
@ -3,7 +3,7 @@ package setup
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
_ "embed"
|
"embed"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/cockroachdb/cockroach-go/v2/crdb"
|
"github.com/cockroachdb/cockroach-go/v2/crdb"
|
||||||
@ -21,11 +21,18 @@ var (
|
|||||||
fillCreatedAt string
|
fillCreatedAt string
|
||||||
//go:embed 11/11_set_column.sql
|
//go:embed 11/11_set_column.sql
|
||||||
setCreatedAtDetails string
|
setCreatedAtDetails string
|
||||||
|
//go:embed 11/postgres/create_index.sql
|
||||||
|
//go:embed 11/cockroach/create_index.sql
|
||||||
|
createdAtIndexCreateStmt embed.FS
|
||||||
|
//go:embed 11/postgres/drop_index.sql
|
||||||
|
//go:embed 11/cockroach/drop_index.sql
|
||||||
|
createdAtIndexDropStmt embed.FS
|
||||||
)
|
)
|
||||||
|
|
||||||
type AddEventCreatedAt struct {
|
type AddEventCreatedAt struct {
|
||||||
step10 *CorrectCreationDate
|
BulkAmount int
|
||||||
dbClient *database.DB
|
step10 *CorrectCreationDate
|
||||||
|
dbClient *database.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mig *AddEventCreatedAt) Execute(ctx context.Context) error {
|
func (mig *AddEventCreatedAt) Execute(ctx context.Context) error {
|
||||||
@ -39,10 +46,19 @@ func (mig *AddEventCreatedAt) Execute(ctx context.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
createIndex, err := readStmt(createdAtIndexCreateStmt, "11", mig.dbClient.Type(), "create_index.sql")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = mig.dbClient.ExecContext(ctx, createIndex)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
var count int
|
var count int
|
||||||
err = crdb.ExecuteTx(ctx, mig.dbClient.DB, nil, func(tx *sql.Tx) error {
|
err = crdb.ExecuteTx(ctx, mig.dbClient.DB, nil, func(tx *sql.Tx) error {
|
||||||
rows, err := tx.Query(fetchCreatedAt)
|
rows, err := tx.Query(fetchCreatedAt, mig.BulkAmount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -80,7 +96,7 @@ func (mig *AddEventCreatedAt) Execute(ctx context.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logging.WithFields("count", count).Debug("creation dates set")
|
logging.WithFields("count", count).Info("creation dates set")
|
||||||
if count < 20 {
|
if count < 20 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -88,6 +104,16 @@ func (mig *AddEventCreatedAt) Execute(ctx context.Context) error {
|
|||||||
|
|
||||||
logging.Info("set details")
|
logging.Info("set details")
|
||||||
_, err = mig.dbClient.ExecContext(ctx, setCreatedAtDetails)
|
_, err = mig.dbClient.ExecContext(ctx, setCreatedAtDetails)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
dropIndex, err := readStmt(createdAtIndexDropStmt, "11", mig.dbClient.Type(), "drop_index.sql")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = mig.dbClient.ExecContext(ctx, dropIndex)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1 +1,12 @@
|
|||||||
SELECT id, creation_date FROM eventstore.events WHERE created_at IS NULL ORDER BY event_sequence DESC, instance_id LIMIT 20 FOR UPDATE
|
SELECT
|
||||||
|
id
|
||||||
|
, creation_date
|
||||||
|
FROM
|
||||||
|
eventstore.events
|
||||||
|
WHERE
|
||||||
|
created_at IS NULL
|
||||||
|
ORDER BY
|
||||||
|
event_sequence DESC
|
||||||
|
, instance_id
|
||||||
|
LIMIT $1
|
||||||
|
FOR UPDATE
|
8
cmd/setup/11/cockroach/create_index.sql
Normal file
8
cmd/setup/11/cockroach/create_index.sql
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
CREATE INDEX IF NOT EXISTS ca_fill_idx ON eventstore.events (
|
||||||
|
event_sequence DESC
|
||||||
|
, instance_id
|
||||||
|
) STORING (
|
||||||
|
id
|
||||||
|
, creation_date
|
||||||
|
, created_at
|
||||||
|
) WHERE created_at IS NULL;
|
1
cmd/setup/11/cockroach/drop_index.sql
Normal file
1
cmd/setup/11/cockroach/drop_index.sql
Normal file
@ -0,0 +1 @@
|
|||||||
|
DROP INDEX IF EXISTS eventstore.events@ca_fill_idx;
|
4
cmd/setup/11/postgres/create_index.sql
Normal file
4
cmd/setup/11/postgres/create_index.sql
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
CREATE INDEX IF NOT EXISTS ca_fill_idx ON eventstore.events (
|
||||||
|
event_sequence DESC
|
||||||
|
, instance_id
|
||||||
|
) WHERE created_at IS NULL;
|
1
cmd/setup/11/postgres/drop_index.sql
Normal file
1
cmd/setup/11/postgres/drop_index.sql
Normal file
@ -0,0 +1 @@
|
|||||||
|
DROP INDEX IF EXISTS eventstore.ca_fill_idx;
|
@ -66,7 +66,7 @@ type Steps struct {
|
|||||||
s8AuthTokens *AuthTokenIndexes
|
s8AuthTokens *AuthTokenIndexes
|
||||||
s9EventstoreIndexes2 *EventstoreIndexesNew
|
s9EventstoreIndexes2 *EventstoreIndexesNew
|
||||||
CorrectCreationDate *CorrectCreationDate
|
CorrectCreationDate *CorrectCreationDate
|
||||||
s11AddEventCreatedAt *AddEventCreatedAt
|
AddEventCreatedAt *AddEventCreatedAt
|
||||||
}
|
}
|
||||||
|
|
||||||
type encryptionKeyConfig struct {
|
type encryptionKeyConfig struct {
|
||||||
|
@ -91,7 +91,8 @@ func Setup(config *Config, steps *Steps, masterKey string) {
|
|||||||
steps.s8AuthTokens = &AuthTokenIndexes{dbClient: dbClient}
|
steps.s8AuthTokens = &AuthTokenIndexes{dbClient: dbClient}
|
||||||
steps.s9EventstoreIndexes2 = New09(dbClient)
|
steps.s9EventstoreIndexes2 = New09(dbClient)
|
||||||
steps.CorrectCreationDate.dbClient = dbClient
|
steps.CorrectCreationDate.dbClient = dbClient
|
||||||
steps.s11AddEventCreatedAt = &AddEventCreatedAt{dbClient: dbClient, step10: steps.CorrectCreationDate}
|
steps.AddEventCreatedAt.dbClient = dbClient
|
||||||
|
steps.AddEventCreatedAt.step10 = steps.CorrectCreationDate
|
||||||
|
|
||||||
err = projection.Create(ctx, dbClient, eventstoreClient, config.Projections, nil, nil)
|
err = projection.Create(ctx, dbClient, eventstoreClient, config.Projections, nil, nil)
|
||||||
logging.OnError(err).Fatal("unable to start projections")
|
logging.OnError(err).Fatal("unable to start projections")
|
||||||
@ -129,7 +130,7 @@ func Setup(config *Config, steps *Steps, masterKey string) {
|
|||||||
logging.OnError(err).Fatal("unable to migrate step 9")
|
logging.OnError(err).Fatal("unable to migrate step 9")
|
||||||
err = migration.Migrate(ctx, eventstoreClient, steps.CorrectCreationDate)
|
err = migration.Migrate(ctx, eventstoreClient, steps.CorrectCreationDate)
|
||||||
logging.OnError(err).Fatal("unable to migrate step 10")
|
logging.OnError(err).Fatal("unable to migrate step 10")
|
||||||
err = migration.Migrate(ctx, eventstoreClient, steps.s11AddEventCreatedAt)
|
err = migration.Migrate(ctx, eventstoreClient, steps.AddEventCreatedAt)
|
||||||
logging.OnError(err).Fatal("unable to migrate step 11")
|
logging.OnError(err).Fatal("unable to migrate step 11")
|
||||||
|
|
||||||
for _, repeatableStep := range repeatableSteps {
|
for _, repeatableStep := range repeatableSteps {
|
||||||
|
@ -30,5 +30,9 @@ FirstInstance:
|
|||||||
MachineKey:
|
MachineKey:
|
||||||
ExpirationDate:
|
ExpirationDate:
|
||||||
Type:
|
Type:
|
||||||
|
|
||||||
CorrectCreationDate:
|
CorrectCreationDate:
|
||||||
FailAfter: 5m
|
FailAfter: 5m
|
||||||
|
|
||||||
|
AddEventCreatedAt:
|
||||||
|
BulkAmount: 100
|
Loading…
x
Reference in New Issue
Block a user