# HG changeset patch # User David Demelier # Date 1581688559 -3600 # Node ID f939d7864de4b59fbbec1a12eda0e28a40cdb0a3 # Parent a2789cb8ebe6ab49991e30a46c2d4d9c06eb88c1 pasterd: fix unique key constraint diff -r a2789cb8ebe6 -r f939d7864de4 database.c --- a/database.c Fri Feb 14 14:19:17 2020 +0100 +++ b/database.c Fri Feb 14 14:55:59 2020 +0100 @@ -135,16 +135,14 @@ assert(id); sqlite3_stmt *stmt = NULL; - bool ret = false; + bool ret = true; - if (sqlite3_prepare(db, sql_get, -1, &stmt, NULL) != SQLITE_OK) { - log_warn("database: error (exists): %s", sqlite3_errmsg(db)); - return false; + if (sqlite3_prepare(db, sql_get, -1, &stmt, NULL) == SQLITE_OK) { + sqlite3_bind_text(stmt, 1, id, -1, NULL); + ret = sqlite3_step(stmt) == SQLITE_ROW; + sqlite3_finalize(stmt); } - ret = sqlite3_step(stmt) == SQLITE_ROW; - sqlite3_finalize(stmt); - return ret; } @@ -169,6 +167,9 @@ /* * Avoid infinite loop, we only try to create a new id in 30 steps. + * + * On error, the function `exist` returns true to indicate we should + * not try to save with that id. */ int tries = 0;