Skip to content

Commit

Permalink
pytest: fix the invoice typo migration if you ran and created both.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jul 30, 2023
1 parent e119a35 commit 17cb14b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
1 change: 0 additions & 1 deletion tests/test_invoices.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,6 @@ def test_invoices_wait_db_migration(node_factory, bitcoind):
l2.rpc.invoice(1000, "test", "test")


@pytest.mark.xfail(strict=True)
def test_invoice_botched_migration(node_factory, chainparams):
"""Test for grubles' case, where they ran successfully with the wrong var: they have *both* last_invoice_created_index *and *last_invoices_created_index* (this can happen if invoice id 1 was deleted, so they didn't die on invoice creation):
Error executing statement: wallet/db.c:1684: UPDATE vars SET name = 'last_invoices_created_index' WHERE name = 'last_invoice_created_index': UNIQUE constraint failed: vars.name
Expand Down
31 changes: 28 additions & 3 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -1679,10 +1679,35 @@ static void migrate_initialize_wait_indexes(struct lightningd *ld,
static void migrate_invoice_created_index_var(struct lightningd *ld, struct db *db)
{
struct db_stmt *stmt;
s64 badindex, realindex;

/* Prior migration had a typo! But we might have run since
* then and created an invoice, so we have to set the real one
* to the max of the two... */
badindex = db_get_intvar(db, "last_invoice_created_index", -1);
realindex = db_get_intvar(db, "last_invoices_created_index", -1);

/* Bad index does not exist? Fine */
if (badindex < 0)
return;

/* Bad index exists, real index doesn't? Rename */
if (badindex >= 0 && realindex < 0) {
stmt = db_prepare_v2(db, SQL("UPDATE vars"
" SET name = 'last_invoices_created_index'"
" WHERE name = 'last_invoice_created_index'"));
db_exec_prepared_v2(stmt);
tal_free(stmt);
return;
}

/* Both exist. Correct value is the higher one. */
if (badindex > realindex)
realindex = badindex;

/* Prior migration had a typo! */
stmt = db_prepare_v2(db, SQL("UPDATE vars"
" SET name = 'last_invoices_created_index'"
/* Update correct one, remove bad one. */
db_set_intvar(db, "last_invoices_created_index", realindex);
stmt = db_prepare_v2(db, SQL("DELETE FROM vars"
" WHERE name = 'last_invoice_created_index'"));
db_exec_prepared_v2(stmt);
tal_free(stmt);
Expand Down

0 comments on commit 17cb14b

Please sign in to comment.