refactor: remove deprecated token columns from disputes table#516
Conversation
|
Warning Rate limit exceeded@grunch has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 4 minutes and 23 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (2)
WalkthroughDependency updates in Cargo.toml (reqwest, mostro-core); DB migration SQL and runtime code remove Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor User as Initiator
participant App as App (dispute.rs)
participant DB as Database
participant Notif as Notifier
participant Nostr as Nostr Network
User->>App: dispute_action(order_id, reason)
App->>DB: Create Dispute (no tokens)
DB-->>App: Dispute{id, ...}
App->>Notif: notify_dispute_to_users(dispute, initiator_pubkey, counterpart_pubkey)
Note over Notif: Build Payload::Dispute(dispute.id, None)
Notif->>Nostr: Send DM to initiator
Notif->>Nostr: Send DM to counterpart
Nostr-->>Notif: Ack
Notif-->>App: Ok
App-->>User: Dispute initiated
sequenceDiagram
autonumber
participant FS as Filesystem
participant DB as SqlitePool
participant Init as db::init
participant Mig as migrate_remove_token_columns
Init->>FS: Check DB file exists?
alt New DB
Init->>DB: Create and run base migrations
Init->>Mig: Inspect disputes schema
Mig->>DB: PRAGMA table_info(disputes)
alt Columns exist
Mig->>DB: ALTER TABLE drop buyer_token/seller_token
DB-->>Mig: Success
else No columns
Mig-->>Init: No-op
end
else Existing DB
Init->>Mig: Inspect and drop token columns if present
end
Init-->>DB: Return ready pool
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested reviewers
Poem
✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 3
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (3)
migrations/20230928145530_disputes.sql (1)
1-9: Do not edit historical migration files; add a new forward migration to drop columnsChanging an already-applied migration’s contents risks migration drift/checksum mismatch with sqlx, breaking environments that previously applied the original file. Keep this file immutable and introduce a new migration to remove the legacy columns.
Minimal forward migration suggestion (new file, e.g., 20250904120000_drop_dispute_tokens.sql):
+-- 20250904120000_drop_dispute_tokens.sql +-- Prefer DROP COLUMN when available; otherwise rebuild table (SQLite < 3.35 fallback). +PRAGMA foreign_keys=OFF; +BEGIN; +-- Try DROP COLUMN if supported +-- If this fails in older SQLite, the application-level fallback in src/db.rs should rebuild (see code comment). +ALTER TABLE disputes DROP COLUMN buyer_token; +ALTER TABLE disputes DROP COLUMN seller_token; +COMMIT; +PRAGMA foreign_keys=ON;Optional: declare a foreign key to orders and reflect SQLite semantics by using TEXT instead of varchar(10), since SQLite doesn’t enforce length.
- order_id char(36) unique not null, - status varchar(10) not null, - order_previous_status varchar(10) not null, + order_id char(36) unique not null, + status TEXT not null, + order_previous_status TEXT not null, + -- Optionally: + -- FOREIGN KEY(order_id) REFERENCES orders(id) ON DELETE CASCADEsrc/app/admin_take_dispute.rs (2)
151-154: Use DisputeStatus instead of Order StatusThese fields/tags represent dispute state; using Status (order) is confusing and error-prone. Prefer DisputeStatus to avoid type/semantic drift.
- dispute.status = Status::InProgress.to_string(); + dispute.status = DisputeStatus::InProgress.to_string(); @@ - vec![Status::InProgress.to_string()], + vec![DisputeStatus::InProgress.to_string()],Also applies to: 219-233
98-101: Fix Keys API usageLikely a compile error: Keys exposes public_key() (method), not a public field.
- my_keys.public_key.to_string(), + my_keys.public_key().to_string(),
🧹 Nitpick comments (4)
src/lightning/invoice.rs (1)
288-291: Zero-amount invoice test: keep the stronger assertion tooUsing regtest invoice + None is fine. To preserve the regression check from past learnings (zero-amount must pass even when an expected amount is supplied), add a second assertion that passes Some(amount) and still succeeds.
// Add alongside the existing test #[tokio::test] async fn test_zero_amount_invoice_with_expected_amount() { init_settings_test(); let payment_request = "lnbcrt1p5tnk83pp5lqj87uf3ct7e4kluu3zcv3zvav9exwkj7chmpyt3u29g2p7uf6hqdqqcqzzsxqyz5vqsp5mrhkqtp7j8uhfzmcn6wdhj577jm87dvudnf28kefhzwwwh7zp7vq9qxpqysgq7ntan3nl38574gjrjne68740uymnc5xf39yryl9w7y2mcq9p3j53rwquhxukpjzclq4p8qmt89z7r4hmzzjkgj889ujgz0lr55c5gyqqeu4g52".to_string(); let res = is_valid_invoice(payment_request, Some(10_000), None).await; assert_eq!(Ok(()), res); }src/db.rs (1)
449-454: Run under a transaction and guard against cross-process racesFor existing DBs, consider acquiring an immediate transaction before migration to avoid TOCTOU if multiple instances start simultaneously and attempt the drop/rebuild concurrently.
-// Run legacy column migration for existing databases -if let Err(e) = migrate_remove_token_columns(&conn).await { +// Run legacy column migration under transactional guard +if let Err(e) = migrate_remove_token_columns(&conn).await { tracing::error!("Failed to migrate token columns on existing database: {}", e); return Err(e); }Note: with the refactor above, migrate_remove_token_columns itself begins/commits the transaction.
src/app/dispute.rs (2)
107-108: Typo in commentMinor clarity fix.
- // Message to discounterpart + // Message to counterpart
69-80: Docstring doesn’t match function signature/behaviorFunction returns only a boolean (buyer initiated) or an error, not a tuple/photos of counterpart.
-/// Returns a tuple containing: -/// - The counterparty's public key as a String -/// - A boolean indicating if the dispute was initiated by the buyer (true) or seller (false) +/// Returns: +/// - Ok(true) if the dispute was initiated by the buyer +/// - Ok(false) if initiated by the seller +/// - Err(CantDoReason::InvalidPubkey) if the sender matches neither party
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
⛔ Files ignored due to path filters (1)
Cargo.lockis excluded by!**/*.lock
📒 Files selected for processing (8)
Cargo.toml(2 hunks)migrations/20230928145530_disputes.sql(1 hunks)src/app/admin_take_dispute.rs(1 hunks)src/app/dispute.rs(4 hunks)src/db.rs(3 hunks)src/flow.rs(0 hunks)src/lightning/invoice.rs(1 hunks)src/util.rs(2 hunks)
💤 Files with no reviewable changes (1)
- src/flow.rs
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-07-17T13:51:17.338Z
Learnt from: arkanoider
PR: MostroP2P/mostro#499
File: src/lightning/invoice.rs:108-114
Timestamp: 2025-07-17T13:51:17.338Z
Learning: In Lightning invoice validation for Mostro buy orders (src/lightning/invoice.rs), zero-amount invoices (amount_sat == 0) should always pass validation even when an expected amount is provided. This is because users can submit invoices at order creation time before knowing the exact satoshi amount they'll receive. The condition `if amount_sat != res && amount_sat != 0` correctly implements this by allowing zero-amount invoices to pass while enforcing exact amount matching for non-zero invoices.
Applied to files:
src/lightning/invoice.rs
🔇 Additional comments (6)
Cargo.toml (2)
65-65: mostro-core bump: no lingering token references
Only occurrences ofbuyer_token/seller_tokenare insrc/db.rsmigration logic (dropping deprecated columns), and allPayload::Dispute(…)calls use the updated two-argument signature. No further changes needed.
43-43: Approve Nostr SDK bump: no leftover APIs and NIP-59 usage confirmed
All old nostr-sdk APIs (Client::new, Options::, set_opts) have been replaced with ClientOptions and ClientBuilder (e.g. src/util.rs:614–617). NIP-59 feature is intentionally enabled and actively used across multiple modules (UnwrappedGift imports and nip59::extract_rumor calls).src/db.rs (1)
356-369: Good: early runtime migration on new DB path with cleanupRunning the legacy column migration right after sqlx migrations and cleaning up the file on failure is solid. No change requested.
src/util.rs (1)
614-614: No remainingOptions::newusages
A ripgrep scan (rg -nP '\bOptions::new\s*\(' -g '!**/target/**') returned no matches; migration toClientOptions::new()is complete.src/app/admin_take_dispute.rs (1)
173-174: Adaptation to new Payload::Dispute arity looks correctUsing Payload::Dispute(dispute.id, Some(dispute_info)) matches the tokenless, 2-field variant.
src/app/dispute.rs (1)
100-127: Tokenless notifications: LGTMBoth user-facing messages now use Payload::Dispute(dispute.id, None); aligns with removing per-party tokens.
- Update mostro-core to latest version removing buyer_token and seller_token - Remove deprecated token columns from disputes migration - Add automatic migration function to drop legacy columns from existing databases - Ensure backward compatibility with existing database installations - Update related dispute handling code to work with new schema
… SQLite compatibility - Add transaction-based column dropping for atomic operations - Implement fallback table rebuild strategy for older SQLite versions - Add SQLite version detection to choose optimal migration method - Create rebuild_disputes_table_without_tokens() helper function - Ensure backward compatibility across all SQLite versions - Add comprehensive error handling with proper rollback mechanisms - Improve logging to indicate which migration strategy is used
- Update get_counterpart_info function documentation for clarity - Fix typo in notify_dispute_to_users comment (discounterpart -> counterpart) - Enhance code readability and maintainability
Summary by CodeRabbit
New Features
Refactor
Tests
Chores