This commit is contained in:
adlerhurst
2025-07-17 09:08:33 +02:00
parent 821b293025
commit 15d07c076f
4 changed files with 60 additions and 59 deletions

View File

@@ -5,6 +5,7 @@ import (
"github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn" "github.com/jackc/pgx/v5/pgconn"
"github.com/zitadel/zitadel/backend/v3/storage/database" "github.com/zitadel/zitadel/backend/v3/storage/database"
) )

View File

@@ -4,55 +4,55 @@ import (
"fmt" "fmt"
) )
// ErrNoRowFound is returned when QueryRow does not find any row. // NoRowFoundError is returned when QueryRow does not find any row.
// It wraps the dialect specific original error to provide more context. // It wraps the dialect specific original error to provide more context.
type ErrNoRowFound struct { type NoRowFoundError struct {
original error original error
} }
func NewNoRowFoundError(original error) error { func NewNoRowFoundError(original error) error {
return &ErrNoRowFound{ return &NoRowFoundError{
original: original, original: original,
} }
} }
func (e *ErrNoRowFound) Error() string { func (e *NoRowFoundError) Error() string {
return "no row found" return "no row found"
} }
func (e *ErrNoRowFound) Is(target error) bool { func (e *NoRowFoundError) Is(target error) bool {
_, ok := target.(*ErrNoRowFound) _, ok := target.(*NoRowFoundError)
return ok return ok
} }
func (e *ErrNoRowFound) Unwrap() error { func (e *NoRowFoundError) Unwrap() error {
return e.original return e.original
} }
// ErrMultipleRowsFound is returned when QueryRow finds multiple rows. // MultipleRowsFoundError is returned when QueryRow finds multiple rows.
// It wraps the dialect specific original error to provide more context. // It wraps the dialect specific original error to provide more context.
type ErrMultipleRowsFound struct { type MultipleRowsFoundError struct {
original error original error
count int count int
} }
func NewMultipleRowsFoundError(original error, count int) error { func NewMultipleRowsFoundError(original error, count int) error {
return &ErrMultipleRowsFound{ return &MultipleRowsFoundError{
original: original, original: original,
count: count, count: count,
} }
} }
func (e *ErrMultipleRowsFound) Error() string { func (e *MultipleRowsFoundError) Error() string {
return fmt.Sprintf("multiple rows found: %d", e.count) return fmt.Sprintf("multiple rows found: %d", e.count)
} }
func (e *ErrMultipleRowsFound) Is(target error) bool { func (e *MultipleRowsFoundError) Is(target error) bool {
_, ok := target.(*ErrMultipleRowsFound) _, ok := target.(*MultipleRowsFoundError)
return ok return ok
} }
func (e *ErrMultipleRowsFound) Unwrap() error { func (e *MultipleRowsFoundError) Unwrap() error {
return e.original return e.original
} }
@@ -93,15 +93,15 @@ func (e *IntegrityViolation) Is(target error) bool {
return ok return ok
} }
// CheckErr is returned when a check constraint fails. // CheckError is returned when a check constraint fails.
// It wraps the [IntegrityViolation] to provide more context. // It wraps the [IntegrityViolation] to provide more context.
// It is used to indicate that a check constraint was violated during an insert or update operation. // It is used to indicate that a check constraint was violated during an insert or update operation.
type CheckErr struct { type CheckError struct {
IntegrityViolation IntegrityViolation
} }
func NewCheckError(table, constraint string, original error) error { func NewCheckError(table, constraint string, original error) error {
return &CheckErr{ return &CheckError{
IntegrityViolation: IntegrityViolation{ IntegrityViolation: IntegrityViolation{
integrityType: IntegrityTypeCheck, integrityType: IntegrityTypeCheck,
table: table, table: table,
@@ -111,24 +111,24 @@ func NewCheckError(table, constraint string, original error) error {
} }
} }
func (e *CheckErr) Is(target error) bool { func (e *CheckError) Is(target error) bool {
_, ok := target.(*CheckErr) _, ok := target.(*CheckError)
return ok return ok
} }
func (e *CheckErr) Unwrap() error { func (e *CheckError) Unwrap() error {
return &e.IntegrityViolation return &e.IntegrityViolation
} }
// UniqueErr is returned when a unique constraint fails. // UniqueError is returned when a unique constraint fails.
// It wraps the [IntegrityViolation] to provide more context. // It wraps the [IntegrityViolation] to provide more context.
// It is used to indicate that a unique constraint was violated during an insert or update operation. // It is used to indicate that a unique constraint was violated during an insert or update operation.
type UniqueErr struct { type UniqueError struct {
IntegrityViolation IntegrityViolation
} }
func NewUniqueError(table, constraint string, original error) error { func NewUniqueError(table, constraint string, original error) error {
return &UniqueErr{ return &UniqueError{
IntegrityViolation: IntegrityViolation{ IntegrityViolation: IntegrityViolation{
integrityType: IntegrityTypeUnique, integrityType: IntegrityTypeUnique,
table: table, table: table,
@@ -138,24 +138,24 @@ func NewUniqueError(table, constraint string, original error) error {
} }
} }
func (e *UniqueErr) Is(target error) bool { func (e *UniqueError) Is(target error) bool {
_, ok := target.(*UniqueErr) _, ok := target.(*UniqueError)
return ok return ok
} }
func (e *UniqueErr) Unwrap() error { func (e *UniqueError) Unwrap() error {
return &e.IntegrityViolation return &e.IntegrityViolation
} }
// ForeignKeyErr is returned when a foreign key constraint fails. // ForeignKeyError is returned when a foreign key constraint fails.
// It wraps the [IntegrityViolation] to provide more context. // It wraps the [IntegrityViolation] to provide more context.
// It is used to indicate that a foreign key constraint was violated during an insert or update operation // It is used to indicate that a foreign key constraint was violated during an insert or update operation
type ForeignKeyErr struct { type ForeignKeyError struct {
IntegrityViolation IntegrityViolation
} }
func NewForeignKeyError(table, constraint string, original error) error { func NewForeignKeyError(table, constraint string, original error) error {
return &ForeignKeyErr{ return &ForeignKeyError{
IntegrityViolation: IntegrityViolation{ IntegrityViolation: IntegrityViolation{
integrityType: IntegrityTypeForeign, integrityType: IntegrityTypeForeign,
table: table, table: table,
@@ -165,24 +165,24 @@ func NewForeignKeyError(table, constraint string, original error) error {
} }
} }
func (e *ForeignKeyErr) Is(target error) bool { func (e *ForeignKeyError) Is(target error) bool {
_, ok := target.(*ForeignKeyErr) _, ok := target.(*ForeignKeyError)
return ok return ok
} }
func (e *ForeignKeyErr) Unwrap() error { func (e *ForeignKeyError) Unwrap() error {
return &e.IntegrityViolation return &e.IntegrityViolation
} }
// NotNullErr is returned when a not null constraint fails. // NotNullError is returned when a not null constraint fails.
// It wraps the [IntegrityViolation] to provide more context. // It wraps the [IntegrityViolation] to provide more context.
// It is used to indicate that a not null constraint was violated during an insert or update operation. // It is used to indicate that a not null constraint was violated during an insert or update operation.
type NotNullErr struct { type NotNullError struct {
IntegrityViolation IntegrityViolation
} }
func NewNotNullError(table, constraint string, original error) error { func NewNotNullError(table, constraint string, original error) error {
return &NotNullErr{ return &NotNullError{
IntegrityViolation: IntegrityViolation{ IntegrityViolation: IntegrityViolation{
integrityType: IntegrityTypeNotNull, integrityType: IntegrityTypeNotNull,
table: table, table: table,
@@ -192,37 +192,37 @@ func NewNotNullError(table, constraint string, original error) error {
} }
} }
func (e *NotNullErr) Is(target error) bool { func (e *NotNullError) Is(target error) bool {
_, ok := target.(*NotNullErr) _, ok := target.(*NotNullError)
return ok return ok
} }
func (e *NotNullErr) Unwrap() error { func (e *NotNullError) Unwrap() error {
return &e.IntegrityViolation return &e.IntegrityViolation
} }
// UnknownErr is returned when an unknown error occurs. // UnknownError is returned when an unknown error occurs.
// It wraps the dialect specific original error to provide more context. // It wraps the dialect specific original error to provide more context.
// It is used to indicate that an error occurred that does not fit into any of the other categories. // It is used to indicate that an error occurred that does not fit into any of the other categories.
type UnknownErr struct { type UnknownError struct {
original error original error
} }
func NewUnknownError(original error) error { func NewUnknownError(original error) error {
return &UnknownErr{ return &UnknownError{
original: original, original: original,
} }
} }
func (e *UnknownErr) Error() string { func (e *UnknownError) Error() string {
return fmt.Sprintf("unknown database error: %v", e.original) return fmt.Sprintf("unknown database error: %v", e.original)
} }
func (e *UnknownErr) Is(target error) bool { func (e *UnknownError) Is(target error) bool {
_, ok := target.(*UnknownErr) _, ok := target.(*UnknownError)
return ok return ok
} }
func (e *UnknownErr) Unwrap() error { func (e *UnknownError) Unwrap() error {
return e.original return e.original
} }

View File

@@ -54,7 +54,7 @@ func TestCreateInstance(t *testing.T) {
} }
return instance return instance
}(), }(),
err: new(database.CheckErr), err: new(database.CheckError),
}, },
{ {
name: "adding same instance twice", name: "adding same instance twice",
@@ -79,7 +79,7 @@ func TestCreateInstance(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
return &inst return &inst
}, },
err: new(database.UniqueErr), err: new(database.UniqueError),
}, },
func() struct { func() struct {
name string name string
@@ -145,7 +145,7 @@ func TestCreateInstance(t *testing.T) {
} }
return instance return instance
}(), }(),
err: new(database.CheckErr), err: new(database.CheckError),
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@@ -262,7 +262,7 @@ func TestUpdateInstance(t *testing.T) {
return &inst return &inst
}, },
rowsAffected: 0, rowsAffected: 0,
getErr: new(database.ErrNoRowFound), getErr: new(database.NoRowFoundError),
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@@ -341,7 +341,7 @@ func TestGetInstance(t *testing.T) {
} }
return &inst return &inst
}, },
err: new(database.ErrNoRowFound), err: new(database.NoRowFoundError),
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@@ -654,7 +654,7 @@ func TestDeleteInstance(t *testing.T) {
instance, err := instanceRepo.Get(ctx, instance, err := instanceRepo.Get(ctx,
tt.instanceID, tt.instanceID,
) )
require.ErrorIs(t, err, new(database.ErrNoRowFound)) require.ErrorIs(t, err, new(database.NoRowFoundError))
assert.Nil(t, instance) assert.Nil(t, instance)
}) })
} }

