1 parent
bedcf4dd83
commit
d2a9033695
23 files changed
+1544
-6
No files matched your search
@@ -0,0 +1,31 @@
|
||||
// Package dberror 按驱动类型识别数据库错误,不执行 SQL 或决定业务幂等结果。
|
||||
package dberror
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const mysqlDuplicateEntryNumber = 1062
|
||||
|
||||
// IsDuplicateKey 按驱动错误类型识别唯一键冲突,不读取错误文案或约束名称。
|
||||
//
|
||||
// 入参:
|
||||
// - err:数据库执行错误,可包含项目场景包装或多层原因链。
|
||||
//
|
||||
// 返回:
|
||||
// - 是否重复:支持 MySQL、SQLite 及 GORM 已翻译的重复键错误;其他约束、普通文本和 nil 返回 false。
|
||||
func IsDuplicateKey(err error) bool {
|
||||
if errors.Is(err, gorm.ErrDuplicatedKey) {
|
||||
return true
|
||||
}
|
||||
var (
|
||||
mysqlError *mysql.MySQLError
|
||||
)
|
||||
if errors.As(err, &mysqlError) && mysqlError != nil {
|
||||
return mysqlError.Number == mysqlDuplicateEntryNumber
|
||||
}
|
||||
return isSQLiteDuplicateKey(err)
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
//go:build cgo
|
||||
|
||||
package dberror
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
// isSQLiteDuplicateKey 在启用CGO时识别SQLite驱动原因链;err为执行错误,返回是否唯一键或主键冲突。
|
||||
func isSQLiteDuplicateKey(err error) bool {
|
||||
var (
|
||||
sqliteError sqlite3.Error
|
||||
)
|
||||
return errors.As(err, &sqliteError) && (sqliteError.ExtendedCode == sqlite3.ErrConstraintUnique || sqliteError.ExtendedCode == sqlite3.ErrConstraintPrimaryKey)
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
//go:build cgo
|
||||
|
||||
package dberror
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
// TestIsDuplicateKeySQLite 验证CGO驱动约束类型,保留主键/唯一键幂等语义,不吞掉其他约束错误。
|
||||
func TestIsDuplicateKeySQLite(t *testing.T) {
|
||||
for _, item := range []struct {
|
||||
name string
|
||||
err error
|
||||
duplicate bool
|
||||
}{
|
||||
{name: "unique", err: sqlite3.Error{Code: sqlite3.ErrConstraint, ExtendedCode: sqlite3.ErrConstraintUnique}, duplicate: true},
|
||||
{name: "wrapped primary", err: fmt.Errorf("write event: %w", sqlite3.Error{Code: sqlite3.ErrConstraint, ExtendedCode: sqlite3.ErrConstraintPrimaryKey}), duplicate: true},
|
||||
{name: "foreign key", err: sqlite3.Error{Code: sqlite3.ErrConstraint, ExtendedCode: sqlite3.ErrConstraintForeignKey}},
|
||||
{name: "not null", err: sqlite3.Error{Code: sqlite3.ErrConstraint, ExtendedCode: sqlite3.ErrConstraintNotNull}},
|
||||
} {
|
||||
t.Run(item.name, func(t *testing.T) {
|
||||
if got := IsDuplicateKey(item.err); got != item.duplicate {
|
||||
t.Fatalf("duplicate=%v want=%v", got, item.duplicate)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
//go:build !cgo
|
||||
|
||||
package dberror
|
||||
|
||||
// isSQLiteDuplicateKey 接收执行错误err;非CGO的sqlite驱动只有不可用桩,不会产生SQLite约束错误,返回false。
|
||||
func isSQLiteDuplicateKey(err error) bool {
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package dberror
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// TestIsDuplicateKeyUsesTypedCauses 验证包装不影响驱动识别,伪造文本和其他约束不进入幂等成功。
|
||||
func TestIsDuplicateKeyUsesTypedCauses(t *testing.T) {
|
||||
for _, item := range []struct {
|
||||
name string
|
||||
err error
|
||||
duplicate bool
|
||||
}{
|
||||
{name: "nil"},
|
||||
{name: "mysql", err: &mysql.MySQLError{Number: 1062, Message: "localized and renamed index"}, duplicate: true},
|
||||
{name: "wrapped mysql", err: fmt.Errorf("write event: %w", &mysql.MySQLError{Number: 1062}), duplicate: true},
|
||||
{name: "gorm translated", err: fmt.Errorf("write event: %w", gorm.ErrDuplicatedKey), duplicate: true},
|
||||
{name: "plain text", err: errors.New("1062 Duplicate entry uk_active_user")},
|
||||
{name: "wrong mysql number", err: &mysql.MySQLError{Number: 1054, Message: "1062 Duplicate entry"}},
|
||||
{name: "sqlite plain text", err: errors.New("UNIQUE constraint failed: table.id")},
|
||||
} {
|
||||
t.Run(item.name, func(t *testing.T) {
|
||||
if got := IsDuplicateKey(item.err); got != item.duplicate {
|
||||
t.Fatalf("IsDuplicateKey(%T)=%v want=%v", item.err, got, item.duplicate)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package dberror
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func TestDuplicateKeyAgainstMySQL(t *testing.T) {
|
||||
dsn := os.Getenv("GINFRAMEWORK_TEST_MYSQL_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("isolated MySQL DSN is not provided")
|
||||
}
|
||||
cfg, err := mysql.ParseDSN(dsn)
|
||||
if err != nil || !strings.HasPrefix(cfg.DBName, "ginframework_test_") {
|
||||
t.Fatal("isolated ginframework_test_ database is required")
|
||||
}
|
||||
db, err := sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
t.Fatal("open isolated MySQL failed")
|
||||
}
|
||||
defer db.Close()
|
||||
conn, err := db.Conn(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
if _, err = conn.ExecContext(context.Background(), "CREATE TEMPORARY TABLE duplicate_probe (id BIGINT PRIMARY KEY, value BIGINT NOT NULL)"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = conn.ExecContext(context.Background(), "INSERT INTO duplicate_probe (id,value) VALUES (?,?)", 1, 1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = conn.ExecContext(context.Background(), "INSERT INTO duplicate_probe (id,value) VALUES (?,?)", 1, 2)
|
||||
var (
|
||||
driverError *mysql.MySQLError
|
||||
)
|
||||
if !errors.As(err, &driverError) || driverError.Number != 1062 || !IsDuplicateKey(fmt.Errorf("insert probe: %w", err)) {
|
||||
t.Fatalf("duplicate cause lost: %T", err)
|
||||
}
|
||||
_, err = conn.ExecContext(context.Background(), "INSERT INTO duplicate_probe (id,value) VALUES (?,NULL)", 2)
|
||||
if err == nil || IsDuplicateKey(err) {
|
||||
t.Fatal("non-null violation classified as duplicate")
|
||||
}
|
||||
}
|
||||