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

payjoin / rust-payjoin / 20240963871

15 Dec 2025 05:12PM UTC coverage: 83.307%. Remained the same
20240963871

Pull #1228

github

web-flow
Merge 238d849aa into 62c8f95a6
Pull Request #1228: flake: build dev profile instead of release

9657 of 11592 relevant lines covered (83.31%)

453.26 hits per line

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

92.65
/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
pub(crate) fn now() -> i64 {
21✔
13
    std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as i64
21✔
14
}
21✔
15

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

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

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

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

29
        Ok(Self(pool))
13✔
30
    }
13✔
31

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

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

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

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

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

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

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

86
    pub(crate) fn get_connection(&self) -> Result<r2d2::PooledConnection<SqliteConnectionManager>> {
37✔
87
        Ok(self.0.get()?)
37✔
88
    }
37✔
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