From ebca7b6e3d4e2f5d3543cb83b606284a61bfbf23 Mon Sep 17 00:00:00 2001 From: Silvan Date: Fri, 2 Jun 2023 11:31:18 +0200 Subject: [PATCH] fix(setup): update chunks in step 11 (#5965) --- cmd/setup/11.go | 66 +++++++++++++++++++++++++++++++- cmd/setup/11.sql | 15 -------- cmd/setup/11/11_add_column.sql | 6 +++ cmd/setup/11/11_fetch_events.sql | 1 + cmd/setup/11/11_fill_column.sql | 1 + cmd/setup/11/11_set_column.sql | 3 ++ 6 files changed, 76 insertions(+), 16 deletions(-) delete mode 100644 cmd/setup/11.sql create mode 100644 cmd/setup/11/11_add_column.sql create mode 100644 cmd/setup/11/11_fetch_events.sql create mode 100644 cmd/setup/11/11_fill_column.sql create mode 100644 cmd/setup/11/11_set_column.sql diff --git a/cmd/setup/11.go b/cmd/setup/11.go index 4745c71994..4126216da1 100644 --- a/cmd/setup/11.go +++ b/cmd/setup/11.go @@ -2,14 +2,25 @@ package setup import ( "context" + "database/sql" _ "embed" + "time" + + "github.com/cockroachdb/cockroach-go/v2/crdb" + "github.com/zitadel/logging" "github.com/zitadel/zitadel/internal/database" ) var ( - //go:embed 11.sql + //go:embed 11/11_add_column.sql addEventCreatedAt string + //go:embed 11/11_fetch_events.sql + fetchCreatedAt string + //go:embed 11/11_fill_column.sql + fillCreatedAt string + //go:embed 11/11_set_column.sql + setCreatedAtDetails string ) type AddEventCreatedAt struct { @@ -24,6 +35,59 @@ func (mig *AddEventCreatedAt) Execute(ctx context.Context) error { return err } _, err := mig.dbClient.ExecContext(ctx, addEventCreatedAt) + if err != nil { + return err + } + + for { + var count int + err = crdb.ExecuteTx(ctx, mig.dbClient.DB, nil, func(tx *sql.Tx) error { + rows, err := tx.Query(fetchCreatedAt) + if err != nil { + return err + } + defer rows.Close() + + data := make(map[string]time.Time, 20) + for rows.Next() { + count++ + var ( + id string + creationDate time.Time + ) + + err = rows.Scan(&id, &creationDate) + if err != nil { + return err + } + + data[id] = creationDate + + } + if err := rows.Err(); err != nil { + return err + } + + for id, creationDate := range data { + _, err = tx.Exec(fillCreatedAt, creationDate, id) + if err != nil { + return err + } + } + + return nil + }) + if err != nil { + return err + } + logging.WithFields("count", count).Debug("creation dates set") + if count < 20 { + break + } + } + + logging.Info("set details") + _, err = mig.dbClient.ExecContext(ctx, setCreatedAtDetails) return err } diff --git a/cmd/setup/11.sql b/cmd/setup/11.sql deleted file mode 100644 index bb62b5f9b2..0000000000 --- a/cmd/setup/11.sql +++ /dev/null @@ -1,15 +0,0 @@ -BEGIN; --- create table with empty created_at -ALTER TABLE eventstore.events ADD COLUMN created_at TIMESTAMPTZ DEFAULT NULL; -COMMIT; - -BEGIN; --- backfill created_at -UPDATE eventstore.events SET created_at = creation_date WHERE created_at IS NULL; -COMMIT; - -BEGIN; --- set column rules -ALTER TABLE eventstore.events ALTER COLUMN created_at SET DEFAULT clock_timestamp(); -ALTER TABLE eventstore.events ALTER COLUMN created_at SET NOT NULL; -COMMIT; \ No newline at end of file diff --git a/cmd/setup/11/11_add_column.sql b/cmd/setup/11/11_add_column.sql new file mode 100644 index 0000000000..0ba49b3d1f --- /dev/null +++ b/cmd/setup/11/11_add_column.sql @@ -0,0 +1,6 @@ +BEGIN; +-- create table with empty created_at +ALTER TABLE eventstore.events ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ DEFAULT NULL; +-- set column rules +ALTER TABLE eventstore.events ALTER COLUMN created_at SET DEFAULT clock_timestamp(); +COMMIT; \ No newline at end of file diff --git a/cmd/setup/11/11_fetch_events.sql b/cmd/setup/11/11_fetch_events.sql new file mode 100644 index 0000000000..7c75125265 --- /dev/null +++ b/cmd/setup/11/11_fetch_events.sql @@ -0,0 +1 @@ +SELECT id, creation_date FROM eventstore.events WHERE created_at IS NULL ORDER BY event_sequence DESC, instance_id LIMIT 20 FOR UPDATE \ No newline at end of file diff --git a/cmd/setup/11/11_fill_column.sql b/cmd/setup/11/11_fill_column.sql new file mode 100644 index 0000000000..0a16913db4 --- /dev/null +++ b/cmd/setup/11/11_fill_column.sql @@ -0,0 +1 @@ +UPDATE eventstore.events SET created_at = $1 WHERE id = $2 \ No newline at end of file diff --git a/cmd/setup/11/11_set_column.sql b/cmd/setup/11/11_set_column.sql new file mode 100644 index 0000000000..108bd182bc --- /dev/null +++ b/cmd/setup/11/11_set_column.sql @@ -0,0 +1,3 @@ +BEGIN; +ALTER TABLE eventstore.events ALTER COLUMN created_at SET NOT NULL; +COMMIT; \ No newline at end of file