Skip to content

Commit 81814a0

Browse files
committed
Update database interface & implement an extract_if method
1 parent 6ef954e commit 81814a0

File tree

6 files changed

+73
-152
lines changed

6 files changed

+73
-152
lines changed

database/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,28 @@ impl fmt::Display for BenchmarkRequestStatus {
816816
}
817817
}
818818

819+
impl<'a> tokio_postgres::types::FromSql<'a> for BenchmarkRequestStatus {
820+
fn from_sql(
821+
ty: &tokio_postgres::types::Type,
822+
raw: &'a [u8],
823+
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
824+
// Decode raw bytes into &str with Postgres' own text codec
825+
let s: &str = <&str as tokio_postgres::types::FromSql>::from_sql(ty, raw)?;
826+
827+
match s {
828+
"waiting_for_artifacts" => Ok(Self::WaitingForArtifacts),
829+
"artifacts_ready" => Ok(Self::ArtifactsReady),
830+
"in_progress" => Ok(Self::InProgress),
831+
"completed" => Ok(Self::Completed),
832+
other => Err(format!("unknown benchmark_request_status '{other}'").into()),
833+
}
834+
}
835+
836+
fn accepts(ty: &tokio_postgres::types::Type) -> bool {
837+
<&str as tokio_postgres::types::FromSql>::accepts(ty)
838+
}
839+
}
840+
819841
#[derive(Debug, Clone, PartialEq)]
820842
pub enum BenchmarkRequestType {
821843
/// A Try commit

database/src/pool.rs

Lines changed: 2 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ pub trait Connection: Send + Sync {
189189
async fn get_benchmark_requests_by_status(
190190
&self,
191191
statuses: &[BenchmarkRequestStatus],
192-
days: Option<i32>,
193192
) -> anyhow::Result<Vec<BenchmarkRequest>>;
194193

195194
/// Update the status of a `benchmark_request`
@@ -482,7 +481,7 @@ mod tests {
482481
.await;
483482

484483
let requests = db
485-
.get_benchmark_requests_by_status(&[BenchmarkRequestStatus::ArtifactsReady], None)
484+
.get_benchmark_requests_by_status(&[BenchmarkRequestStatus::ArtifactsReady])
486485
.await
487486
.unwrap();
488487

@@ -495,90 +494,6 @@ mod tests {
495494
.await;
496495
}
497496

498-
#[tokio::test]
499-
async fn get_benchmark_requests_by_status_and_date() {
500-
run_postgres_test(|ctx| async {
501-
let db = ctx.db_client();
502-
let old_created_at = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap();
503-
let created_at = Utc::now() - chrono::Duration::days(5);
504-
let cutoff_days = 6;
505-
506-
// The following three are the only three that should be returned
507-
// from the database query.
508-
let master_benchmark_request = BenchmarkRequest::create_master(
509-
"a-sha-1",
510-
"parent-sha-1",
511-
42,
512-
created_at,
513-
BenchmarkRequestStatus::ArtifactsReady,
514-
"llvm",
515-
"",
516-
);
517-
let master_benchmark_request_two = BenchmarkRequest::create_master(
518-
"a-sha-2",
519-
"parent-sha-1",
520-
42,
521-
created_at,
522-
BenchmarkRequestStatus::Completed,
523-
"llvm",
524-
"",
525-
);
526-
let master_benchmark_request_three = BenchmarkRequest::create_master(
527-
"a-sha-3",
528-
"parent-sha-3",
529-
42,
530-
created_at,
531-
BenchmarkRequestStatus::InProgress,
532-
"llvm",
533-
"",
534-
);
535-
536-
let try_benchmark_request = BenchmarkRequest::create_try(
537-
"b-sha-2",
538-
"parent-sha-2",
539-
32,
540-
old_created_at,
541-
BenchmarkRequestStatus::Completed,
542-
"cranelift",
543-
"",
544-
);
545-
546-
let release_benchmark_request = BenchmarkRequest::create_release(
547-
"1.8.0",
548-
old_created_at,
549-
BenchmarkRequestStatus::ArtifactsReady,
550-
"cranelift,llvm",
551-
"",
552-
);
553-
554-
let db = db.connection().await;
555-
db.insert_benchmark_request(&master_benchmark_request).await;
556-
db.insert_benchmark_request(&master_benchmark_request_two)
557-
.await;
558-
db.insert_benchmark_request(&master_benchmark_request_three)
559-
.await;
560-
db.insert_benchmark_request(&try_benchmark_request).await;
561-
db.insert_benchmark_request(&release_benchmark_request)
562-
.await;
563-
564-
let requests = db
565-
.get_benchmark_requests_by_status(
566-
&[
567-
BenchmarkRequestStatus::ArtifactsReady,
568-
BenchmarkRequestStatus::InProgress,
569-
BenchmarkRequestStatus::Completed,
570-
],
571-
Some(cutoff_days),
572-
)
573-
.await;
574-
575-
assert_eq!(requests?.len(), 3);
576-
577-
Ok(ctx)
578-
})
579-
.await;
580-
}
581-
582497
#[tokio::test]
583498
async fn update_benchmark_request_status() {
584499
// Insert one item into the database, change the status and then
@@ -607,7 +522,7 @@ mod tests {
607522
.unwrap();
608523

609524
let requests = db
610-
.get_benchmark_requests_by_status(&[BenchmarkRequestStatus::InProgress], None)
525+
.get_benchmark_requests_by_status(&[BenchmarkRequestStatus::InProgress])
611526
.await
612527
.unwrap();
613528

database/src/pool/postgres.rs

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::str::FromStr;
1313
use std::sync::Arc;
1414
use std::time::Duration;
1515
use tokio::sync::Mutex;
16-
use tokio_postgres::types::{FromSql, Type};
1716
use tokio_postgres::GenericClient;
1817
use tokio_postgres::Statement;
1918

@@ -1418,11 +1417,10 @@ where
14181417
async fn get_benchmark_requests_by_status(
14191418
&self,
14201419
statuses: &[BenchmarkRequestStatus],
1421-
days: Option<i32>,
14221420
) -> anyhow::Result<Vec<BenchmarkRequest>> {
14231421
// There is a small period of time where a try commit's parent_sha
14241422
// could be NULL, this query will filter that out.
1425-
let mut query = "
1423+
let query = "
14261424
SELECT
14271425
tag,
14281426
parent_sha,
@@ -1438,22 +1436,16 @@ where
14381436
AND (commit_type <> 'try' OR parent_sha IS NOT NULL) "
14391437
.to_string();
14401438

1441-
// Optionally add interval
1442-
let rows = if let Some(days) = days {
1443-
// postgres expects the interval to be a `float` so we cast with;
1444-
// `::INT`, we can't do; `INTERVAL '$2 day'` which, while looking
1445-
// natural is invalid.
1446-
query += "AND created_at > current_date - $2::INT * INTERVAL '1 day'";
1447-
self.conn().query(&query, &[&statuses, &days]).await
1448-
} else {
1449-
self.conn().query(&query, &[&statuses]).await
1450-
}
1451-
.context("Failed to get benchmark requests")?;
1439+
let rows = self
1440+
.conn()
1441+
.query(&query, &[&statuses])
1442+
.await
1443+
.context("Failed to get benchmark requests")?;
14521444

14531445
let benchmark_requests = rows
14541446
.iter()
14551447
.map(|row| {
1456-
let tag = row.get::<_, String>(0);
1448+
let tag = row.get::<_, &str>(0);
14571449
let parent_sha = row.get::<_, Option<String>>(1);
14581450
let pr = row.get::<_, Option<i32>>(2);
14591451
let commit_type = row.get::<_, String>(3);
@@ -1466,7 +1458,7 @@ where
14661458
match commit_type.as_str() {
14671459
"try" => {
14681460
let mut try_benchmark = BenchmarkRequest::create_try(
1469-
&tag,
1461+
tag,
14701462
&parent_sha.unwrap(),
14711463
pr.unwrap() as u32,
14721464
created_at,
@@ -1479,7 +1471,7 @@ where
14791471
}
14801472
"master" => {
14811473
let mut master_benchmark = BenchmarkRequest::create_master(
1482-
&tag,
1474+
tag,
14831475
&parent_sha.unwrap(),
14841476
pr.unwrap() as u32,
14851477
created_at,
@@ -1492,7 +1484,7 @@ where
14921484
}
14931485
"release" => {
14941486
let mut release_benchmark = BenchmarkRequest::create_release(
1495-
&tag, created_at, status, &backends, &profiles,
1487+
tag, created_at, status, &backends, &profiles,
14961488
);
14971489
release_benchmark.completed_at = completed_at;
14981490
release_benchmark
@@ -1575,28 +1567,6 @@ macro_rules! impl_to_postgresql_via_to_string {
15751567
impl_to_postgresql_via_to_string!(BenchmarkRequestType);
15761568
impl_to_postgresql_via_to_string!(BenchmarkRequestStatus);
15771569

1578-
impl<'a> FromSql<'a> for BenchmarkRequestStatus {
1579-
fn from_sql(
1580-
ty: &Type,
1581-
raw: &'a [u8],
1582-
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
1583-
// Decode raw bytes into &str with Postgres' own text codec
1584-
let s: &str = <&str as FromSql>::from_sql(ty, raw)?;
1585-
1586-
match s {
1587-
"waiting_for_artifacts" => Ok(Self::WaitingForArtifacts),
1588-
"artifacts_ready" => Ok(Self::ArtifactsReady),
1589-
"in_progress" => Ok(Self::InProgress),
1590-
"completed" => Ok(Self::Completed),
1591-
other => Err(format!("unknown benchmark_request_status '{other}'").into()),
1592-
}
1593-
}
1594-
1595-
fn accepts(ty: &Type) -> bool {
1596-
<&str as FromSql>::accepts(ty)
1597-
}
1598-
}
1599-
16001570
#[cfg(test)]
16011571
mod tests {
16021572
use super::make_certificates;

database/src/pool/sqlite.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1271,7 +1271,6 @@ impl Connection for SqliteConnection {
12711271
async fn get_benchmark_requests_by_status(
12721272
&self,
12731273
_statuses: &[BenchmarkRequestStatus],
1274-
_days: Option<i32>,
12751274
) -> anyhow::Result<Vec<BenchmarkRequest>> {
12761275
no_queue_implementation_abort!()
12771276
}

database/src/tests/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(dead_code)]
2+
13
use std::future::Future;
24
use tokio_postgres::config::Host;
35
use tokio_postgres::Config;
@@ -23,7 +25,6 @@ pub struct TestContext {
2325
}
2426

2527
impl TestContext {
26-
#[allow(dead_code)]
2728
async fn new_postgres(db_url: &str) -> Self {
2829
let config: Config = db_url.parse().expect("Cannot parse connection string");
2930

@@ -78,7 +79,6 @@ impl TestContext {
7879
}
7980
}
8081

81-
#[allow(dead_code)]
8282
async fn new_sqlite() -> Self {
8383
let pool = Pool::open(":memory:");
8484
Self {
@@ -87,12 +87,10 @@ impl TestContext {
8787
}
8888
}
8989

90-
#[allow(dead_code)]
9190
pub fn db_client(&self) -> &Pool {
9291
&self.client
9392
}
9493

95-
#[allow(dead_code)]
9694
async fn finish(self) {
9795
// Cleanup the test database
9896
// First, we need to stop using the database
@@ -118,7 +116,6 @@ impl TestContext {
118116
}
119117

120118
/// Runs a test against an actual postgres database.
121-
#[allow(dead_code)]
122119
pub async fn run_postgres_test<F, Fut>(f: F)
123120
where
124121
F: Fn(TestContext) -> Fut,
@@ -146,7 +143,6 @@ where
146143

147144
/// Runs a test against an actual database.
148145
/// Checks both Postgres and SQLite.
149-
#[allow(dead_code)]
150146
pub async fn run_db_test<F, Fut>(f: F)
151147
where
152148
F: Fn(TestContext) -> Fut + Clone,

0 commit comments

Comments
 (0)