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
75
76
77
78
79
80
81
82
83
84
85
86
|
use rusqlite::{params, OptionalExtension, Row};
use crate::db::{release_by_id, Db, ReleaseRow, Writer};
use crate::error::Result;
#[derive(Debug, Clone)]
pub struct Pkgbase {
pub id: i64,
pub name: String,
pub aur_url: String,
pub mirror_path: Option<String>,
pub aur_head: Option<String>,
pub current_accepted_release_id: Option<i64>,
pub state: String,
}
fn row_to_pkgbase(r: &Row<'_>) -> rusqlite::Result<Pkgbase> {
Ok(Pkgbase {
id: r.get(0)?,
name: r.get(1)?,
aur_url: r.get(2)?,
mirror_path: r.get(3)?,
aur_head: r.get(4)?,
current_accepted_release_id: r.get(5)?,
state: r.get(6)?,
})
}
const PKGBASE_COLS: &str = "id, name, aur_url, mirror_path, aur_head, \
current_accepted_release_id, state";
pub fn get_pkgbase(db: &Db, name: &str) -> Result<Option<Pkgbase>> {
db.read(|c| {
c.query_row(&format!("SELECT {PKGBASE_COLS} FROM pkgbase WHERE name = ?1"), params![name], row_to_pkgbase)
.optional()
.map_err(Into::into)
})
}
pub fn get_pkgbase_by_id(db: &Db, id: i64) -> Result<Option<Pkgbase>> {
db.read(|c| {
c.query_row(&format!("SELECT {PKGBASE_COLS} FROM pkgbase WHERE id = ?1"), params![id], row_to_pkgbase)
.optional()
.map_err(Into::into)
})
}
/// The release a pkgbase currently points at, if it points at one. This is a
/// pointer stored on the pkgbase row, so reading it is a persistence question,
/// not a review one — the watcher needs it to decide what a change is a change
/// *from*, and review must not be the only way to ask.
pub fn accepted_release_for(db: &Db, pb: &Pkgbase) -> Result<Option<ReleaseRow>> {
match pb.current_accepted_release_id {
Some(id) => release_by_id(db, id),
None => Ok(None),
}
}
pub fn set_aur_head(writer: &Writer, pkgbase_id: i64, head: &str, mirror: &str) -> Result<()> {
let (h, m) = (head.to_string(), mirror.to_string());
writer.tx(move |tx| {
tx.execute("UPDATE pkgbase SET aur_head=?1, mirror_path=?2 WHERE id=?3", params![h, m, pkgbase_id])?;
Ok(())
})
}
/// Compare-and-set the accepted-release pointer.
///
/// Test-tier only, and the reason is worth keeping next to it: production
/// performs this CAS inside a transaction through `cas_accepted_release_in`,
/// which is what `pipeline::publish` calls at publish.rs:208. This standalone
/// form takes a `Writer` and is called by nothing in the crate — `tests/db.rs`
/// alone pins that the pointer moves once and refuses the second time. The two
/// names differ by three characters, which is exactly how a name-match census
/// once credited this function with a production caller it never had.
#[cfg(feature = "test-support")]
pub fn cas_accepted_release(writer: &Writer, pkgbase_id: i64, expected: Option<i64>, new_id: i64) -> Result<bool> {
writer.tx(move |tx| {
let n = tx.execute(
"UPDATE pkgbase SET current_accepted_release_id=?1
WHERE id=?2 AND current_accepted_release_id IS ?3",
params![new_id, pkgbase_id, expected],
)?;
Ok(n == 1)
})
}
|