mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +01:00
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:
committed by
Christian Decker
parent
1e51275792
commit
63390a58b0
12
wallet/db.c
12
wallet/db.c
@@ -332,7 +332,7 @@ static int db_migration_count(void)
|
|||||||
/**
|
/**
|
||||||
* db_migrate - Apply all remaining migrations from the current version
|
* 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 */
|
/* Attempt to read the version from the database */
|
||||||
int current, available;
|
int current, available;
|
||||||
@@ -342,6 +342,12 @@ static void db_migrate(struct db *db)
|
|||||||
current = db_get_version(db);
|
current = db_get_version(db);
|
||||||
available = db_migration_count();
|
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)
|
while (++current <= available)
|
||||||
db_exec(__func__, db, "%s", dbmigrations[current]);
|
db_exec(__func__, db, "%s", dbmigrations[current]);
|
||||||
|
|
||||||
@@ -351,11 +357,11 @@ static void db_migrate(struct db *db)
|
|||||||
db_commit_transaction(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);
|
struct db *db = db_open(ctx, DB_FILE);
|
||||||
|
|
||||||
db_migrate(db);
|
db_migrate(db, log);
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,8 @@
|
|||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
struct log;
|
||||||
|
|
||||||
struct db {
|
struct db {
|
||||||
char *filename;
|
char *filename;
|
||||||
const char *in_transaction;
|
const char *in_transaction;
|
||||||
@@ -30,7 +32,7 @@ struct db {
|
|||||||
* @ctx: the tal_t context to allocate from
|
* @ctx: the tal_t context to allocate from
|
||||||
* @log: where to log messages to
|
* @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)
|
* db_query - Prepare and execute a query, and return the result (or NULL)
|
||||||
|
|||||||
@@ -3,6 +3,11 @@
|
|||||||
static void db_fatal(const char *fmt, ...);
|
static void db_fatal(const char *fmt, ...);
|
||||||
#define fatal db_fatal
|
#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 "wallet/db.c"
|
||||||
|
|
||||||
#include "test_utils.h"
|
#include "test_utils.h"
|
||||||
@@ -48,7 +53,7 @@ static bool test_empty_db_migrate(void)
|
|||||||
db_begin_transaction(db);
|
db_begin_transaction(db);
|
||||||
CHECK(db_get_version(db) == -1);
|
CHECK(db_get_version(db) == -1);
|
||||||
db_commit_transaction(db);
|
db_commit_transaction(db);
|
||||||
db_migrate(db);
|
db_migrate(db, NULL);
|
||||||
db_begin_transaction(db);
|
db_begin_transaction(db);
|
||||||
CHECK(db_get_version(db) == db_migration_count());
|
CHECK(db_get_version(db) == db_migration_count());
|
||||||
db_commit_transaction(db);
|
db_commit_transaction(db);
|
||||||
@@ -86,7 +91,7 @@ static bool test_vars(void)
|
|||||||
struct db *db = create_test_db(__func__);
|
struct db *db = create_test_db(__func__);
|
||||||
char *varname = "testvar";
|
char *varname = "testvar";
|
||||||
CHECK(db);
|
CHECK(db);
|
||||||
db_migrate(db);
|
db_migrate(db, NULL);
|
||||||
|
|
||||||
db_begin_transaction(db);
|
db_begin_transaction(db);
|
||||||
/* Check default behavior */
|
/* Check default behavior */
|
||||||
|
|||||||
@@ -4,6 +4,11 @@ static void wallet_fatal(const char *fmt, ...);
|
|||||||
#define fatal wallet_fatal
|
#define fatal wallet_fatal
|
||||||
#include "test_utils.h"
|
#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/wallet.c"
|
||||||
|
|
||||||
#include "wallet/db.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);
|
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);
|
ltmp = tal_tmpctx(ctx);
|
||||||
log_book = new_log_book(w, 20*1024*1024, LOG_DBG);
|
log_book = new_log_book(w, 20*1024*1024, LOG_DBG);
|
||||||
w->log = new_log(w, log_book, "wallet_tests(%u):", (int)getpid());
|
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;
|
return w;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,7 +93,7 @@ static bool test_wallet_outputs(void)
|
|||||||
|
|
||||||
w->db = db_open(w, filename);
|
w->db = db_open(w, filename);
|
||||||
CHECK_MSG(w->db, "Failed opening the db");
|
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(!wallet_err, "DB migration failed");
|
||||||
|
|
||||||
memset(&u, 0, sizeof(u));
|
memset(&u, 0, sizeof(u));
|
||||||
@@ -142,7 +148,7 @@ static bool test_shachain_crud(void)
|
|||||||
|
|
||||||
w->db = db_open(w, filename);
|
w->db = db_open(w, filename);
|
||||||
CHECK_MSG(w->db, "Failed opening the db");
|
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(!wallet_err, "DB migration failed");
|
||||||
|
|
||||||
CHECK_MSG(fd != -1, "Unable to generate temp filename");
|
CHECK_MSG(fd != -1, "Unable to generate temp filename");
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
struct wallet *wallet_new(const tal_t *ctx, struct log *log)
|
struct wallet *wallet_new(const tal_t *ctx, struct log *log)
|
||||||
{
|
{
|
||||||
struct wallet *wallet = tal(ctx, struct wallet);
|
struct wallet *wallet = tal(ctx, struct wallet);
|
||||||
wallet->db = db_setup(wallet);
|
wallet->db = db_setup(wallet, log);
|
||||||
wallet->log = log;
|
wallet->log = log;
|
||||||
wallet->bip32_base = NULL;
|
wallet->bip32_base = NULL;
|
||||||
return wallet;
|
return wallet;
|
||||||
|
|||||||
Reference in New Issue
Block a user