From f0eac5798a6974c61fa8bbd804a9a40bc09a555a Mon Sep 17 00:00:00 2001 From: SirLouen Date: Mon, 16 Nov 2026 22:30:19 +0100 Subject: [PATCH 1/2] feat(dbkit/sqlite): let callers match a change that a RESTRICT foreign key stops --- dbkit/sqlite/CHANGELOG.md | 2 ++ dbkit/sqlite/classify.go | 5 ++++- dbkit/sqlite/classify_test.go | 11 ++++++----- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/dbkit/sqlite/CHANGELOG.md b/dbkit/sqlite/CHANGELOG.md index 4c41fba..418762d 100644 --- a/dbkit/sqlite/CHANGELOG.md +++ b/dbkit/sqlite/CHANGELOG.md @@ -18,10 +18,12 @@ Releases of this module are tagged `dbkit/sqlite/vX.Y.Z`. - `ErrSnapshotExists`, which a snapshot error matches when a file already holds the target. - `ErrSnapshotUnsynced`, which a snapshot error matches when the copy is in place but its folder failed to sync. - `SnapshotOptions.Mode`, which narrows the permission bits of each copy below those of the live file and must keep the owner's read and write. +- `ErrRestrict`, the class `Classify` gives a delete or update that a RESTRICT foreign key stops. It also matches `dbkit.ErrForeignKey`. ### Changed - **Breaking.** Requires `modernc.org/sqlite` v1.62.0 and `modernc.org/libc` v1.80.1, and `LibcVersion` is v1.80.1. +- The error of a delete or update that a RESTRICT foreign key stops reads "dbkit: a foreign key constraint failed: another row still refers to the row: ", then the driver's message. - **Breaking.** `NewSnapshotter` takes `SnapshotOptions`. Pass `sqlite.SnapshotOptions{}` for no margin and the live file's mode. - **Breaking.** A snapshot fails with `ErrNoSpace` before it writes the copy when the target folder has less free space than the used pages of the database plus `SnapshotOptions.Margin`. - **Breaking.** `Snapshot` refuses a target folder on a network file system, or on Linux a FUSE one, before it takes the lock, so a missing folder now fails at that read unless the snapshot runs as root. diff --git a/dbkit/sqlite/classify.go b/dbkit/sqlite/classify.go index 67accad..2dd15da 100644 --- a/dbkit/sqlite/classify.go +++ b/dbkit/sqlite/classify.go @@ -19,6 +19,9 @@ const LibcVersion = "v1.80.1" // restrictMessage is the text SQLite gives a foreign key a RESTRICT action stops. const restrictMessage = "FOREIGN KEY constraint failed" +// ErrRestrict is the class of a delete or update a RESTRICT foreign key stops, and it matches dbkit.ErrForeignKey. +var ErrRestrict = fmt.Errorf("%w: another row still refers to the row", dbkit.ErrForeignKey) + // constraintClasses maps each extended constraint result code that has a class to that class. var constraintClasses = map[int]error{ sqlite3.SQLITE_CONSTRAINT_UNIQUE: dbkit.ErrUnique, @@ -49,7 +52,7 @@ func classOf(driverErr *modernc.Error) error { return dbkit.ErrBusy } if code == sqlite3.SQLITE_CONSTRAINT_TRIGGER && strings.Contains(driverErr.Error(), restrictMessage) { - return dbkit.ErrForeignKey + return ErrRestrict } return constraintClasses[code] } diff --git a/dbkit/sqlite/classify_test.go b/dbkit/sqlite/classify_test.go index c77a219..bebd090 100644 --- a/dbkit/sqlite/classify_test.go +++ b/dbkit/sqlite/classify_test.go @@ -37,8 +37,9 @@ INSERT INTO classify_keepers VALUES (1); INSERT INTO classify_plain (rowid, a) VALUES (1, 1); ` -// classifyClasses lists every dbkit error class. -var classifyClasses = []error{dbkit.ErrBusy, dbkit.ErrUnique, dbkit.ErrForeignKey, dbkit.ErrNotNull, dbkit.ErrCheck} +// classifyClasses lists every dbkit error class and the restrict class of this package. +var classifyClasses = []error{dbkit.ErrBusy, dbkit.ErrUnique, dbkit.ErrForeignKey, dbkit.ErrNotNull, dbkit.ErrCheck, + sqlite.ErrRestrict} // classifyOpen returns a handle on a fresh file holding classifySchema, with a short busy timeout. func classifyOpen(t *testing.T) *sql.DB { @@ -127,9 +128,9 @@ func TestClassify(t *testing.T) { {"unique", classifyExec("INSERT INTO classify_rows (id, code) VALUES (2, 'taken')"), 2067, dbkit.ErrUnique}, {"primary key", classifyExec("INSERT INTO classify_rows (id, code) VALUES (1, 'free')"), 1555, dbkit.ErrUnique}, {"a repeated rowid", classifyExec("INSERT INTO classify_plain (rowid, a) VALUES (1, 2)"), 2579, dbkit.ErrUnique}, - {"a restricted delete", classifyExec("DELETE FROM classify_kept WHERE id = 1"), 1811, dbkit.ErrForeignKey}, + {"a restricted delete", classifyExec("DELETE FROM classify_kept WHERE id = 1"), 1811, sqlite.ErrRestrict}, {"a restricted update", classifyExec("UPDATE classify_kept SET id = 2 WHERE id = 1"), 1811, - dbkit.ErrForeignKey}, + sqlite.ErrRestrict}, {"foreign key", classifyExec("INSERT INTO classify_rows VALUES (3, 'orphan', 99, 0)"), 787, dbkit.ErrForeignKey}, {"not null", classifyExec("INSERT INTO classify_rows (id, code) VALUES (4, NULL)"), 1299, dbkit.ErrNotNull}, @@ -147,7 +148,7 @@ func TestClassify(t *testing.T) { classifyMustCode(t, got, c.code) for _, class := range classifyClasses { - if want := class == c.class; errors.Is(got, class) != want { + if want := errors.Is(c.class, class); errors.Is(got, class) != want { t.Errorf("errors.Is(Classify(%v), %v) = %v, want %v", err, class, !want, want) } } From b87992f4ba89adc8e15c847c9ada608cecf472c5 Mon Sep 17 00:00:00 2001 From: SirLouen Date: Mon, 16 Nov 2026 22:43:06 +0100 Subject: [PATCH 2/2] fix(dbkit/sqlite): match a restrict stop by the driver's whole message --- dbkit/sqlite/CHANGELOG.md | 1 + dbkit/sqlite/classify.go | 9 ++++----- dbkit/sqlite/classify_test.go | 9 +++++++++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/dbkit/sqlite/CHANGELOG.md b/dbkit/sqlite/CHANGELOG.md index 418762d..d53a526 100644 --- a/dbkit/sqlite/CHANGELOG.md +++ b/dbkit/sqlite/CHANGELOG.md @@ -24,6 +24,7 @@ Releases of this module are tagged `dbkit/sqlite/vX.Y.Z`. - **Breaking.** Requires `modernc.org/sqlite` v1.62.0 and `modernc.org/libc` v1.80.1, and `LibcVersion` is v1.80.1. - The error of a delete or update that a RESTRICT foreign key stops reads "dbkit: a foreign key constraint failed: another row still refers to the row: ", then the driver's message. +- A trigger abort whose message holds more than "FOREIGN KEY constraint failed" no longer matches `dbkit.ErrForeignKey`. - **Breaking.** `NewSnapshotter` takes `SnapshotOptions`. Pass `sqlite.SnapshotOptions{}` for no margin and the live file's mode. - **Breaking.** A snapshot fails with `ErrNoSpace` before it writes the copy when the target folder has less free space than the used pages of the database plus `SnapshotOptions.Margin`. - **Breaking.** `Snapshot` refuses a target folder on a network file system, or on Linux a FUSE one, before it takes the lock, so a missing folder now fails at that read unless the snapshot runs as root. diff --git a/dbkit/sqlite/classify.go b/dbkit/sqlite/classify.go index 2dd15da..12e3992 100644 --- a/dbkit/sqlite/classify.go +++ b/dbkit/sqlite/classify.go @@ -5,7 +5,6 @@ package sqlite import ( "errors" "fmt" - "strings" modernc "modernc.org/sqlite" sqlite3 "modernc.org/sqlite/lib" @@ -16,10 +15,10 @@ import ( // LibcVersion is the modernc.org/libc version the driver is pinned with. const LibcVersion = "v1.80.1" -// restrictMessage is the text SQLite gives a foreign key a RESTRICT action stops. -const restrictMessage = "FOREIGN KEY constraint failed" +// restrictMessage is the driver's whole text of a change a RESTRICT foreign key stops. +const restrictMessage = "constraint failed: FOREIGN KEY constraint failed (1811)" -// ErrRestrict is the class of a delete or update a RESTRICT foreign key stops, and it matches dbkit.ErrForeignKey. +// ErrRestrict matches dbkit.ErrForeignKey and classes a change a RESTRICT key, or a trigger in its words, stops. var ErrRestrict = fmt.Errorf("%w: another row still refers to the row", dbkit.ErrForeignKey) // constraintClasses maps each extended constraint result code that has a class to that class. @@ -51,7 +50,7 @@ func classOf(driverErr *modernc.Error) error { if primary := code & 0xff; primary == sqlite3.SQLITE_BUSY || primary == sqlite3.SQLITE_LOCKED { return dbkit.ErrBusy } - if code == sqlite3.SQLITE_CONSTRAINT_TRIGGER && strings.Contains(driverErr.Error(), restrictMessage) { + if code == sqlite3.SQLITE_CONSTRAINT_TRIGGER && driverErr.Error() == restrictMessage { return ErrRestrict } return constraintClasses[code] diff --git a/dbkit/sqlite/classify_test.go b/dbkit/sqlite/classify_test.go index bebd090..c0bdae6 100644 --- a/dbkit/sqlite/classify_test.go +++ b/dbkit/sqlite/classify_test.go @@ -29,6 +29,12 @@ CREATE TABLE classify_scanned (a INTEGER); CREATE TABLE classify_kept (id INTEGER PRIMARY KEY); CREATE TABLE classify_keepers (kept INTEGER REFERENCES classify_kept (id) ON DELETE RESTRICT ON UPDATE RESTRICT); CREATE TABLE classify_plain (a INTEGER); +CREATE TABLE classify_worded (a INTEGER); +CREATE TRIGGER classify_word BEFORE INSERT ON classify_worded +BEGIN SELECT RAISE(ABORT, 'FOREIGN KEY constraint failed in the app'); END; +CREATE TABLE classify_mimic (a INTEGER); +CREATE TRIGGER classify_mime BEFORE INSERT ON classify_mimic +BEGIN SELECT RAISE(ABORT, 'FOREIGN KEY constraint failed'); END; INSERT INTO classify_parents VALUES (1); INSERT INTO classify_rows VALUES (1, 'taken', 1, 0); INSERT INTO classify_scanned VALUES (1), (2); @@ -136,6 +142,9 @@ func TestClassify(t *testing.T) { {"not null", classifyExec("INSERT INTO classify_rows (id, code) VALUES (4, NULL)"), 1299, dbkit.ErrNotNull}, {"check", classifyExec("INSERT INTO classify_rows VALUES (5, 'negative', 1, -1)"), 275, dbkit.ErrCheck}, {"a trigger abort", classifyExec("INSERT INTO classify_guarded VALUES (1)"), 1811, nil}, + {"a trigger abort that names a foreign key", classifyExec("INSERT INTO classify_worded VALUES (1)"), 1811, nil}, + {"a trigger abort in a restrict's own words", classifyExec("INSERT INTO classify_mimic VALUES (1)"), 1811, + sqlite.ErrRestrict}, {"a syntax error", classifyExec("SELEC 1"), 1, nil}, } for _, c := range cases {