diff --git a/wallet/db.c b/wallet/db.c index 60cf44003..575ee67ff 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -332,7 +332,7 @@ static int db_migration_count(void) /** * db_migrate - Apply all remaining migrations from the current version */ -static void db_migrate(struct db *db) +static void db_migrate(struct db *db, struct log *log) { /* Attempt to read the version from the database */ int current, available; @@ -342,6 +342,12 @@ static void db_migrate(struct db *db) current = db_get_version(db); available = db_migration_count(); + if (current == -1) + log_info(log, "Creating database"); + else if (current != available) + log_info(log, "Updating database from version %u to %u", + current, available); + while (++current <= available) db_exec(__func__, db, "%s", dbmigrations[current]); @@ -351,11 +357,11 @@ static void db_migrate(struct db *db) db_commit_transaction(db); } -struct db *db_setup(const tal_t *ctx) +struct db *db_setup(const tal_t *ctx, struct log *log) { struct db *db = db_open(ctx, DB_FILE); - db_migrate(db); + db_migrate(db, log); return db; } diff --git a/wallet/db.h b/wallet/db.h index 2e7d18612..eb731acbf 100644 --- a/wallet/db.h +++ b/wallet/db.h @@ -13,6 +13,8 @@ #include #include +struct log; + struct db { char *filename; const char *in_transaction; @@ -30,7 +32,7 @@ struct db { * @ctx: the tal_t context to allocate from * @log: where to log messages to */ -struct db *db_setup(const tal_t *ctx); +struct db *db_setup(const tal_t *ctx, struct log *log); /** * db_query - Prepare and execute a query, and return the result (or NULL) diff --git a/wallet/test/run-db.c b/wallet/test/run-db.c index 22d6ca4ea..10e05f19a 100644 --- a/wallet/test/run-db.c +++ b/wallet/test/run-db.c @@ -3,6 +3,11 @@ static void db_fatal(const char *fmt, ...); #define fatal db_fatal +static void db_log_(struct log *log, enum log_level level, const char *fmt, ...) +{ +} +#define log_ db_log_ + #include "wallet/db.c" #include "test_utils.h" @@ -48,7 +53,7 @@ static bool test_empty_db_migrate(void) db_begin_transaction(db); CHECK(db_get_version(db) == -1); db_commit_transaction(db); - db_migrate(db); + db_migrate(db, NULL); db_begin_transaction(db); CHECK(db_get_version(db) == db_migration_count()); db_commit_transaction(db); @@ -86,7 +91,7 @@ static bool test_vars(void) struct db *db = create_test_db(__func__); char *varname = "testvar"; CHECK(db); - db_migrate(db); + db_migrate(db, NULL); db_begin_transaction(db); /* Check default behavior */ diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 0a34e3047..66855b814 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -4,6 +4,11 @@ static void wallet_fatal(const char *fmt, ...); #define fatal wallet_fatal #include "test_utils.h" +static void db_log_(struct log *log, enum log_level level, const char *fmt, ...) +{ +} +#define log_ db_log_ + #include "wallet/wallet.c" #include "wallet/db.c" @@ -65,14 +70,15 @@ static struct wallet *create_test_wallet(const tal_t *ctx) w->db = db_open(w, filename); - CHECK_MSG(w->db, "Failed opening the db"); - 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); w->log = new_log(w, log_book, "wallet_tests(%u):", (int)getpid()); + CHECK_MSG(w->db, "Failed opening the db"); + db_migrate(w->db, w->log); + CHECK_MSG(!wallet_err, "DB migration failed"); + + return w; } @@ -87,7 +93,7 @@ static bool test_wallet_outputs(void) w->db = db_open(w, filename); CHECK_MSG(w->db, "Failed opening the db"); - db_migrate(w->db); + db_migrate(w->db, NULL); CHECK_MSG(!wallet_err, "DB migration failed"); memset(&u, 0, sizeof(u)); @@ -142,7 +148,7 @@ static bool test_shachain_crud(void) w->db = db_open(w, filename); CHECK_MSG(w->db, "Failed opening the db"); - db_migrate(w->db); + db_migrate(w->db, NULL); CHECK_MSG(!wallet_err, "DB migration failed"); CHECK_MSG(fd != -1, "Unable to generate temp filename"); diff --git a/wallet/wallet.c b/wallet/wallet.c index 321dfc045..ef4b31593 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -17,7 +17,7 @@ struct wallet *wallet_new(const tal_t *ctx, struct log *log) { struct wallet *wallet = tal(ctx, struct wallet); - wallet->db = db_setup(wallet); + wallet->db = db_setup(wallet, log); wallet->log = log; wallet->bip32_base = NULL; return wallet;