From 1cf3b4796d5a2e6bd1b1939d4c6501771b2705a0 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Tue, 17 Nov 2026 18:44:46 +0000 Subject: [PATCH] db: translate driver errors to gorm sentinels Unique and foreign-key violations reached callers as raw SQLite or Postgres text, so no layer could tell a taken name from a server fault. --- hscontrol/db/db.go | 5 +++- hscontrol/db/db_test.go | 9 ++------ hscontrol/db/preauth_keys_test.go | 2 +- hscontrol/db/users_test.go | 38 +++++++++++++++++++++++++++++-- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/hscontrol/db/db.go b/hscontrol/db/db.go index b7c4bd3c7..cd8adadac 100644 --- a/hscontrol/db/db.go +++ b/hscontrol/db/db.go @@ -545,6 +545,8 @@ func openDB(cfg types.DatabaseConfig, sqlitePath string) (*gorm.DB, error) { sqlite.Open(connectionURL), &gorm.Config{ Logger: dbLogger, + // Callers match gorm.ErrDuplicatedKey, not driver text. + TranslateError: true, }, ) if err != nil { @@ -583,7 +585,8 @@ func openDB(cfg types.DatabaseConfig, sqlitePath string) (*gorm.DB, error) { pg.Pass = types.Secret(pass) db, err := gorm.Open(postgres.Open(pg.DSN()), &gorm.Config{ - Logger: dbLogger, + Logger: dbLogger, + TranslateError: true, }) if err != nil { return nil, err diff --git a/hscontrol/db/db_test.go b/hscontrol/db/db_test.go index 05bb96a16..4891df1aa 100644 --- a/hscontrol/db/db_test.go +++ b/hscontrol/db/db_test.go @@ -321,15 +321,10 @@ func createSQLiteFromSQLFile(sqlFilePath, dbPath string) error { return err } -// requireConstraintFailed checks if the error is a constraint failure with -// either SQLite and PostgreSQL error messages. +// requireConstraintFailed checks that a unique index refused the write. func requireConstraintFailed(t *testing.T, err error) { t.Helper() - require.Error(t, err) - - if !strings.Contains(err.Error(), "UNIQUE constraint failed:") && !strings.Contains(err.Error(), "violates unique constraint") { - require.Failf(t, "expected error to contain a constraint failure, got: %s", err.Error()) - } + require.ErrorIs(t, err, gorm.ErrDuplicatedKey) } func TestConstraints(t *testing.T) { diff --git a/hscontrol/db/preauth_keys_test.go b/hscontrol/db/preauth_keys_test.go index 3aa51f70f..180ad1990 100644 --- a/hscontrol/db/preauth_keys_test.go +++ b/hscontrol/db/preauth_keys_test.go @@ -207,7 +207,7 @@ func TestCannotDeleteAssignedPreAuthKey(t *testing.T) { err = db.DB.Where("kind = ? AND id = ?", types.CredentialPreAuthKey, key.ID). Delete(&types.Credential{}).Error - require.ErrorContains(t, err, "constraint failed: FOREIGN KEY constraint failed") + require.ErrorIs(t, err, gorm.ErrForeignKeyViolated) } func TestPreAuthKeyAuthentication(t *testing.T) { diff --git a/hscontrol/db/users_test.go b/hscontrol/db/users_test.go index ab7379250..81ccef771 100644 --- a/hscontrol/db/users_test.go +++ b/hscontrol/db/users_test.go @@ -331,6 +331,41 @@ func TestGetUserErrorPropagation(t *testing.T) { } } +// TestDuplicateUserNameIsTyped pins the root cause: a unique-index violation +// must reach callers as a typed error on every backend, not as driver text. +func TestDuplicateUserNameIsTyped(t *testing.T) { + backends := map[string]func(t *testing.T) *HSDatabase{ + "sqlite": func(t *testing.T) *HSDatabase { + t.Helper() + + db, err := newSQLiteTestDB() + require.NoError(t, err) + + return db + }, + "postgres": newPostgresTestDB, + } + + for name, open := range backends { + t.Run(name+"/create", func(t *testing.T) { + db := open(t) + db.CreateUserForTest("dup") + + _, err := db.CreateUser(types.User{Name: "dup"}) + require.ErrorIs(t, err, gorm.ErrDuplicatedKey) + }) + + t.Run(name+"/rename", func(t *testing.T) { + db := open(t) + db.CreateUserForTest("dup") + other := db.CreateUserForTest("other") + + err := db.RenameUser(types.UserID(other.ID), "dup") + require.ErrorIs(t, err, gorm.ErrDuplicatedKey) + }) + } +} + func TestRenameUser(t *testing.T) { tests := []struct { name string @@ -381,8 +416,7 @@ func TestRenameUser(t *testing.T) { assert.Equal(t, "test2", userTest2.Name) err := db.RenameUser(types.UserID(userTest2.ID), "test") - require.Error(t, err) - assert.Contains(t, err.Error(), "UNIQUE constraint failed") + require.ErrorIs(t, err, gorm.ErrDuplicatedKey) }, }, }