• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

payjoin / rust-payjoin / 17267118353

27 Aug 2025 12:46PM UTC coverage: 85.708% (-0.1%) from 85.806%
17267118353

Pull #873

github

web-flow
Merge 114eb89f3 into b2d7b40ce
Pull Request #873: Migrate payjoin-cli from sled to rusqlite

151 of 176 new or added lines in 3 files covered. (85.8%)

1 existing line in 1 file now uncovered.

7976 of 9306 relevant lines covered (85.71%)

502.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.54
/payjoin-cli/src/db/mod.rs
1
use std::path::Path;
2

3
use payjoin::bitcoin::consensus::encode::serialize;
4
use payjoin::bitcoin::OutPoint;
5
use r2d2::Pool;
6
use r2d2_sqlite::SqliteConnectionManager;
7
use rusqlite::{params, Connection};
8

9
pub(crate) mod error;
10
use error::*;
11

12
#[inline]
13
pub(crate) fn now() -> i64 {
19✔
14
    std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as i64
19✔
15
}
19✔
16

17
pub(crate) const DB_PATH: &str = "payjoin.sqlite";
18

19
pub(crate) struct Database(Pool<SqliteConnectionManager>);
20

21
impl Database {
22
    pub(crate) fn create(path: impl AsRef<Path>) -> Result<Self> {
12✔
23
        let manager = SqliteConnectionManager::file(path.as_ref());
12✔
24
        let pool = Pool::new(manager)?;
12✔
25

26
        // Initialize database schema
27
        let conn = pool.get()?;
12✔
28
        Self::init_schema(&conn)?;
12✔
29

30
        Ok(Self(pool))
12✔
31
    }
12✔
32

33
    fn init_schema(conn: &Connection) -> Result<()> {
12✔
34
        // Enable foreign keys
35
        conn.execute("PRAGMA foreign_keys = ON", [])?;
12✔
36

37
        conn.execute(
12✔
38
            "CREATE TABLE IF NOT EXISTS send_sessions (
12✔
39
                session_id INTEGER PRIMARY KEY AUTOINCREMENT,
12✔
40
                completed_at INTEGER
12✔
41
            )",
12✔
42
            [],
12✔
NEW
43
        )?;
×
44

45
        conn.execute(
12✔
46
            "CREATE TABLE IF NOT EXISTS receive_sessions (
12✔
47
                session_id INTEGER PRIMARY KEY AUTOINCREMENT,
12✔
48
                completed_at INTEGER
12✔
49
            )",
12✔
50
            [],
12✔
NEW
51
        )?;
×
52

53
        conn.execute(
12✔
54
            "CREATE TABLE IF NOT EXISTS send_session_events (
12✔
55
                id INTEGER PRIMARY KEY AUTOINCREMENT,
12✔
56
                session_id INTEGER NOT NULL,
12✔
57
                event_data TEXT NOT NULL,
12✔
58
                created_at INTEGER NOT NULL,
12✔
59
                FOREIGN KEY(session_id) REFERENCES send_sessions(session_id)
12✔
60
            )",
12✔
61
            [],
12✔
NEW
62
        )?;
×
63

64
        conn.execute(
12✔
65
            "CREATE TABLE IF NOT EXISTS receive_session_events (
12✔
66
                id INTEGER PRIMARY KEY AUTOINCREMENT,
12✔
67
                session_id INTEGER NOT NULL,
12✔
68
                event_data TEXT NOT NULL,
12✔
69
                created_at INTEGER NOT NULL,
12✔
70
                FOREIGN KEY(session_id) REFERENCES receive_sessions(session_id)
12✔
71
            )",
12✔
72
            [],
12✔
NEW
73
        )?;
×
74

75
        conn.execute(
12✔
76
            "CREATE TABLE IF NOT EXISTS inputs_seen (
12✔
77
                outpoint BLOB PRIMARY KEY,
12✔
78
                created_at INTEGER NOT NULL
12✔
79
            )",
12✔
80
            [],
12✔
NEW
81
        )?;
×
82

83
        Ok(())
12✔
84
    }
12✔
85

86
    pub(crate) fn get_connection(&self) -> Result<r2d2::PooledConnection<SqliteConnectionManager>> {
32✔
87
        Ok(self.0.get()?)
32✔
88
    }
32✔
89
    /// Inserts the input and returns true if the input was seen before, false otherwise.
90
    pub(crate) fn insert_input_seen_before(&self, input: OutPoint) -> Result<bool> {
4✔
91
        let conn = self.get_connection()?;
4✔
92
        let key = serialize(&input);
4✔
93

94
        let was_seen_before = conn.execute(
4✔
95
            "INSERT OR IGNORE INTO inputs_seen (outpoint, created_at) VALUES (?1, ?2)",
4✔
96
            params![key, now()],
4✔
97
        )? == 0;
4✔
98

99
        Ok(was_seen_before)
4✔
100
    }
4✔
101
}
102

103
#[cfg(feature = "v2")]
104
pub(crate) mod v2;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc