From e87014e444085a64411559f46ea38877938de8c4 Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Mon, 27 Feb 2023 16:54:13 +0100 Subject: [PATCH] sqlite: return correct error on pod-name conflict I wasn't able to find a way to get error-checks working with the sqlite3 library with the time at hand. [NO NEW TESTS NEEDED] Signed-off-by: Valentin Rothberg --- libpod/sqlite_state.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/libpod/sqlite_state.go b/libpod/sqlite_state.go index a543523923..b86b648996 100644 --- a/libpod/sqlite_state.go +++ b/libpod/sqlite_state.go @@ -1470,6 +1470,19 @@ func (s *SQLiteState) AddPod(pod *Pod) (defErr error) { } }() + // TODO: explore whether there's a more idiomatic way to do error checks for the name. + // There is a sqlite3.ErrConstraintUnique error but I (vrothberg) couldn't find a way + // to work with the returned errors yet. + var check int + row := tx.QueryRow("SELECT 1 FROM PodConfig WHERE Name=?;", pod.Name()) + if err := row.Scan(&check); err != nil { + if !errors.Is(err, sql.ErrNoRows) { + return fmt.Errorf("checking if pod name %s exists in database: %w", pod.ID(), err) + } + } else if check != 0 { + return fmt.Errorf("name \"%s\" is in use: %w", pod.Name(), define.ErrPodExists) + } + if _, err := tx.Exec("INSERT INTO IDNamespace VALUES (?);", pod.ID()); err != nil { return fmt.Errorf("adding pod id to database: %w", err) }