From ad896998889edbc5d5526873f8773dcb3134faf2 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 10 Sep 2019 14:02:12 +0200 Subject: [PATCH] db: Allow some internal queries to fail The first ever query to check if the version DB exists may fail. We allow this, but we need to restart the DB transaction since postgres fails the current transaction and rolls back any changes. This just commits (and fails) and starts a new transaction so the rest of the migration can continue. Signed-off-by: Christian Decker --- wallet/db.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/wallet/db.c b/wallet/db.c index 683107b04..6ab1bd8ae 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -697,13 +697,23 @@ static int db_get_version(struct db *db) { int res = -1; struct db_stmt *stmt = db_prepare_v2(db, SQL("SELECT version FROM version LIMIT 1")); + + /* + * Tentatively execute a query, but allow failures. Some databases + * like postgres will terminate the DB transaction if there is an + * error during the execution of a query, e.g., trying to access a + * table that doesn't exist yet, so we need to terminate and restart + * the DB transaction. + */ if (!db_query_prepared(stmt)) { + db_commit_transaction(stmt->db); + db_begin_transaction(stmt->db); tal_free(stmt); return res; } if (db_step(stmt)) - res = db_column_u64(stmt, 0); + res = db_column_int(stmt, 0); tal_free(stmt); return res;