store: widen payments.kind to accept 'tip' so tips are stored
insert_payment has allowed kind = "tip" since the tip path landed, but the
payments table's CHECK only permitted invoice/receipt, so every tip insert
failed ("CHECK constraint failed: kind IN ('invoice','receipt')"). That made
the outbound tip row impossible and silently broke the inbound path too:
ingest_tip returned early and neither the row nor the [tip] chat line was
written.
- recreate the table with the widened CHECK on open, copying rows verbatim
(new stores get it from the schema directly)
- outbound tip() records kind = "tip" with the tip's memo, matching ingest_tip
- tests: outbound row, inbound round trip, pre-tip store migration
Refs kanban t_ecf204ad
This commit is contained in:
parent
24b5c93c43
commit
186ec7436d
2 changed files with 183 additions and 6 deletions
97
src/store.rs
97
src/store.rs
|
|
@ -261,7 +261,7 @@ impl Store {
|
|||
id INTEGER PRIMARY KEY,
|
||||
friend_id INTEGER NOT NULL REFERENCES friends(id),
|
||||
dir TEXT NOT NULL CHECK(dir IN ('in','out')),
|
||||
kind TEXT NOT NULL CHECK(kind IN ('invoice','receipt')),
|
||||
kind TEXT NOT NULL CHECK(kind IN ('invoice','receipt','tip')),
|
||||
amount_atomic TEXT NOT NULL,
|
||||
address TEXT NOT NULL,
|
||||
memo TEXT NOT NULL DEFAULT '',
|
||||
|
|
@ -365,6 +365,51 @@ impl Store {
|
|||
"payload",
|
||||
"ALTER TABLE outbox ADD COLUMN payload BLOB",
|
||||
)?;
|
||||
// `insert_payment` has accepted `kind = "tip"` since the tip path
|
||||
// landed, but the table's CHECK only allowed invoice/receipt, so every
|
||||
// tip insert (inbound *and* outbound) failed the constraint and the
|
||||
// frame was dropped. SQLite cannot alter a CHECK, so rebuild.
|
||||
self.widen_payment_kinds()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Widen `payments.kind` to accept `'tip'` on stores created before the
|
||||
/// tip path. The table is rebuilt in place because SQLite has no
|
||||
/// `ALTER TABLE ... DROP CONSTRAINT`; ids and rows are preserved verbatim.
|
||||
fn widen_payment_kinds(&self) -> Result<()> {
|
||||
let schema: Option<String> = self
|
||||
.conn
|
||||
.query_row(
|
||||
"SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'payments'",
|
||||
[],
|
||||
|row| row.get(0),
|
||||
)
|
||||
.optional()?;
|
||||
let Some(schema) = schema else {
|
||||
return Ok(());
|
||||
};
|
||||
if schema.contains("'tip'") {
|
||||
return Ok(());
|
||||
}
|
||||
self.conn.execute_batch(
|
||||
"ALTER TABLE payments RENAME TO payments_pre_tip;
|
||||
CREATE TABLE payments (
|
||||
id INTEGER PRIMARY KEY,
|
||||
friend_id INTEGER NOT NULL REFERENCES friends(id),
|
||||
dir TEXT NOT NULL CHECK(dir IN ('in','out')),
|
||||
kind TEXT NOT NULL CHECK(kind IN ('invoice','receipt','tip')),
|
||||
amount_atomic TEXT NOT NULL,
|
||||
address TEXT NOT NULL,
|
||||
memo TEXT NOT NULL DEFAULT '',
|
||||
txid TEXT,
|
||||
verified INTEGER NOT NULL DEFAULT 0,
|
||||
created_at INTEGER NOT NULL
|
||||
);
|
||||
INSERT INTO payments (id, friend_id, dir, kind, amount_atomic, address, memo, txid, verified, created_at)
|
||||
SELECT id, friend_id, dir, kind, amount_atomic, address, memo, txid, verified, created_at
|
||||
FROM payments_pre_tip;
|
||||
DROP TABLE payments_pre_tip;",
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -1940,6 +1985,56 @@ mod at_rest {
|
|||
}
|
||||
}
|
||||
|
||||
/// The old `payments.kind` CHECK allowed only invoice/receipt, so every
|
||||
/// `kind = "tip"` insert (inbound and outbound) failed the constraint.
|
||||
/// Opening an existing store must widen it without losing the rows in it.
|
||||
#[test]
|
||||
fn a_store_predating_tip_payments_is_widened_on_open() {
|
||||
const ADDR: &str = "4AdUndXHHZ6cfufTMvppY6JwXNouMBzSkbLYfpAV5Usx3skxNgYeYTRj5UzqtReoS44qo9mtmXCqY45DJ852K5Jv2684Rge";
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let db_path = dir.path().join("onionwire.db");
|
||||
write_legacy_plaintext_store(&db_path, "tip-pass");
|
||||
{
|
||||
let conn = Connection::open(&db_path).unwrap();
|
||||
let schema: String = conn
|
||||
.query_row(
|
||||
"SELECT sql FROM sqlite_master WHERE name = 'payments'",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!schema.contains("'tip'"), "fixture is the pre-tip table");
|
||||
conn.execute(
|
||||
"INSERT INTO payments (friend_id, dir, kind, amount_atomic, address, memo, txid, verified, created_at)
|
||||
VALUES (1, 'out', 'invoice', '1000000000000', ?1, 'm', NULL, 0, 7)",
|
||||
params![ADDR],
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let store = Store::open_at_with_passphrase(dir.path(), "tip-pass").expect("open+upgrade");
|
||||
let rows = store.list_payments(&LEGACY_FRIEND_PK).unwrap();
|
||||
assert_eq!(rows.len(), 1, "the pre-tip row survives the rebuild");
|
||||
assert_eq!(rows[0].kind, "invoice");
|
||||
assert_eq!(rows[0].amount_atomic, "1000000000000");
|
||||
assert_eq!(rows[0].created_at, 7, "rows are copied verbatim");
|
||||
|
||||
store
|
||||
.insert_payment(
|
||||
&LEGACY_FRIEND_PK,
|
||||
PaymentWrite {
|
||||
dir: "in",
|
||||
kind: "tip",
|
||||
amount_atomic: "500000000000",
|
||||
address: ADDR,
|
||||
memo: "lunch",
|
||||
txid: Some("abc123"),
|
||||
verified: false,
|
||||
},
|
||||
)
|
||||
.expect("a migrated store accepts the tip kind");
|
||||
}
|
||||
|
||||
/// `ratchet_sessions` keys on `friend_id`, and nothing in the crate deletes
|
||||
/// friends yet, so the cascade is exercised with the raw statement a future
|
||||
/// friend-removal path would run.
|
||||
|
|
|
|||
Loading…
Reference in a new issue