From 9a28a4c1056af217edbff781e77cbc4139ec7047 Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Tue, 2 Apr 2024 15:16:07 +0200 Subject: [PATCH] kvdb: remove the unused SQL error mappers --- kvdb/go.mod | 4 +- kvdb/sqlbase/sqlerrors.go | 64 --------------------------- kvdb/sqlbase/sqlerrors_no_postgres.go | 9 ---- kvdb/sqlbase/sqlerrors_no_sqlite.go | 9 ---- kvdb/sqlbase/sqlerrors_postgres.go | 51 --------------------- kvdb/sqlbase/sqlerrors_sqlite.go | 53 ---------------------- 6 files changed, 2 insertions(+), 188 deletions(-) delete mode 100644 kvdb/sqlbase/sqlerrors.go delete mode 100644 kvdb/sqlbase/sqlerrors_no_postgres.go delete mode 100644 kvdb/sqlbase/sqlerrors_no_sqlite.go delete mode 100644 kvdb/sqlbase/sqlerrors_postgres.go delete mode 100644 kvdb/sqlbase/sqlerrors_sqlite.go diff --git a/kvdb/go.mod b/kvdb/go.mod index 861558c6c..721342950 100644 --- a/kvdb/go.mod +++ b/kvdb/go.mod @@ -6,8 +6,6 @@ require ( github.com/davecgh/go-spew v1.1.1 github.com/fergusstrange/embedded-postgres v1.25.0 github.com/google/btree v1.0.1 - github.com/jackc/pgconn v1.14.3 - github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438 github.com/jackc/pgx/v4 v4.18.1 github.com/lightningnetwork/lnd/healthcheck v1.0.0 github.com/lightningnetwork/lnd/sqldb v0.0.0-00010101000000-000000000000 @@ -51,6 +49,8 @@ require ( github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect github.com/imdario/mergo v0.3.12 // indirect github.com/jackc/chunkreader/v2 v2.0.1 // indirect + github.com/jackc/pgconn v1.14.3 // indirect + github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438 // indirect github.com/jackc/pgio v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgproto3/v2 v2.3.3 // indirect diff --git a/kvdb/sqlbase/sqlerrors.go b/kvdb/sqlbase/sqlerrors.go deleted file mode 100644 index 5e8afdb41..000000000 --- a/kvdb/sqlbase/sqlerrors.go +++ /dev/null @@ -1,64 +0,0 @@ -package sqlbase - -import ( - "errors" - "fmt" -) - -var ( - // ErrRetriesExceeded is returned when a transaction is retried more - // than the max allowed valued without a success. - ErrRetriesExceeded = errors.New("db tx retries exceeded") -) - -// MapSQLError attempts to interpret a given error as a database agnostic SQL -// error. -func MapSQLError(err error) error { - // Attempt to interpret the error as a sqlite error. - if sqliteErr := parseSqliteError(err); sqliteErr != nil { - return sqliteErr - } - - // Attempt to interpret the error as a postgres error. - if postgresErr := parsePostgresError(err); postgresErr != nil { - return postgresErr - } - - // Return original error if it could not be classified as a database - // specific error. - return err -} - -// ErrSQLUniqueConstraintViolation is an error type which represents a database -// agnostic SQL unique constraint violation. -type ErrSQLUniqueConstraintViolation struct { - DBError error -} - -func (e ErrSQLUniqueConstraintViolation) Error() string { - return fmt.Sprintf("sql unique constraint violation: %v", e.DBError) -} - -// ErrSerializationError is an error type which represents a database agnostic -// error that a transaction couldn't be serialized with other concurrent db -// transactions. -type ErrSerializationError struct { - DBError error -} - -// Unwrap returns the wrapped error. -func (e ErrSerializationError) Unwrap() error { - return e.DBError -} - -// Error returns the error message. -func (e ErrSerializationError) Error() string { - return e.DBError.Error() -} - -// IsSerializationError returns true if the given error is a serialization -// error. -func IsSerializationError(err error) bool { - var serializationError *ErrSerializationError - return errors.As(err, &serializationError) -} diff --git a/kvdb/sqlbase/sqlerrors_no_postgres.go b/kvdb/sqlbase/sqlerrors_no_postgres.go deleted file mode 100644 index 94f08b66a..000000000 --- a/kvdb/sqlbase/sqlerrors_no_postgres.go +++ /dev/null @@ -1,9 +0,0 @@ -//go:build !kvdb_postgres - -package sqlbase - -// parsePostgresError attempts to parse a postgres error as a database agnostic -// SQL error. -func parsePostgresError(err error) error { - return nil -} diff --git a/kvdb/sqlbase/sqlerrors_no_sqlite.go b/kvdb/sqlbase/sqlerrors_no_sqlite.go deleted file mode 100644 index 9f475a14c..000000000 --- a/kvdb/sqlbase/sqlerrors_no_sqlite.go +++ /dev/null @@ -1,9 +0,0 @@ -//go:build !kvdb_sqlite || (windows && (arm || 386)) || (linux && (ppc64 || mips || mipsle || mips64)) - -package sqlbase - -// parseSqliteError attempts to parse a sqlite error as a database agnostic -// SQL error. -func parseSqliteError(err error) error { - return nil -} diff --git a/kvdb/sqlbase/sqlerrors_postgres.go b/kvdb/sqlbase/sqlerrors_postgres.go deleted file mode 100644 index 1b12dda66..000000000 --- a/kvdb/sqlbase/sqlerrors_postgres.go +++ /dev/null @@ -1,51 +0,0 @@ -//go:build kvdb_postgres - -package sqlbase - -import ( - "errors" - "fmt" - "strings" - - "github.com/jackc/pgconn" - "github.com/jackc/pgerrcode" -) - -// parsePostgresError attempts to parse a postgres error as a database agnostic -// SQL error. -func parsePostgresError(err error) error { - if err == nil { - return nil - } - - // Sometimes the error won't be properly wrapped, so we'll need to - // inspect raw error itself to detect something we can wrap properly. - const postgresErrMsg = "could not serialize access" - if strings.Contains(err.Error(), postgresErrMsg) { - return &ErrSerializationError{ - DBError: err, - } - } - - var pqErr *pgconn.PgError - if !errors.As(err, &pqErr) { - return nil - } - - switch pqErr.Code { - // Handle unique constraint violation error. - case pgerrcode.UniqueViolation: - return &ErrSQLUniqueConstraintViolation{ - DBError: pqErr, - } - - // Unable to serialize the transaction, so we'll need to try again. - case pgerrcode.SerializationFailure: - return &ErrSerializationError{ - DBError: pqErr, - } - - default: - return fmt.Errorf("unknown postgres error: %w", pqErr) - } -} diff --git a/kvdb/sqlbase/sqlerrors_sqlite.go b/kvdb/sqlbase/sqlerrors_sqlite.go deleted file mode 100644 index 9df93893a..000000000 --- a/kvdb/sqlbase/sqlerrors_sqlite.go +++ /dev/null @@ -1,53 +0,0 @@ -//go:build kvdb_sqlite && !(windows && (arm || 386)) && !(linux && (ppc64 || mips || mipsle || mips64)) - -package sqlbase - -import ( - "errors" - "fmt" - "strings" - - "modernc.org/sqlite" - sqlite3 "modernc.org/sqlite/lib" -) - -// parseSqliteError attempts to parse a sqlite error as a database agnostic -// SQL error. -func parseSqliteError(err error) error { - if err == nil { - return nil - } - - // If the error isn't wrapped properly, the errors.As call with fail, - // so we'll also try to check the expected error message directly. - // This is taken from: - // https://gitlab.com/cznic/sqlite/-/blob/v1.25.0/sqlite.go#L75. - const sqliteErrMsg = "SQLITE_BUSY" - if strings.Contains(err.Error(), sqliteErrMsg) { - return &ErrSerializationError{ - DBError: err, - } - } - - var sqliteErr *sqlite.Error - if !errors.As(err, &sqliteErr) { - return nil - } - - switch sqliteErr.Code() { - // Handle unique constraint violation error. - case sqlite3.SQLITE_CONSTRAINT_UNIQUE: - return &ErrSQLUniqueConstraintViolation{ - DBError: sqliteErr, - } - - // Database is currently busy, so we'll need to try again. - case sqlite3.SQLITE_BUSY: - return &ErrSerializationError{ - DBError: sqliteErr, - } - - default: - return fmt.Errorf("unknown sqlite error: %w", sqliteErr) - } -}