wallet: add list of upgrades.

Useful for debugging a db.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-02-17 15:11:08 +10:30
parent 097b9345eb
commit d47d85fa30
4 changed files with 45 additions and 2 deletions

View File

@ -1047,6 +1047,29 @@ class LightningDTests(BaseLightningDTests):
wait_forget_channels(l1)
wait_forget_channels(l2)
def test_db_upgrade(self):
l1 = self.node_factory.get_node()
l1.stop()
version = subprocess.check_output(['lightningd/lightningd',
'--version']).decode('utf-8').splitlines()[0]
upgrades = l1.db_query("SELECT * from db_upgrades;")
assert len(upgrades) == 1
assert(upgrades[0]['upgrade_from'] == -1)
assert(upgrades[0]['lightning_version'] == version)
# Try resetting to earlier db state.
os.unlink(os.path.join(l1.daemon.lightning_dir, "lightningd.sqlite3"))
l1.db_manip("CREATE TABLE version (version INTEGER);")
l1.db_manip("INSERT INTO version VALUES (1);")
l1.daemon.start()
upgrades = l1.db_query("SELECT * from db_upgrades;")
assert len(upgrades) == 1
assert(upgrades[0]['upgrade_from'] == 1)
assert(upgrades[0]['lightning_version'] == version)
def test_closing_different_fees(self):
l1 = self.node_factory.get_node()

View File

@ -365,6 +365,16 @@ class LightningNode(object):
db.close()
return result
# Assumes node is stopped!
def db_manip(self, query):
db = sqlite3.connect(os.path.join(self.daemon.lightning_dir, "lightningd.sqlite3"))
db.row_factory = sqlite3.Row
c = db.cursor()
c.execute(query)
db.commit()
c.close()
db.close()
def stop(self, timeout=10):
""" Attempt to do a clean shutdown, but kill if it hangs
"""

View File

@ -2,6 +2,7 @@
#include <ccan/tal/str/str.h>
#include <ccan/tal/tal.h>
#include <common/version.h>
#include <inttypes.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
@ -193,6 +194,8 @@ char *dbmigrations[] = {
"CREATE INDEX channel_idx ON htlc_sigs (channelid)",
/* Get rid of OPENINGD entries; we don't put them in db any more */
"DELETE FROM channels WHERE state=1",
/* Keep track of db ugprades, for debugging */
"CREATE TABLE db_upgrades (upgrade_from INTEGER, lightning_version TEXT);",
NULL,
};
@ -386,11 +389,11 @@ static int db_migration_count(void)
static void db_migrate(struct db *db, struct log *log)
{
/* Attempt to read the version from the database */
int current, available;
int current, orig, available;
db_begin_transaction(db);
current = db_get_version(db);
orig = current = db_get_version(db);
available = db_migration_count();
if (current == -1)
@ -408,6 +411,12 @@ static void db_migrate(struct db *db, struct log *log)
/* Finally update the version number in the version table */
db_exec(__func__, db, "UPDATE version SET version=%d;", available);
/* Annotate that we did upgrade, if any. */
if (current != orig)
db_exec(__func__, db,
"INSERT INTO db_upgrades VALUES (%i, '%s');",
orig, version());
db_commit_transaction(db);
}

View File

@ -10,6 +10,7 @@ WALLET_TEST_COMMON_OBJS := \
common/timeout.o \
common/utils.o \
common/wireaddr.o \
common/version.o \
wire/towire.o \
wire/fromwire.o