diff --git a/db/exec.c b/db/exec.c index b791be16c..34d085e7a 100644 --- a/db/exec.c +++ b/db/exec.c @@ -25,7 +25,7 @@ int db_get_version(struct db *db) * table that doesn't exist yet, so we need to terminate and restart * the DB transaction. */ - if (!db_query_prepared(stmt)) { + if (!db_query_prepared_canfail(stmt)) { db_commit_transaction(stmt->db); db_begin_transaction(stmt->db); tal_free(stmt); @@ -45,7 +45,7 @@ u32 db_data_version_get(struct db *db) u32 version; stmt = db_prepare_v2(db, SQL("SELECT intval FROM vars WHERE name = 'data_version'")); /* postgres will act upset if the table doesn't exist yet. */ - if (!db_query_prepared(stmt)) { + if (!db_query_prepared_canfail(stmt)) { tal_free(stmt); return 0; } @@ -85,7 +85,7 @@ s64 db_get_intvar(struct db *db, char *varname, s64 defval) struct db_stmt *stmt = db_prepare_v2( db, SQL("SELECT intval FROM vars WHERE name= ? LIMIT 1")); db_bind_text(stmt, 0, varname); - if (db_query_prepared(stmt) && db_step(stmt)) + if (db_query_prepared_canfail(stmt) && db_step(stmt)) res = db_col_int(stmt, "intval"); tal_free(stmt); diff --git a/db/utils.c b/db/utils.c index 106aae834..ffed7a75a 100644 --- a/db/utils.c +++ b/db/utils.c @@ -135,7 +135,7 @@ struct db_stmt *db_prepare_untranslated(struct db *db, const char *query) return stmt; } -bool db_query_prepared(struct db_stmt *stmt) +bool db_query_prepared_canfail(struct db_stmt *stmt) { /* Make sure we don't accidentally execute a modifying query using a * read-only path. */ @@ -147,6 +147,13 @@ bool db_query_prepared(struct db_stmt *stmt) return ret; } +void db_query_prepared(struct db_stmt *stmt) +{ + if (!db_query_prepared_canfail(stmt)) + db_fatal("query failed: %s: %s", + stmt->location, stmt->query->query); +} + bool db_step(struct db_stmt *stmt) { bool ret; diff --git a/db/utils.h b/db/utils.h index e0c1f97f3..d34618b27 100644 --- a/db/utils.h +++ b/db/utils.h @@ -49,7 +49,12 @@ bool db_exec_prepared_v2(struct db_stmt *stmt TAKES); * * @stmt: The prepared statement to execute */ -bool db_query_prepared(struct db_stmt *stmt); +void db_query_prepared(struct db_stmt *stmt); + +/** + * Variation which allows failure. + */ +bool db_query_prepared_canfail(struct db_stmt *stmt); size_t db_count_changes(struct db_stmt *stmt); void db_report_changes(struct db *db, const char *final, size_t min); diff --git a/wallet/test/run-db.c b/wallet/test/run-db.c index 139242528..1ecbaa0d3 100644 --- a/wallet/test/run-db.c +++ b/wallet/test/run-db.c @@ -224,7 +224,7 @@ static bool test_manip_columns(void) CHECK(db->config->delete_columns(db, "tableb", &field1, 1)); stmt = db_prepare_v2(db, SQL("SELECT id, field1a FROM tablea;")); - CHECK_MSG(db_query_prepared(stmt), "db_query_prepared must succeed"); + CHECK_MSG(db_query_prepared_canfail(stmt), "db_query_prepared must succeed"); CHECK_MSG(!db_err, "Simple correct SQL command"); CHECK(db_step(stmt)); CHECK(db_col_u64(stmt, "id") == 0); @@ -233,7 +233,7 @@ static bool test_manip_columns(void) tal_free(stmt); stmt = db_prepare_v2(db, SQL("SELECT id, field2 FROM tableb;")); - CHECK_MSG(db_query_prepared(stmt), "db_query_prepared must succeed"); + CHECK_MSG(db_query_prepared_canfail(stmt), "db_query_prepared must succeed"); CHECK_MSG(!db_err, "Simple correct SQL command"); CHECK(db_step(stmt)); CHECK(db_col_u64(stmt, "id") == 0); @@ -247,7 +247,7 @@ static bool test_manip_columns(void) db_begin_transaction(db); /* This will actually fail */ stmt = db_prepare_v2(db, SQL("SELECT field1 FROM tablea;")); - CHECK_MSG(!db_query_prepared(stmt), "db_query_prepared must fail"); + CHECK_MSG(!db_query_prepared_canfail(stmt), "db_query_prepared must fail"); db->dirty = false; db->changes = tal_arr(db, const char *, 0); db_commit_transaction(db); @@ -255,7 +255,7 @@ static bool test_manip_columns(void) db_begin_transaction(db); /* This will actually fail */ stmt = db_prepare_v2(db, SQL("SELECT field1 FROM tableb;")); - CHECK_MSG(!db_query_prepared(stmt), "db_query_prepared must fail"); + CHECK_MSG(!db_query_prepared_canfail(stmt), "db_query_prepared must fail"); db->dirty = false; db->changes = tal_arr(db, const char *, 0); db_commit_transaction(db);