From 7d310514875fb90cfb0c54281588ede0aaede214 Mon Sep 17 00:00:00 2001 From: Till Faelligen Date: Mon, 1 Jun 2020 17:22:18 +0200 Subject: [PATCH] Also check sqlite for constraint error --- internal/postgres.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/internal/postgres.go b/internal/postgres.go index 7ae40d8fd..de29da49f 100644 --- a/internal/postgres.go +++ b/internal/postgres.go @@ -16,10 +16,22 @@ package internal -import "github.com/lib/pq" +import ( + "errors" -// IsUniqueConstraintViolationErr returns true if the error is a postgresql unique_violation error + "github.com/lib/pq" + "github.com/mattn/go-sqlite3" +) + +// IsUniqueConstraintViolationErr returns true if the error is a postgresql unique_violation or sqlite3.ErrConstraint func IsUniqueConstraintViolationErr(err error) bool { pqErr, ok := err.(*pq.Error) - return ok && pqErr.Code == "23505" + if ok { + return pqErr.Code == "23505" + } + sqliteErr, ok := err.(*sqlite3.Error) + if ok { + return errors.Is(sqliteErr, sqlite3.ErrConstraint) + } + return false }