Older LND versions could create multiple payments for the same hash. We need to preserve those historical records during KV→SQL migration, but they don’t fit the normal payment schema because we enforce a unique payment hash constraint. Introduce a lean payment_duplicates table to store only the essential fields (identifier, amount, timestamps, settle/fail data). This keeps the primary payment records stable and makes the migration deterministic even when duplicate records lack attempt info. The table is intentionally minimal and can be dropped after migration if no duplicate payments exist. For now there is no logic in place which allows the noderunner to fetch duplicate payments after the migration.
44 lines
2.0 KiB
SQL
44 lines
2.0 KiB
SQL
-- ─────────────────────────────────────────────
|
|
-- Payment Duplicate Records Table
|
|
-- ─────────────────────────────────────────────
|
|
-- Stores duplicate payment records that were created in older versions
|
|
-- of lnd. This table is intentionally minimal and is expected to be dropped
|
|
-- in the future especially if no duplicates were migrated.
|
|
-- ─────────────────────────────────────────────
|
|
|
|
CREATE TABLE IF NOT EXISTS payment_duplicates (
|
|
-- Primary key for the duplicate record.
|
|
id INTEGER PRIMARY KEY,
|
|
|
|
-- Reference to the primary payment this duplicate belongs to.
|
|
payment_id BIGINT NOT NULL REFERENCES payments (id) ON DELETE CASCADE,
|
|
|
|
-- Amount of the duplicate payment in millisatoshis.
|
|
amount_msat BIGINT NOT NULL,
|
|
|
|
-- Timestamp when the duplicate payment was created.
|
|
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
-- Failure reason for failed payments (if known).
|
|
fail_reason INTEGER,
|
|
|
|
-- Settlement payload for succeeded payments (if known).
|
|
settle_preimage BLOB,
|
|
|
|
-- Settlement time for succeeded payments (if known).
|
|
settle_time TIMESTAMP,
|
|
|
|
-- Ensure we record either a failure reason or settlement data.
|
|
-- During the migration if we encounter a duplicate payment that has no
|
|
-- failure reason or settlement data, we will mark it as failed. Duplicate
|
|
-- payments were a bug in older versions of LND, so we can be sure if a
|
|
-- duplicate payment has no failure reason or settlement data, the
|
|
-- corresponding HTLC has been failed.
|
|
CONSTRAINT chk_payment_duplicates_outcome
|
|
CHECK (fail_reason IS NOT NULL OR settle_preimage IS NOT NULL)
|
|
);
|
|
|
|
-- Index for efficient lookup by primary payment.
|
|
CREATE INDEX IF NOT EXISTS idx_payment_duplicates_payment_id
|
|
ON payment_duplicates(payment_id);
|