summaryrefslogtreecommitdiffstats
path: root/src/db/snapshots.rs
blob: e9e1e12a2cdd76e3985537887ab3ecdae298f562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use rusqlite::{params, OptionalExtension};

use crate::db::{Db, Writer};
use crate::error::Result;
use crate::time;

#[allow(clippy::too_many_arguments)]
pub fn put_snapshot(
    writer: &Writer,
    pkgbase_id: i64,
    aur_commit: &str,
    recipe_hash: &str,
    srcinfo_json: &str,
    files_json: &str,
) -> Result<i64> {
    let (c, rh, si, fj) =
        (aur_commit.to_string(), recipe_hash.to_string(), srcinfo_json.to_string(), files_json.to_string());
    let at = time::now();
    writer.tx(move |tx| {
        tx.execute(
            "INSERT INTO snapshot (pkgbase_id, aur_commit, recipe_hash, srcinfo_json, files_json, fetched_at)
             VALUES (?1,?2,?3,?4,?5,?6)
             ON CONFLICT(pkgbase_id, aur_commit) DO NOTHING",
            params![pkgbase_id, c, rh, si, fj, at],
        )?;
        tx.query_row("SELECT id FROM snapshot WHERE pkgbase_id=?1 AND aur_commit=?2", params![pkgbase_id, c], |r| {
            r.get(0)
        })
        .map_err(Into::into)
    })
}

#[derive(Debug, Clone)]
pub struct SnapshotRow {
    pub id: i64,
    pub aur_commit: String,
    pub recipe_hash: String,
    pub srcinfo_json: String,
}

/// Find a snapshot by the commit it was taken at.
///
/// Test-tier only: detection looks snapshots up by id, and nothing in the
/// crate performs a lookup by commit.
#[cfg(feature = "test-support")]
pub fn snapshot_by_commit(db: &Db, pkgbase_id: i64, commit: &str) -> Result<Option<SnapshotRow>> {
    db.read(|c| {
        c.query_row(
            "SELECT id, aur_commit, recipe_hash, srcinfo_json FROM snapshot
             WHERE pkgbase_id=?1 AND aur_commit=?2",
            params![pkgbase_id, commit],
            |r| {
                Ok(SnapshotRow {
                    id: r.get(0)?,
                    aur_commit: r.get(1)?,
                    recipe_hash: r.get(2)?,
                    srcinfo_json: r.get(3)?,
                })
            },
        )
        .optional()
        .map_err(Into::into)
    })
}

pub fn snapshot_by_id(db: &Db, id: i64) -> Result<Option<SnapshotRow>> {
    db.read(|c| {
        c.query_row("SELECT id, aur_commit, recipe_hash, srcinfo_json FROM snapshot WHERE id=?1", params![id], |r| {
            Ok(SnapshotRow { id: r.get(0)?, aur_commit: r.get(1)?, recipe_hash: r.get(2)?, srcinfo_json: r.get(3)? })
        })
        .optional()
        .map_err(Into::into)
    })
}