View File

@@ -63,7 +63,7 @@ func TestCreateOrganization(t *testing.T) {
} }
return organization return organization
}(), }(),
err: new(database.CheckErr), err: new(database.CheckError),
}, },
{ {
name: "adding org with same id twice", name: "adding org with same id twice",
@@ -85,7 +85,7 @@ func TestCreateOrganization(t *testing.T) {
org.Name = gofakeit.Name() org.Name = gofakeit.Name()
return &org return &org
}, },
err: new(database.UniqueErr), err: new(database.UniqueError),
}, },
{ {
name: "adding org with same name twice", name: "adding org with same name twice",
@@ -107,7 +107,7 @@ func TestCreateOrganization(t *testing.T) {
org.ID = gofakeit.Name() org.ID = gofakeit.Name()
return &org return &org
}, },
err: new(database.UniqueErr), err: new(database.UniqueError),
}, },
func() struct { func() struct {
name string name string
@@ -180,7 +180,7 @@ func TestCreateOrganization(t *testing.T) {
} }
return organization return organization
}(), }(),
err: new(database.CheckErr), err: new(database.CheckError),
}, },
{ {
name: "adding organization with no instance id", name: "adding organization with no instance id",
@@ -194,7 +194,7 @@ func TestCreateOrganization(t *testing.T) {
} }
return organization return organization
}(), }(),
err: new(database.ForeignKeyErr), err: new(database.ForeignKeyError),
}, },
{ {
name: "adding organization with non existent instance id", name: "adding organization with non existent instance id",
@@ -209,7 +209,7 @@ func TestCreateOrganization(t *testing.T) {
} }
return organization return organization
}(), }(),
err: new(database.ForeignKeyErr), err: new(database.ForeignKeyError),
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@@ -496,7 +496,7 @@ func TestGetOrganization(t *testing.T) {
return &org return &org
}, },
orgIdentifierCondition: orgRepo.NameCondition("non-existent-instance-name"), orgIdentifierCondition: orgRepo.NameCondition("non-existent-instance-name"),
err: new(database.ErrNoRowFound), err: new(database.NoRowFoundError),
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@@ -932,7 +932,7 @@ func TestDeleteOrganization(t *testing.T) {
tt.orgIdentifierCondition, tt.orgIdentifierCondition,
instanceId, instanceId,
) )
require.ErrorIs(t, err, new(database.ErrNoRowFound)) require.ErrorIs(t, err, new(database.NoRowFoundError))
assert.Nil(t, organization) assert.Nil(t, organization)
}) })
} }