Skip to content

Commit

Permalink
Polish constants' names to follow Go convention
Browse files Browse the repository at this point in the history
  • Loading branch information
vasayxtx committed Jan 9, 2025
1 parent b75db01 commit f7b8fb3
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
.vscode/
vendor/
coverage.out
17 changes: 0 additions & 17 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,6 @@ const (
DialectMSSQL Dialect = "mssql"
)

// PostgresErrCode defines the type for Postgres error codes.
type PostgresErrCode string

// Postgres error codes (will be filled gradually).
const (
PgxErrCodeUniqueViolation PostgresErrCode = "23505"
PgxErrCodeDeadlockDetected PostgresErrCode = "40P01"
PgxErrCodeSerializationFailure PostgresErrCode = "40001"
PgxErrFeatureNotSupported PostgresErrCode = "0A000"

// nolint: staticcheck // lib/pq using is deprecated. Use pgx Postgres driver.
PostgresErrCodeUniqueViolation PostgresErrCode = "unique_violation"
// nolint: staticcheck // lib/pq using is deprecated. Use pgx Postgres driver.
PostgresErrCodeDeadlockDetected PostgresErrCode = "deadlock_detected"
PostgresErrCodeSerializationFailure PostgresErrCode = "serialization_failure"
)

// PostgresSSLMode defines possible values for Postgres sslmode connection parameter.
type PostgresSSLMode string

Expand Down
8 changes: 4 additions & 4 deletions dbrutil/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import (
"github.com/stretchr/testify/require"

"github.com/acronis/go-dbkit"
_ "github.com/acronis/go-dbkit/pgx"
"github.com/acronis/go-dbkit/pgx"
)

