diff --git a/wallet/db.c b/wallet/db.c index 1f50fc2a0..1d054db6f 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -363,47 +363,30 @@ static int db_migration_count(void) /** * db_migrate - Apply all remaining migrations from the current version */ -static bool db_migrate(struct db *db) +static void db_migrate(struct db *db) { /* Attempt to read the version from the database */ int current, available; - if (!db_begin_transaction(db)) { - /* No need to rollback, we didn't even start... */ - return false; - } + db_begin_transaction(db); current = db_get_version(db); available = db_migration_count(); - while (++current <= available) { - if (!db_exec(__func__, db, "%s", dbmigrations[current])) - goto fail; - } + while (++current <= available) + db_exec(__func__, db, "%s", dbmigrations[current]); /* Finally update the version number in the version table */ db_exec(__func__, db, "UPDATE version SET version=%d;", available); - if (!db_commit_transaction(db)) { - goto fail; - } - - return true; -fail: - db_rollback_transaction(db); - return false; + db_commit_transaction(db); } struct db *db_setup(const tal_t *ctx) { struct db *db = db_open(ctx, DB_FILE); - if (!db) { - return db; - } - if (!db_migrate(db)) { - return tal_free(db); - } + db_migrate(db); return db; } diff --git a/wallet/db_tests.c b/wallet/db_tests.c index ee5d2568f..83feee23c 100644 --- a/wallet/db_tests.c +++ b/wallet/db_tests.c @@ -24,7 +24,7 @@ static bool test_empty_db_migrate(void) struct db *db = create_test_db(__func__); CHECK(db); CHECK(db_get_version(db) == -1); - CHECK(db_migrate(db)); + db_migrate(db); CHECK(db_get_version(db) == db_migration_count()); tal_free(db); @@ -55,7 +55,7 @@ static bool test_vars(void) struct db *db = create_test_db(__func__); char *varname = "testvar"; CHECK(db); - CHECK(db_migrate(db)); + db_migrate(db); /* Check default behavior */ CHECK(db_get_intvar(db, varname, 42) == 42); diff --git a/wallet/wallet.c b/wallet/wallet.c index 4c18035aa..ed3283947 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -18,9 +18,6 @@ struct wallet *wallet_new(const tal_t *ctx, struct log *log) wallet->db = db_setup(wallet); wallet->log = log; wallet->bip32_base = NULL; - if (!wallet->db) { - fatal("Unable to setup the wallet database"); - } return wallet; } diff --git a/wallet/wallet_tests.c b/wallet/wallet_tests.c index bba988e53..f58a69b30 100644 --- a/wallet/wallet_tests.c +++ b/wallet/wallet_tests.c @@ -1,13 +1,32 @@ + #include + +static void wallet_fatal(const char *fmt, ...); +#define fatal wallet_fatal + #include "wallet.c" #include "db.c" #include -#include +#include +#include #include #include #include +static char *wallet_err; +static void wallet_fatal(const char *fmt, ...) +{ + va_list ap; + + /* Fail hard if we're complaining about not being in transaction */ + assert(!strstarts(fmt, "No longer in transaction")); + + va_start(ap, fmt); + wallet_err = tal_vfmt(NULL, fmt, ap); + va_end(ap); +} + void invoice_add(struct invoices *invs, struct invoice *inv){} @@ -37,7 +56,8 @@ static struct wallet *create_test_wallet(const tal_t *ctx) w->db = db_open(w, filename); CHECK_MSG(w->db, "Failed opening the db"); - CHECK_MSG(db_migrate(w->db), "DB migration failed"); + db_migrate(w->db); + CHECK_MSG(!wallet_err, "DB migration failed"); ltmp = tal_tmpctx(ctx); log_book = new_log_book(w, 20*1024*1024, LOG_DBG); @@ -57,7 +77,8 @@ static bool test_wallet_outputs(void) w->db = db_open(w, filename); CHECK_MSG(w->db, "Failed opening the db"); - CHECK_MSG(db_migrate(w->db), "DB migration failed"); + db_migrate(w->db); + CHECK_MSG(!wallet_err, "DB migration failed"); memset(&u, 0, sizeof(u)); @@ -108,7 +129,8 @@ static bool test_shachain_crud(void) w->db = db_open(w, filename); CHECK_MSG(w->db, "Failed opening the db"); - CHECK_MSG(db_migrate(w->db), "DB migration failed"); + db_migrate(w->db); + CHECK_MSG(!wallet_err, "DB migration failed"); CHECK_MSG(fd != -1, "Unable to generate temp filename"); close(fd);