db: log a message to say whether we created or updated db.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-01-02 14:33:42 +10:30 committed by Christian Decker
parent 1e51275792
commit 63390a58b0
5 changed files with 32 additions and 13 deletions

View File

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

View File

@ -13,6 +13,8 @@
#include <sqlite3.h>
#include <stdbool.h>
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)

View File

@ -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 */

View File

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

View File

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