From c8b4ef28bd315e35b43bc931f2d31b2445242b51 Mon Sep 17 00:00:00 2001 From: adlerhurst Date: Sat, 24 Jun 2023 08:54:25 +0200 Subject: [PATCH] fix(migration): speed up update and add logs --- cmd/setup/10.go | 2 +- cmd/setup/11.go | 36 +++++++++++++++++++++----------- cmd/setup/11/11_fetch_events.sql | 3 ++- cmd/setup/11/11_fill_column.sql | 2 +- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/cmd/setup/10.go b/cmd/setup/10.go index a83113cb18..16d14d3d09 100644 --- a/cmd/setup/10.go +++ b/cmd/setup/10.go @@ -35,7 +35,7 @@ func (mig *CorrectCreationDate) Execute(ctx context.Context) (err error) { defer cancel() for { - affected := int64(0) + var affected int64 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 { diff --git a/cmd/setup/11.go b/cmd/setup/11.go index 544188d126..c3ddadfff7 100644 --- a/cmd/setup/11.go +++ b/cmd/setup/11.go @@ -38,24 +38,34 @@ type AddEventCreatedAt struct { func (mig *AddEventCreatedAt) Execute(ctx context.Context) error { // execute step 10 again because events created after the first execution of step 10 // could still have the wrong ordering of sequences and creation date + logging.WithFields("step", "11").Info("ensure creation dates order") if err := mig.step10.Execute(ctx); err != nil { + logging.WithFields("step", "11").WithError(err).Info("ensure creation dates order failed") return err } + logging.WithFields("step", "11").Info("ensure creation dates order done") + logging.WithFields("step", "11").Info("add created_at column") _, err := mig.dbClient.ExecContext(ctx, addEventCreatedAt) if err != nil { + logging.WithFields("step", "11").WithError(err).Info("add created_at column failed") return err } + logging.WithFields("step", "11").Info("created_at column added") createIndex, err := readStmt(createdAtIndexCreateStmt, "11", mig.dbClient.Type(), "create_index.sql") if err != nil { return err } + logging.WithFields("step", "11").Info("create index") _, err = mig.dbClient.ExecContext(ctx, createIndex) + logging.WithFields("step", "11").WithError(err).Info("create index failed") if err != nil { return err } + logging.WithFields("step", "11").Info("index created") - for { + for i := 0; ; i++ { + logging.WithFields("step", "11", "iteration", i).Info("begin set created_at iteration") var count int err = crdb.ExecuteTx(ctx, mig.dbClient.DB, nil, func(tx *sql.Tx) error { rows, err := tx.Query(fetchCreatedAt, mig.BulkAmount) @@ -64,28 +74,30 @@ func (mig *AddEventCreatedAt) Execute(ctx context.Context) error { } defer rows.Close() - data := make(map[string]time.Time, 20) + type date struct { + instanceID string + eventSequence uint64 + creationDate time.Time + } + dates := make([]*date, 0, 20) + for rows.Next() { count++ - var ( - id string - creationDate time.Time - ) - err = rows.Scan(&id, &creationDate) + d := new(date) + err = rows.Scan(&d.instanceID, &d.eventSequence, &d.creationDate) if err != nil { return err } - - data[id] = creationDate + dates = append(dates, d) } if err := rows.Err(); err != nil { return err } - for id, creationDate := range data { - _, err = tx.Exec(fillCreatedAt, creationDate, id) + for _, d := range dates { + _, err = tx.Exec(fillCreatedAt, d.creationDate, d.instanceID, d.eventSequence) if err != nil { return err } @@ -96,7 +108,7 @@ func (mig *AddEventCreatedAt) Execute(ctx context.Context) error { if err != nil { return err } - logging.WithFields("count", count).Info("creation dates set") + logging.WithFields("step", "11", "iteration", i, "count", count).Info("set created_at iteration done") if count < 20 { break } diff --git a/cmd/setup/11/11_fetch_events.sql b/cmd/setup/11/11_fetch_events.sql index f668826751..64add9340f 100644 --- a/cmd/setup/11/11_fetch_events.sql +++ b/cmd/setup/11/11_fetch_events.sql @@ -1,5 +1,6 @@ SELECT - id + instance_id + , event_sequence , creation_date FROM eventstore.events diff --git a/cmd/setup/11/11_fill_column.sql b/cmd/setup/11/11_fill_column.sql index 0a16913db4..68265e6a7a 100644 --- a/cmd/setup/11/11_fill_column.sql +++ b/cmd/setup/11/11_fill_column.sql @@ -1 +1 @@ -UPDATE eventstore.events SET created_at = $1 WHERE id = $2 \ No newline at end of file +UPDATE eventstore.events SET created_at = $1 WHERE instance_id = $2 AND event_sequence = $3 \ No newline at end of file