diff --git a/wallet/db.c b/wallet/db.c index 741747daf..49951effe 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -905,8 +905,6 @@ static void db_migrate(struct lightningd *ld, struct db *db) int current, orig, available; struct db_stmt *stmt; - db_begin_transaction(db); - orig = current = db_get_version(db); available = ARRAY_SIZE(dbmigrations) - 1; @@ -947,14 +945,18 @@ static void db_migrate(struct lightningd *ld, struct db *db) tal_free(stmt); } - db_commit_transaction(db); } struct db *db_setup(const tal_t *ctx, struct lightningd *ld) { struct db *db = db_open(ctx, ld->wallet_dsn); db->log = new_log(db, ld->log_book, NULL, "database"); + + db_begin_transaction(db); + db_migrate(ld, db); + + db_commit_transaction(db); return db; } diff --git a/wallet/test/run-db.c b/wallet/test/run-db.c index db9ddb953..bf326366c 100644 --- a/wallet/test/run-db.c +++ b/wallet/test/run-db.c @@ -73,8 +73,9 @@ static bool test_empty_db_migrate(struct lightningd *ld) CHECK(db); db_begin_transaction(db); CHECK(db_get_version(db) == -1); - db_commit_transaction(db); db_migrate(ld, db); + db_commit_transaction(db); + db_begin_transaction(db); CHECK(db_get_version(db) == ARRAY_SIZE(dbmigrations) - 1); db_commit_transaction(db); @@ -118,9 +119,9 @@ static bool test_vars(struct lightningd *ld) struct db *db = create_test_db(); char *varname = "testvar"; CHECK(db); - db_migrate(ld, db); db_begin_transaction(db); + db_migrate(ld, db); /* Check default behavior */ CHECK(db_get_intvar(db, varname, 42) == 42); diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 7010abd7a..63db38de4 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -747,7 +747,9 @@ static struct wallet *create_test_wallet(struct lightningd *ld, const tal_t *ctx w->bip32_base) == WALLY_OK); CHECK_MSG(w->db, "Failed opening the db"); + db_begin_transaction(w->db); db_migrate(ld, w->db); + db_commit_transaction(w->db); CHECK_MSG(!wallet_err, "DB migration failed"); w->max_channel_dbid = 0;