-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(eventstore): correct creation date of events (#5683)
* fix: add setup step to correct creation dates * fix(eventstore): replace now with statement ts * fix(step10): correct number * fix: handle wrong instance domain removed events
- Loading branch information
1 parent
c420de1
commit 8da8fbe
Showing
7 changed files
with
106 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package setup | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
|
||
"github.com/zitadel/logging" | ||
|
||
"github.com/zitadel/zitadel/internal/database" | ||
) | ||
|
||
var ( | ||
//go:embed 10.sql | ||
correctCreationDate10 string | ||
) | ||
|
||
type CorrectCreationDate struct { | ||
dbClient *database.DB | ||
} | ||
|
||
func (mig *CorrectCreationDate) Execute(ctx context.Context) (err error) { | ||
tx, err := mig.dbClient.Begin() | ||
if err != nil { | ||
return err | ||
} | ||
if mig.dbClient.Type() == "cockroach" { | ||
if _, err := tx.Exec("SET experimental_enable_temp_tables=on"); err != nil { | ||
return err | ||
} | ||
} | ||
defer func() { | ||
if err != nil { | ||
logging.OnError(tx.Rollback()).Debug("rollback failed") | ||
return | ||
} | ||
err = tx.Commit() | ||
}() | ||
for { | ||
res, err := tx.ExecContext(ctx, correctCreationDate10) | ||
if err != nil { | ||
return err | ||
} | ||
affected, _ := res.RowsAffected() | ||
logging.WithFields("count", affected).Info("creation dates changed") | ||
if affected == 0 { | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
func (mig *CorrectCreationDate) String() string { | ||
return "10_correct_creation_date" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
CREATE temporary TABLE IF NOT EXISTS wrong_events ( | ||
instance_id STRING | ||
, event_sequence INT8 | ||
, current_cd TIMESTAMPTZ | ||
, next_cd TIMESTAMPTZ | ||
); | ||
|
||
TRUNCATE wrong_events; | ||
|
||
INSERT INTO wrong_events ( | ||
SELECT * FROM ( | ||
SELECT | ||
instance_id | ||
, event_sequence | ||
, creation_date AS current_cd | ||
, lead(creation_date) OVER ( | ||
PARTITION BY instance_id | ||
ORDER BY event_sequence DESC | ||
) AS next_cd | ||
FROM | ||
eventstore.events | ||
) WHERE | ||
current_cd < next_cd | ||
ORDER BY | ||
event_sequence DESC | ||
); | ||
|
||
UPDATE eventstore.events e SET creation_date = we.next_cd FROM wrong_events we WHERE e.event_sequence = we.event_sequence and e.instance_id = we.instance_id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8da8fbe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./
docs-zitadel.vercel.app
docs-git-main-zitadel.vercel.app
zitadel-docs.vercel.app