Skip to content

Commit c49e3b2

Browse files
committed
Refactor getting artifact info to avoid duplication
1 parent 2822e8a commit c49e3b2

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

database/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,30 @@ impl From<Commit> for ArtifactId {
384384
}
385385
}
386386

387+
struct ArtifactInfo<'a> {
388+
name: &'a str,
389+
date: Option<DateTime<Utc>>,
390+
kind: &'static str,
391+
}
392+
393+
impl ArtifactId {
394+
fn info(&self) -> ArtifactInfo {
395+
let (name, date, ty) = match self {
396+
Self::Commit(commit) => (
397+
commit.sha.as_str(),
398+
Some(commit.date.0),
399+
if commit.is_try() { "try" } else { "master" },
400+
),
401+
Self::Tag(a) => (a.as_str(), None, "release"),
402+
};
403+
ArtifactInfo {
404+
name,
405+
date,
406+
kind: ty,
407+
}
408+
}
409+
}
410+
387411
intern!(pub struct QueryLabel);
388412

389413
#[derive(PartialEq, Eq, Clone, Debug)]

database/src/pool/postgres.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -948,33 +948,26 @@ where
948948
}
949949

950950
async fn artifact_id(&self, artifact: &ArtifactId) -> ArtifactIdNumber {
951-
let (name, date, ty) = match artifact {
952-
ArtifactId::Commit(commit) => (
953-
commit.sha.to_string(),
954-
Some(commit.date.0),
955-
if commit.is_try() { "try" } else { "master" },
956-
),
957-
ArtifactId::Tag(a) => (a.clone(), None, "release"),
958-
};
951+
let info = artifact.info();
959952
let aid = self
960953
.conn()
961-
.query_opt("select id from artifact where name = $1", &[&name])
954+
.query_opt("select id from artifact where name = $1", &[&info.name])
962955
.await
963956
.unwrap();
964957

965958
let aid = match aid {
966959
Some(aid) => aid.get::<_, i32>(0) as u32,
967960
None => {
968961
self.conn()
969-
.query_opt("insert into artifact (name, date, type) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING RETURNING id", &[
970-
&name,
971-
&date,
972-
&ty,
973-
])
974-
.await
975-
.unwrap();
962+
.query_opt("insert into artifact (name, date, type) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING RETURNING id", &[
963+
&info.name,
964+
&info.date,
965+
&info.kind,
966+
])
967+
.await
968+
.unwrap();
976969
self.conn()
977-
.query_one("select id from artifact where name = $1", &[&name])
970+
.query_one("select id from artifact where name = $1", &[&info.name])
978971
.await
979972
.unwrap()
980973
.get::<_, i32>(0) as u32

database/src/pool/sqlite.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -624,26 +624,19 @@ impl Connection for SqliteConnection {
624624
)
625625
}
626626
async fn artifact_id(&self, artifact: &crate::ArtifactId) -> ArtifactIdNumber {
627-
let (name, date, ty) = match artifact {
628-
crate::ArtifactId::Commit(commit) => (
629-
commit.sha.to_string(),
630-
Some(commit.date.0),
631-
if commit.is_try() { "try" } else { "master" },
632-
),
633-
crate::ArtifactId::Tag(a) => (a.clone(), None, "release"),
634-
};
627+
let info = artifact.info();
635628

636629
self.raw_ref()
637630
.execute(
638631
"insert or ignore into artifact (name, date, type) VALUES (?, ?, ?)",
639-
params![&name, &date.map(|d| d.timestamp()), &ty,],
632+
params![&info.name, &info.date.map(|d| d.timestamp()), &info.kind,],
640633
)
641634
.unwrap();
642635
ArtifactIdNumber(
643636
self.raw_ref()
644637
.query_row(
645638
"select id from artifact where name = $1",
646-
params![&name],
639+
params![&info.name],
647640
|r| r.get::<_, i32>(0),
648641
)
649642
.unwrap() as u32,

0 commit comments

Comments
 (0)