Chore: Make cgo optional (for sqlite) (#108756)

* make cgo optional for sqlite

* update go.mod; check error code differently

* reduce api surface even more

* move test errors into sqlite package

* add a comment
This commit is contained in:
Serge Zaitsev
2025-07-31 09:25:19 +00:00
committed by GitHub
parent f39b878b0a
commit 6b1143565a
15 changed files with 160 additions and 41 deletions
+46
View File
@@ -0,0 +1,46 @@
//go:build cgo
package sqlite
import (
"errors"
"github.com/mattn/go-sqlite3"
)
type Driver = sqlite3.SQLiteDriver
// The errors below are used in tests to simulate specific SQLite errors. It's a temporary solution
// until we rewrite the tests not to depend on the sqlite3 package internals directly.
var (
TestErrUniqueConstraintViolation = sqlite3.Error{Code: sqlite3.ErrConstraint, ExtendedCode: sqlite3.ErrConstraintUnique}
TestErrBusy = sqlite3.Error{Code: sqlite3.ErrBusy}
TestErrLocked = sqlite3.Error{Code: sqlite3.ErrLocked}
)
func IsBusyOrLocked(err error) bool {
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) {
return sqliteErr.Code == sqlite3.ErrLocked || sqliteErr.Code == sqlite3.ErrBusy
}
return false
}
func IsUniqueConstraintViolation(err error) bool {
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) {
return sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique || sqliteErr.ExtendedCode == sqlite3.ErrConstraintPrimaryKey
}
return false
}
func ErrorMessage(err error) string {
if err == nil {
return ""
}
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) {
return sqliteErr.Error()
}
return err.Error()
}