db: fix migrations which write to db.

valgrind noticed that this was uninitialized when I tried a complex
migration.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2022-09-09 17:02:15 +09:30 committed by Christian Decker
parent 6438ee4126
commit d7aa2749c3
2 changed files with 6 additions and 3 deletions

View File

@ -45,8 +45,11 @@ u32 db_data_version_get(struct db *db)
u32 version;
stmt = db_prepare_v2(db, SQL("SELECT intval FROM vars WHERE name = 'data_version'"));
db_query_prepared(stmt);
db_step(stmt);
version = db_col_int(stmt, "intval");
/* This fails on uninitialized db, so "0" */
if (db_step(stmt))
version = db_col_int(stmt, "intval");
else
version = 0;
tal_free(stmt);
return version;
}

View File

@ -950,10 +950,10 @@ struct db *db_setup(const tal_t *ctx, struct lightningd *ld,
db->report_changes_fn = plugin_hook_db_sync;
db_begin_transaction(db);
db->data_version = db_data_version_get(db);
migrated = db_migrate(ld, db, bip32_base);
db->data_version = db_data_version_get(db);
db_commit_transaction(db);
/* This needs to be done outside a transaction, apparently.