db: Move db_migrate transaction up one level

We are about to do some more operations before committing, so moving this up
allows us to reuse the same transaction.
This commit is contained in:
Christian Decker
2019-12-18 18:57:37 +01:00
committed by neil saitug
parent 6020a0d587
commit 097af493dd
3 changed files with 10 additions and 5 deletions

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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;