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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
use rusqlite::{params, params_from_iter, OptionalExtension};
use crate::db::Db;
use crate::error::Result;
pub fn artifact_path(db: &Db, id: i64) -> Result<Option<String>> {
db.read(|c| {
Ok(c.query_row("SELECT path FROM artifact WHERE id=?1", params![id], |r| r.get::<_, String>(0)).optional()?)
})
}
fn artifact_id_by_digest_in(tx: &rusqlite::Transaction<'_>, digest: &str) -> Result<Option<i64>> {
Ok(tx.query_row("SELECT id FROM artifact WHERE digest=?1", params![digest], |r| r.get::<_, i64>(0)).optional()?)
}
pub fn accepted_pointer_in(tx: &rusqlite::Transaction<'_>, pkgbase_id: i64) -> Result<Option<i64>> {
tx.query_row("SELECT current_accepted_release_id FROM pkgbase WHERE id=?1", params![pkgbase_id], |r| {
r.get::<_, Option<i64>>(0)
})
.optional()
.map(Option::flatten)
.map_err(Into::into)
}
pub fn cas_accepted_release_in(
tx: &rusqlite::Transaction<'_>,
pkgbase_id: i64,
new_release_id: i64,
expected: Option<i64>,
) -> Result<bool> {
let moved = tx.execute(
"UPDATE pkgbase SET current_accepted_release_id=?1 WHERE id=?2 AND current_accepted_release_id IS ?3",
params![new_release_id, pkgbase_id, expected],
)?;
Ok(moved == 1)
}
pub fn remove_index_packages_in(tx: &rusqlite::Transaction<'_>, names: &[String]) -> Result<()> {
if names.is_empty() {
return Ok(());
}
let ph = names.iter().map(|_| "?").collect::<Vec<_>>().join(",");
let sql = format!("UPDATE package SET state='removed' WHERE state='in_index' AND name IN ({ph})");
let mut stmt = tx.prepare(&sql)?;
stmt.execute(params_from_iter(names.iter().map(|n| n as &dyn rusqlite::ToSql)))?;
Ok(())
}
pub fn remove_package_by_name_in(tx: &rusqlite::Transaction<'_>, repo_id: i64, name: &str) -> Result<()> {
tx.execute("UPDATE package SET state='removed' WHERE repo_id=?1 AND name=?2", params![repo_id, name])?;
Ok(())
}
pub struct NewRelease<'a> {
pub pkgbase_id: i64,
pub candidate_id: Option<i64>,
pub approval_id: Option<i64>,
pub aur_commit: &'a str,
pub recipe_hash: &'a str,
pub upstream_pins_json: &'a str,
pub source_digests_json: &'a str,
pub epoch: &'a str,
pub pkgver: &'a str,
pub pkgrel: &'a str,
pub world_snapshot_id: Option<i64>,
pub build_id: &'a str,
}
pub fn insert_release_in(tx: &mut rusqlite::Transaction<'_>, r: &NewRelease<'_>, at: &str) -> Result<i64> {
tx.execute(
"INSERT INTO release (pkgbase_id, candidate_id, approval_id, aur_commit, recipe_hash,
upstream_pins_json, source_digests_json, epoch, pkgver, pkgrel,
world_snapshot_id, state, build_id, published_at, created_at)
VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,'published',?12,?13,?13)",
params![
r.pkgbase_id,
r.candidate_id,
r.approval_id,
r.aur_commit,
r.recipe_hash,
r.upstream_pins_json,
r.source_digests_json,
r.epoch,
r.pkgver,
r.pkgrel,
r.world_snapshot_id,
r.build_id,
at
],
)?;
Ok(tx.last_insert_rowid())
}
pub fn insert_package_in(
tx: &rusqlite::Transaction<'_>,
repo_id: i64,
release_id: i64,
name: &str,
arch: &str,
version: &str,
digest: &str,
) -> Result<()> {
let artifact_id = artifact_id_by_digest_in(tx, digest)?;
tx.execute(
"INSERT INTO package (repo_id, release_id, name, arch, version, artifact_id, state)
VALUES (?1,?2,?3,?4,?5,?6,'in_index')",
params![repo_id, release_id, name, arch, version, artifact_id],
)?;
Ok(())
}
|