add specific error handling for unique constraint vs other errors
This commit is contained in:
parent
ad551edcb0
commit
ace7f7b293
1 changed files with 8 additions and 2 deletions
10
store.go
10
store.go
|
|
@ -2,7 +2,9 @@ package main
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
_ "modernc.org/sqlite"
|
||||
"log"
|
||||
"modernc.org/sqlite"
|
||||
sqlite3 "modernc.org/sqlite/lib"
|
||||
)
|
||||
|
||||
const schema = `
|
||||
|
|
@ -40,7 +42,11 @@ func (s *SQLStore) AcquireLock(chapterID int64, dept string, repID int64) (int64
|
|||
`INSERT INTO runs (chapter_id, department, rep_user_id, status)
|
||||
VALUES (?, ?, ?, 'running')`, chapterID, dept, repID)
|
||||
if err != nil {
|
||||
return 0, ErrLocked // refine: only on constraint violation
|
||||
var serr *sqLite.Error
|
||||
if errors.As(err, &serr) && serr.Code() == sqlite3.SQLITE_CONSTRAINT_UNIQUE {
|
||||
return 0, ErrLocked
|
||||
}
|
||||
return 0, ErrLocked
|
||||
}
|
||||
return res.LastInsertId()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue