Skip to content

Commit a6c2f18

Browse files
committed
bookkeeper: fix up out-of-order migrations in rc1
This was introduced in rc1, resulting in: ``` 2024-08-15T01:33:48.343Z **BROKEN** plugin-bookkeeper: query failed: plugins/bkpr/recorder.c:738: SELECT e.id, e.account_id, a.name, e.origin, e.tag, e.credit, e.debit, e.output_value, e.currency, e.timestamp, e.blockheight, e.utxo_txid, e.outnum, e.spending_txid, e.payment_id, e.ignored, e.stealable, e.ev_desc, e.spliced FROM chain_events e LEFT OUTER JOIN accounts a ON e.account_id = a.id WHERE e.spending_txid = ? AND e.account_id = ? AND e.utxo_txid = ? AND e.outnum = ? ``` Signed-off-by: Rusty Russell <[email protected]>
1 parent be5ad3d commit a6c2f18

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

plugins/bkpr/db.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct migration {
1414
};
1515

1616
static void migration_remove_dupe_lease_fees(struct plugin *p, struct db *db);
17+
static void migration_maybe_add_chainevents_spliced(struct plugin *p, struct db *db);
1718

1819
/* Do not reorder or remove elements from this array.
1920
* It is used to migrate existing databases from a prevoius state, based on
@@ -100,7 +101,8 @@ static struct migration db_migrations[] = {
100101
{SQL("ALTER TABLE channel_events ADD ev_desc TEXT DEFAULT NULL;"), NULL},
101102
{SQL("ALTER TABLE channel_events ADD rebalance_id BIGINT DEFAULT NULL;"), NULL},
102103
{SQL("ALTER TABLE chain_events ADD spliced INTEGER DEFAULT 0;"), NULL},
103-
{NULL, migration_remove_dupe_lease_fees}
104+
{NULL, migration_remove_dupe_lease_fees},
105+
{NULL, migration_maybe_add_chainevents_spliced}
104106
};
105107

106108
static bool db_migrate(struct plugin *p, struct db *db)
@@ -184,6 +186,27 @@ static void migration_remove_dupe_lease_fees(struct plugin *p, struct db *db)
184186
tal_free(stmt);
185187
}
186188

189+
/* OK, funny story. We added the "ALTER TABLE chain_events ADD spliced INTEGER DEFAULT 0;"
190+
* migration in the wrong place, NOT at the end. So if you are migrating from an old version,
191+
* "migration_remove_dupe_lease_fees" ran (again), which is harmless, but this migration
192+
* never got added. */
193+
static void migration_maybe_add_chainevents_spliced(struct plugin *p, struct db *db)
194+
{
195+
struct db_stmt *stmt;
196+
bool col_exists;
197+
198+
stmt = db_prepare_v2(db, SQL("SELECT spliced FROM chain_events"));
199+
col_exists = db_query_prepared_canfail(stmt);
200+
tal_free(stmt);
201+
if (col_exists)
202+
return;
203+
204+
plugin_log(p, LOG_INFORM,
205+
"Database fixup: adding spliced column to chain_events table");
206+
stmt = db_prepare_v2(db, SQL("ALTER TABLE chain_events ADD spliced INTEGER DEFAULT 0;"));
207+
db_exec_prepared_v2(take(stmt));
208+
}
209+
187210
static void db_error(struct plugin *plugin, bool fatal, const char *fmt, va_list ap)
188211
{
189212
if (fatal)
960 Bytes
Binary file not shown.

tests/test_bookkeeper.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,3 +947,16 @@ def test_bookkeeper_custom_notifs(node_factory):
947947
incomes = l1.rpc.bkpr_listincome()['income_events']
948948
acct_fee = only_one([inc['debit_msat'] for inc in incomes if inc['account'] == acct and inc['tag'] == 'onchain_fee'])
949949
assert acct_fee == Millisatoshi(fee)
950+
951+
952+
@unittest.skipIf(os.getenv('TEST_DB_PROVIDER', 'sqlite3') != 'sqlite3', "This test is based on a sqlite3 snapshot")
953+
def test_bookkeeper_bad_migration(node_factory):
954+
l1 = node_factory.get_node(bkpr_dbfile='bookkeeper-accounts-pre-v24.08-migration.sqlite3.xz')
955+
l2 = node_factory.get_node()
956+
957+
# Make sure l1 does fixup, use query to force an access (which would fail if column not present)
958+
assert l1.daemon.is_in_log("plugin-bookkeeper: Database fixup: adding spliced column to chain_events table")
959+
l1.rpc.bkpr_listaccountevents('wallet')
960+
961+
# l2 will *not* do this
962+
assert not l2.daemon.is_in_log("adding spliced column")

0 commit comments

Comments
 (0)