Skip to content

Commit

Permalink
fix(setup): smaller transactions (#5742)
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerhurst committed Apr 25, 2023
1 parent 095ec21 commit 13f6b46
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 38 deletions.
48 changes: 24 additions & 24 deletions cmd/setup/10.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ 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"
Expand All @@ -15,35 +18,32 @@ var (
)

type CorrectCreationDate struct {
dbClient *database.DB
dbClient *database.DB
FailAfter time.Duration
}

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()
}()
ctx, cancel := context.WithTimeout(ctx, mig.FailAfter)
defer cancel()

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 {
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 {
return err
}
}
res, err := tx.ExecContext(ctx, correctCreationDate10)
if err != nil {
return err
}
affected, _ = res.RowsAffected()
logging.WithFields("count", affected).Info("creation dates changed")
return nil
})
if affected == 0 || err != nil {
return err
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions cmd/setup/10.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ CREATE temporary TABLE IF NOT EXISTS wrong_events (
, next_cd TIMESTAMPTZ
);

TRUNCATE wrong_events;

INSERT INTO wrong_events (
SELECT * FROM (
SELECT
Expand Down
20 changes: 10 additions & 10 deletions cmd/setup/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ func MustNewConfig(v *viper.Viper) *Config {
}

type Steps struct {
s1ProjectionTable *ProjectionTable
s2AssetsTable *AssetTable
FirstInstance *FirstInstance
s4EventstoreIndexes *EventstoreIndexesNew
s5LastFailed *LastFailed
s6OwnerRemoveColumns *OwnerRemoveColumns
s7LogstoreTables *LogstoreTables
s8AuthTokens *AuthTokenIndexes
s9EventstoreIndexes2 *EventstoreIndexesNew
s10EventstoreCreationDate *CorrectCreationDate
s1ProjectionTable *ProjectionTable
s2AssetsTable *AssetTable
FirstInstance *FirstInstance
s4EventstoreIndexes *EventstoreIndexesNew
s5LastFailed *LastFailed
s6OwnerRemoveColumns *OwnerRemoveColumns
s7LogstoreTables *LogstoreTables
s8AuthTokens *AuthTokenIndexes
s9EventstoreIndexes2 *EventstoreIndexesNew
CorrectCreationDate *CorrectCreationDate
}

type encryptionKeyConfig struct {
Expand Down
4 changes: 2 additions & 2 deletions cmd/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func Setup(config *Config, steps *Steps, masterKey string) {
steps.s7LogstoreTables = &LogstoreTables{dbClient: dbClient.DB, username: config.Database.Username(), dbType: config.Database.Type()}
steps.s8AuthTokens = &AuthTokenIndexes{dbClient: dbClient}
steps.s9EventstoreIndexes2 = New09(dbClient)
steps.s10EventstoreCreationDate = &CorrectCreationDate{dbClient: dbClient}
steps.CorrectCreationDate.dbClient = dbClient

err = projection.Create(ctx, dbClient, eventstoreClient, config.Projections, nil, nil)
logging.OnError(err).Fatal("unable to start projections")
Expand Down Expand Up @@ -124,7 +124,7 @@ func Setup(config *Config, steps *Steps, masterKey string) {
logging.OnError(err).Fatal("unable to migrate step 8")
err = migration.Migrate(ctx, eventstoreClient, steps.s9EventstoreIndexes2)
logging.OnError(err).Fatal("unable to migrate step 9")
err = migration.Migrate(ctx, eventstoreClient, steps.s10EventstoreCreationDate)
err = migration.Migrate(ctx, eventstoreClient, steps.CorrectCreationDate)
logging.OnError(err).Fatal("unable to migrate step 10")

for _, repeatableStep := range repeatableSteps {
Expand Down
2 changes: 2 additions & 0 deletions cmd/setup/steps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ FirstInstance:
MachineKey:
ExpirationDate:
Type:
CorrectCreationDate:
FailAfter: 5m

1 comment on commit 13f6b46

@vercel
Copy link

@vercel vercel bot commented on 13f6b46 Apr 25, 2023

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 – ./

zitadel-docs.vercel.app
docs-zitadel.vercel.app
docs-git-main-zitadel.vercel.app

Please sign in to comment.