// Test that retriable errors stays retriable even wrapped in Tx structures
func TestTxErrorsIsRetriable(t *testing.T) {
retriable := []dbkit.PostgresErrCode{
dbkit.PgxErrCodeDeadlockDetected,
dbkit.PgxErrCodeSerializationFailure,
retriable := []pgx.ErrCode{
pgx.ErrCodeDeadlockDetected,
pgx.ErrCodeSerializationFailure,
}

mkerr := func(code string) []error {
Expand Down
11 changes: 6 additions & 5 deletions mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func init() {
dbkit.RegisterIsRetryableFunc(&mssql.Driver{}, func(err error) bool {
var msErr mssql.Error
if errors.As(err, &msErr) {
if msErr.Number == int32(MSSQLErrDeadlock) { // deadlock error
if msErr.Number == int32(ErrDeadlock) { // deadlock error
return true
}
}
Expand All @@ -37,12 +37,13 @@ type ErrCode int32

// MSSQL error codes (will be filled gradually).
const (
MSSQLErrDeadlock ErrCode = 1205
MSSQLErrCodeUniqueViolation ErrCode = 2627
MSSQLErrCodeUniqueIndexViolation ErrCode = 2601
ErrDeadlock ErrCode = 1205
ErrCodeUniqueViolation ErrCode = 2627
ErrCodeUniqueIndexViolation ErrCode = 2601
)

// CheckMSSQLError checks if the passed error relates to MSSQL and it's internal code matches the one from the argument.
// CheckMSSQLError checks if the passed error relates to MSSQL,
// and it's internal code matches the one from the argument.
func CheckMSSQLError(err error, errCode ErrCode) bool {
var msErr mssql.Error
if errors.As(err, &msErr) {
Expand Down
22 changes: 11 additions & 11 deletions mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/

// Package mysql provides helpers for working MySQL database.
// Package mysql provides helpers for working with the MySQL database using the github.com/go-sql-driver/mysql driver.
// Should be imported explicitly.
// To register mysql as retryable func use side effect import like so:
//
Expand All @@ -25,7 +25,7 @@ func init() {
var mySQLError *mysql.MySQLError
if errors.As(err, &mySQLError) {
switch mySQLError.Number {
case uint16(MySQLErrDeadlock), uint16(MySQLErrLockTimedOut):
case uint16(ErrDeadlock), uint16(ErrLockTimedOut):
return true
}
}
Expand All @@ -36,21 +36,21 @@ func init() {
})
}

// MySQLErrCode defines the type for MySQL error codes.
// nolint: revive
type MySQLErrCode uint16
// ErrCode defines the type for MySQL error codes.
type ErrCode uint16

// MySQL error codes (will be filled gradually).
const (
MySQLErrCodeDupEntry MySQLErrCode = 1062
MySQLErrDeadlock MySQLErrCode = 1213
MySQLErrLockTimedOut MySQLErrCode = 1205
ErrCodeDupEntry ErrCode = 1062
ErrDeadlock ErrCode = 1213
ErrLockTimedOut ErrCode = 1205
)

// CheckMySQLError checks if the passed error relates to MySQL and it's internal code matches the one from the argument.
func CheckMySQLError(err error, errCode MySQLErrCode) bool {
// CheckMySQLError checks if the passed error relates to MySQL,
// and it's internal code matches the one from the argument.
func CheckMySQLError(err error, errCode ErrCode) bool {
var mySQLError *mysql.MySQLError
if ok := errors.As(err, &mySQLError); ok {
if errors.As(err, &mySQLError) {
return mySQLError.Number == uint16(errCode)
}
return false
Expand Down
8 changes: 4 additions & 4 deletions mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ func TestMysqlIsRetryable(t *testing.T) {
isRetryable := dbkit.GetIsRetryable(&mysql.MySQLDriver{})
require.NotNil(t, isRetryable)
require.True(t, isRetryable(&mysql.MySQLError{
Number: uint16(MySQLErrDeadlock),
Number: uint16(ErrDeadlock),
}))
require.True(t, isRetryable(&mysql.MySQLError{
Number: uint16(MySQLErrLockTimedOut),
Number: uint16(ErrLockTimedOut),
}))
require.True(t, isRetryable(mysql.ErrInvalidConn))
require.False(t, isRetryable(driver.ErrBadConn))
require.True(t, isRetryable(fmt.Errorf("wrapped error: %w", &mysql.MySQLError{
Number: uint16(MySQLErrDeadlock),
Number: uint16(ErrDeadlock),
})))
}

// TestCheckMySQLError covers behavior of CheckMySQLError func.
func TestCheckMySQLError(t *testing.T) {
var deadlockErr MySQLErrCode = 1213
var deadlockErr ErrCode = 1213
sqlErr := &mysql.MySQLError{
Number: 1213,
Message: "deadlock found when trying to get lock",
Expand Down
2 changes: 1 addition & 1 deletion pgx/deadlock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ import (
func TestDeadlockErrorHandling(t *gotesting.T) {
testing.DeadlockTest(t, dbkit.DialectPgx,
func(err error) bool {
return CheckPostgresError(err, dbkit.PgxErrCodeDeadlockDetected)
return CheckPostgresError(err, ErrCodeDeadlockDetected)
})
}
26 changes: 18 additions & 8 deletions pgx/postgres.go → pgx/pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/

// Package pgx provides helpers for working Postgres database via jackc/pgx driver.
// Package pgx provides helpers for working with the Postgres database using the github.com/jackc/pgx driver.
// Should be imported explicitly.
// To register postgres as retryable func use side effect import like so:
//
Expand All @@ -25,10 +25,10 @@ func init() {
dbkit.RegisterIsRetryableFunc(&pg.Driver{}, func(err error) bool {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
switch errCode := dbkit.PostgresErrCode(pgErr.Code); errCode {
case dbkit.PgxErrCodeDeadlockDetected:
switch errCode := ErrCode(pgErr.Code); errCode {
case ErrCodeDeadlockDetected:
return true
case dbkit.PgxErrCodeSerializationFailure:
case ErrCodeSerializationFailure:
return true
}
if checkInvalidCachedPlanPgError(pgErr) {
Expand All @@ -39,12 +39,22 @@ func init() {
})
}

// ErrCode defines the type for Pgx error codes.
type ErrCode string

// Pgx error codes (will be filled gradually).
const (
ErrCodeUniqueViolation ErrCode = "23505"
ErrCodeDeadlockDetected ErrCode = "40P01"
ErrCodeSerializationFailure ErrCode = "40001"
ErrFeatureNotSupported ErrCode = "0A000"
)

// CheckPostgresError checks if the passed error relates to Postgres,
// and it's internal code matches the one from the argument.
func CheckPostgresError(err error, errCode dbkit.PostgresErrCode) bool {
func CheckPostgresError(err error, errCode ErrCode) bool {
var pgErr *pgconn.PgError
ok := errors.As(err, &pgErr)
if ok {
if errors.As(err, &pgErr) {
return pgErr.Code == string(errCode)
}
return false
Expand All @@ -69,6 +79,6 @@ func CheckInvalidCachedPlanError(err error) bool {
// Source: https://github.com/jackc/pgconn/blob/9cf57526250f6cd3e6cbf4fd7269c882e66898ce/stmtcache/lru.go#L91-L103
func checkInvalidCachedPlanPgError(pgErr *pgconn.PgError) bool {
return pgErr.Severity == "ERROR" &&
pgErr.Code == string(dbkit.PgxErrFeatureNotSupported) &&
pgErr.Code == string(ErrFeatureNotSupported) &&
pgErr.Message == "cached plan must not change result type"
}
6 changes: 3 additions & 3 deletions pgx/postgres_test.go → pgx/pgx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func TestPostgresIsRetryable(t *gotesting.T) {
isRetryable := dbkit.GetIsRetryable(&pg.Driver{})
require.NotNil(t, isRetryable)
// enum all retriable errors
retriable := []dbkit.PostgresErrCode{
dbkit.PgxErrCodeDeadlockDetected,
dbkit.PgxErrCodeSerializationFailure,
retriable := []ErrCode{
ErrCodeDeadlockDetected,
ErrCodeSerializationFailure,
}
for _, code := range retriable {
var err error
Expand Down
2 changes: 1 addition & 1 deletion postgres/deadlock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ import (
func TestDeadlockErrorHandling(t *gotesting.T) {
testing.DeadlockTest(t, dbkit.DialectPostgres,
func(err error) bool {
return CheckPostgresError(err, dbkit.PostgresErrCodeDeadlockDetected)
return CheckPostgresError(err, ErrCodeDeadlockDetected)
})
}
32 changes: 21 additions & 11 deletions postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/

// Package postgres provides helpers for working Postgres database.
// Package postgres provides helpers for working with the Postgres database using the github.com/lib/pq driver.
// Should be imported explicitly.
// To register postgres as retryable func use side effect import like so:
//
Expand All @@ -14,32 +14,42 @@ package postgres
import (
"errors"

pg "github.com/lib/pq"
"github.com/lib/pq"

"github.com/acronis/go-dbkit"
)

// nolint
func init() {
dbkit.RegisterIsRetryableFunc(&pg.Driver{}, func(err error) bool {
var pgErr *pg.Error
dbkit.RegisterIsRetryableFunc(&pq.Driver{}, func(err error) bool {
var pgErr *pq.Error
if errors.As(err, &pgErr) {
name := dbkit.PostgresErrCode(pgErr.Code.Name())
name := ErrCode(pgErr.Code.Name())
switch name {
case dbkit.PostgresErrCodeDeadlockDetected:
case ErrCodeDeadlockDetected:
return true
case dbkit.PostgresErrCodeSerializationFailure:
case ErrCodeSerializationFailure:
return true
}
}
return false
})
}

// CheckPostgresError checks if the passed error relates to Postgres and it's internal code matches the one from the argument.
// nolint: staticcheck // lib/pq using is deprecated. Use pgx Postgres driver.
func CheckPostgresError(err error, errCode dbkit.PostgresErrCode) bool {
var pgErr *pg.Error
// ErrCode defines the type for Postgres error codes.
type ErrCode string

// Postgres error codes (will be filled gradually).
const (
ErrCodeUniqueViolation ErrCode = "unique_violation"
ErrCodeDeadlockDetected ErrCode = "deadlock_detected"
ErrCodeSerializationFailure ErrCode = "serialization_failure"
)

// CheckPostgresError checks if the passed error relates to Postgres,
// and it's internal code matches the one from the argument.
func CheckPostgresError(err error, errCode ErrCode) bool {
var pgErr *pq.Error
if errors.As(err, &pgErr) {
return pgErr.Code.Name() == string(errCode)
}
Expand Down
6 changes: 4 additions & 2 deletions sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ package sqlite

import (
"errors"
sqlite3 "github.com/mattn/go-sqlite3"

"github.com/mattn/go-sqlite3"

"github.com/acronis/go-dbkit"
)
Expand All @@ -32,7 +33,8 @@ func init() {
})
}

// CheckSQLiteError checks if the passed error relates to SQLite and it's internal code matches the one from the argument.
// CheckSQLiteError checks if the passed error relates to SQLite,
// and it's internal code matches the one from the argument.
func CheckSQLiteError(err error, errCode sqlite3.ErrNoExtended) bool {
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) {
Expand Down

0 comments on commit f7b8fb3

Please sign in to comment.