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

payjoin / rust-payjoin / 16533244097

25 Jul 2025 11:11PM UTC coverage: 85.705% (-0.08%) from 85.783%
16533244097

Pull #873

github

web-flow
Merge c6a55118a into 5ecfed745
Pull Request #873: Migrate payjoin-cli from sled to rusqlite

152 of 175 new or added lines in 4 files covered. (86.86%)

1 existing line in 1 file now uncovered.

7920 of 9241 relevant lines covered (85.71%)

507.16 hits per line

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

92.31
/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
macro_rules! now {
13
    () => {
14
        std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as i64
15
    };
16
}
17

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

20
pub(crate) struct Database {
21
    pool: Pool<SqliteConnectionManager>,
22
}
23

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

29
        // Initialize database schema
30
        let conn = pool.get()?;
10✔
31
        Self::init_schema(&conn)?;
10✔
32

33
        Ok(Self { pool })
10✔
34
    }
10✔
35

36
    fn init_schema(conn: &Connection) -> Result<()> {
10✔
37
        // Enable foreign keys
38
        conn.execute("PRAGMA foreign_keys = ON", [])?;
10✔
39

40
        conn.execute(
10✔
41
            "CREATE TABLE IF NOT EXISTS send_sessions (
10✔
42
                session_id INTEGER PRIMARY KEY AUTOINCREMENT,
10✔
43
                completed_at INTEGER
10✔
44
            )",
10✔
45
            [],
10✔
NEW
46
        )?;
×
47

48
        conn.execute(
10✔
49
            "CREATE TABLE IF NOT EXISTS receive_sessions (
10✔
50
                session_id INTEGER PRIMARY KEY AUTOINCREMENT,
10✔
51
                completed_at INTEGER
10✔
52
            )",
10✔
53
            [],
10✔
NEW
54
        )?;
×
55

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

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

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

86
        Ok(())
10✔
87
    }
10✔
88

89
    pub(crate) fn get_connection(&self) -> Result<r2d2::PooledConnection<SqliteConnectionManager>> {
31✔
90
        Ok(self.pool.get()?)
31✔
91
    }
31✔
92
    /// Inserts the input and returns true if the input was seen before, false otherwise.
93
    pub(crate) fn insert_input_seen_before(&self, input: OutPoint) -> Result<bool> {
3✔
94
        let conn = self.get_connection()?;
3✔
95
        let key = serialize(&input);
3✔
96
        let timestamp = now!();
3✔
97

98
        let was_seen_before = conn.execute(
3✔
99
            "INSERT OR IGNORE INTO inputs_seen (outpoint, created_at) VALUES (?1, ?2)",
3✔
100
            params![key, timestamp],
3✔
101
        )? == 0;
3✔
102

103
        Ok(was_seen_before)
3✔
104
    }
3✔
105
}
106

107
#[cfg(feature = "v2")]
108
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