mirror of
https://github.com/zitadel/zitadel.git
synced 2025-01-08 19:07:39 +00:00
fix(setup): update chunks in step 11 (#5965)
This commit is contained in:
parent
c8b0e5a6c5
commit
ebca7b6e3d
@ -2,14 +2,25 @@ package setup
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/cockroachdb/cockroach-go/v2/crdb"
|
||||||
|
"github.com/zitadel/logging"
|
||||||
|
|
||||||
"github.com/zitadel/zitadel/internal/database"
|
"github.com/zitadel/zitadel/internal/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
//go:embed 11.sql
|
//go:embed 11/11_add_column.sql
|
||||||
addEventCreatedAt string
|
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 {
|
type AddEventCreatedAt struct {
|
||||||
@ -24,6 +35,59 @@ func (mig *AddEventCreatedAt) Execute(ctx context.Context) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err := mig.dbClient.ExecContext(ctx, addEventCreatedAt)
|
_, 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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
|
6
cmd/setup/11/11_add_column.sql
Normal file
6
cmd/setup/11/11_add_column.sql
Normal file
@ -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;
|
1
cmd/setup/11/11_fetch_events.sql
Normal file
1
cmd/setup/11/11_fetch_events.sql
Normal file
@ -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
|
1
cmd/setup/11/11_fill_column.sql
Normal file
1
cmd/setup/11/11_fill_column.sql
Normal file
@ -0,0 +1 @@
|
|||||||
|
UPDATE eventstore.events SET created_at = $1 WHERE id = $2
|
3
cmd/setup/11/11_set_column.sql
Normal file
3
cmd/setup/11/11_set_column.sql
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
BEGIN;
|
||||||
|
ALTER TABLE eventstore.events ALTER COLUMN created_at SET NOT NULL;
|
||||||
|
COMMIT;
|
Loading…
x
Reference in New Issue
Block a user