diff --git a/README.md b/README.md index 7ffa285..3e6980a 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,16 @@ db, err := gorm.Open(sqlite.Open(":memory:?_pragma=foreign_keys(1)"), &gorm.Conf ``` More info: [https://www.sqlite.org/foreignkeys.html](https://www.sqlite.org/foreignkeys.html) +# Upstream parity +This driver tracks [go-gorm/sqlite](https://github.com/go-gorm/sqlite). The last upstream commit merged here is [525c431](https://github.com/go-gorm/sqlite/commit/525c4315a871b2463d75eadac28093149551dba3). Upstream behaviour fixes get ported, so migration and DDL parsing behave the same on both drivers. + +Three differences are deliberate and stay: +- the SQLite backend is pure-Go, so the driver imports `github.com/glebarez/go-sqlite` and `modernc.org/sqlite` rather than `github.com/mattn/go-sqlite3` +- `DriverName` defaults to `sqlite`, not upstream's `sqlite3` +- `Translate` reads the SQLite extended result code off the driver error, rather than marshalling the error to JSON and matching on field names + +Anything that does not depend on the backend goes upstream first and comes back here as a port. First one: [go-gorm/sqlite#247](https://github.com/go-gorm/sqlite/pull/247). + # FAQ ## How is this better than standard GORM SQLite driver? The [standard GORM driver for SQLite](https://github.com/go-gorm/sqlite) has one major drawback: it is based on a [Go-bindings of SQLite C-source](https://github.com/mattn/go-sqlite3) (this is called [cgo](https://go.dev/blog/cgo)). This fact imposes following restrictions on Go developers: diff --git a/go.mod b/go.mod index ea8b0f8..2d39509 100644 --- a/go.mod +++ b/go.mod @@ -3,20 +3,21 @@ module github.com/glebarez/sqlite go 1.18 require ( - github.com/glebarez/go-sqlite v1.21.2 - gorm.io/gorm v1.25.7 - modernc.org/sqlite v1.23.1 + github.com/glebarez/go-sqlite v1.22.0 + gorm.io/gorm v1.31.2 + modernc.org/sqlite v1.28.0 ) require ( github.com/dustin/go-humanize v1.0.1 // indirect - github.com/google/uuid v1.3.0 // indirect + github.com/google/uuid v1.5.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect - github.com/mattn/go-isatty v0.0.17 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect - golang.org/x/sys v0.7.0 // indirect - modernc.org/libc v1.22.5 // indirect - modernc.org/mathutil v1.5.0 // indirect - modernc.org/memory v1.5.0 // indirect + golang.org/x/sys v0.15.0 // indirect + golang.org/x/text v0.20.0 // indirect + modernc.org/libc v1.37.6 // indirect + modernc.org/mathutil v1.6.0 // indirect + modernc.org/memory v1.7.2 // indirect ) diff --git a/go.sum b/go.sum index 8f8a50e..ac45768 100644 --- a/go.sum +++ b/go.sum @@ -1,29 +1,32 @@ github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= -github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo= -github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k= +github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec+ruQ= +github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc= github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= +github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= -github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= -github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= -golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -gorm.io/gorm v1.25.7 h1:VsD6acwRjz2zFxGO50gPO6AkNs7KKnvfzUjHQhZDz/A= -gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= -modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE= -modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY= -modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ= -modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= -modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds= -modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU= -modernc.org/sqlite v1.23.1 h1:nrSBg4aRQQwq59JpvGEQ15tNxoO5pX/kUjcRNwSAGQM= -modernc.org/sqlite v1.23.1/go.mod h1:OrDj17Mggn6MhE+iPbBNf7RGKODDE9NFT0f3EwDzJqk= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug= +golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4= +gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ= +gorm.io/gorm v1.31.2 h1:3o8FXNo9v9S858gil+3LlZA1LkCOzgb4g5BL64FgaCo= +gorm.io/gorm v1.31.2/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs= +modernc.org/libc v1.37.6 h1:orZH3c5wmhIQFTXF+Nt+eeauyd+ZIt2BX6ARe+kD+aw= +modernc.org/libc v1.37.6/go.mod h1:YAXkAZ8ktnkCKaN9sw/UDeUVkGYJ/YquGO4FTi5nmHE= +modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4= +modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo= +modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E= +modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E= +modernc.org/sqlite v1.28.0 h1:Zx+LyDDmXczNnEQdvPuEfcFVA2ZPyaD7UCZDjef3BHQ= +modernc.org/sqlite v1.28.0/go.mod h1:Qxpazz0zH8Z1xCFyi5GSL3FzbtZ3fvbjmywNogldEW0= diff --git a/sqlite.go b/sqlite.go index caef1b9..6b6d67b 100644 --- a/sqlite.go +++ b/sqlite.go @@ -266,6 +266,8 @@ func (dialector Dialector) Translate(err error) error { return gorm.ErrDuplicatedKey case sqlite3.SQLITE_CONSTRAINT_FOREIGNKEY: return gorm.ErrForeignKeyViolated + case sqlite3.SQLITE_CONSTRAINT_CHECK: + return gorm.ErrCheckConstraintViolated } } return err diff --git a/sqlite_error_translator_test.go b/sqlite_error_translator_test.go index b2e3584..690d155 100644 --- a/sqlite_error_translator_test.go +++ b/sqlite_error_translator_test.go @@ -1,6 +1,7 @@ package sqlite import ( + "strings" "testing" "gorm.io/gorm" @@ -46,3 +47,49 @@ func TestErrorTranslator(t *testing.T) { t.Errorf("Expected error from second create to be gorm.ErrDuplicatedKey: %v", err) } } + +func TestErrorTranslatorCheckConstraint(t *testing.T) { + // A DSN of its own, so the shared in-memory cache of the other tests is untouched. + const InMemoryDSN = "file:testdatabase_check?mode=memory&cache=shared" + + // Price carries a CHECK constraint, which SQLite reports as extended code 275. + type Product struct { + Name string + Price int `gorm:"check:price_positive,price > 0"` + } + + db, err := gorm.Open(&Dialector{DSN: InMemoryDSN}, &gorm.Config{ + Logger: logger.Default.LogMode(logger.Silent), + TranslateError: true}) + + if err != nil { + t.Fatalf("Expected Open to succeed; got error: %v", err) + } + + if err := db.AutoMigrate(&Product{}); err != nil { + t.Fatalf("Expected to migrate database models to succeed: %v", err) + } + + // Without this the failing insert below could fail for an unrelated reason and + // the test would still look green. + var ddl string + if err := db.Raw("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'products'").Scan(&ddl).Error; err != nil { + t.Fatalf("Expected to read the table DDL: %v", err) + } + if !strings.Contains(strings.ToUpper(ddl), "CHECK") { + t.Fatalf("Expected the products table to carry a CHECK constraint; got: %s", ddl) + } + + if err := db.Create(&Product{Name: "valid", Price: 10}).Error; err != nil { + t.Errorf("Expected a create satisfying the check to succeed: %v", err) + } + + err = db.Create(&Product{Name: "invalid", Price: -1}).Error + if err == nil { + t.Fatalf("Expected a create violating the check to fail.") + } + + if err != gorm.ErrCheckConstraintViolated { + t.Errorf("Expected error from the violating create to be gorm.ErrCheckConstraintViolated: %v", err) + } +}