From d47d85fa30a9d9318323504b01f6173c723952d4 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 17 Feb 2018 15:11:08 +1030 Subject: [PATCH] wallet: add list of upgrades. Useful for debugging a db. Signed-off-by: Rusty Russell --- tests/test_lightningd.py | 23 +++++++++++++++++++++++ tests/utils.py | 10 ++++++++++ wallet/db.c | 13 +++++++++++-- wallet/test/Makefile | 1 + 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/tests/test_lightningd.py b/tests/test_lightningd.py index 1455eab17..9cc3b8147 100644 --- a/tests/test_lightningd.py +++ b/tests/test_lightningd.py @@ -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() diff --git a/tests/utils.py b/tests/utils.py index f8b7db817..9de45b357 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -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 """ diff --git a/wallet/db.c b/wallet/db.c index 6639ad66d..8ea5c0b7e 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -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); } diff --git a/wallet/test/Makefile b/wallet/test/Makefile index 090e23822..e7d028428 100644 --- a/wallet/test/Makefile +++ b/wallet/test/Makefile @@ -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