Add retry for cockroachdb init to avoid a concurrency issue in CockroachDB, specifically a TransactionRetryError.

This commit is contained in:
go-jet 2024-10-29 11:53:38 +01:00
parent 49104d1969
commit aaf705d770

View file

@ -169,7 +169,10 @@ func initPostgresDB(dbType string, connectionString string) error {
for _, schemaName := range schemaNames {
fmt.Println("\nInitializing", schemaName, "schema...")
err = execFile(db, fmt.Sprintf("./testdata/init/%s/%s.sql", dbType, schemaName))
// retry add due to a concurrency issue in CockroachDB, specifically a TransactionRetryError
err = retry(3, func() error {
return execFile(db, fmt.Sprintf("./testdata/init/%s/%s.sql", dbType, schemaName))
})
if err != nil {
return fmt.Errorf("failed to execute sql file: %w", err)
}
@ -183,6 +186,23 @@ func initPostgresDB(dbType string, connectionString string) error {
return nil
}
func retry(count int, f func() error) error {
for i := 0; i < count; i++ {
err := f()
if err == nil {
break
}
if i == count-1 {
return err
}
}
return nil
}
func execFile(db *sql.DB, sqlFilePath string) error {
testSampleSql, err := os.ReadFile(sqlFilePath) // #nosec G304
if err != nil {