Skip to content

Commit 229bc65

Browse files
Merge pull request #1641 from Kobzol/db-error
Change how errors are stored in the DB
2 parents 825994d + df61f2a commit 229bc65

File tree

4 files changed

+54
-110
lines changed

4 files changed

+54
-110
lines changed

database/src/bin/postgres-to-sqlite.rs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ impl Table for Error {
191191
}
192192

193193
fn postgres_select_statement(&self, since_weeks_ago: Option<u32>) -> String {
194-
let s = "select series, aid, error from ".to_string() + self.name();
194+
let s = "select benchmark, aid, error from ".to_string() + self.name();
195195
with_filter_clause_maybe(s, ARTIFACT_JOIN_AND_WHERE, since_weeks_ago)
196196
}
197197

198198
fn sqlite_insert_statement(&self) -> &'static str {
199-
"insert into error (series, aid, error) VALUES (?, ?, ?)"
199+
"insert into error (benchmark, aid, error) VALUES (?, ?, ?)"
200200
}
201201

202202
fn sqlite_execute_insert(&self, statement: &mut rusqlite::Statement, row: tokio_postgres::Row) {
@@ -210,28 +210,6 @@ impl Table for Error {
210210
}
211211
}
212212

213-
struct ErrorSeries;
214-
215-
impl Table for ErrorSeries {
216-
fn name(&self) -> &'static str {
217-
"error_series"
218-
}
219-
220-
fn postgres_select_statement(&self, _since_weeks_ago: Option<u32>) -> String {
221-
"select id, crate from ".to_string() + self.name()
222-
}
223-
224-
fn sqlite_insert_statement(&self) -> &'static str {
225-
"insert into error_series (id, crate) VALUES (?, ?)"
226-
}
227-
228-
fn sqlite_execute_insert(&self, statement: &mut rusqlite::Statement, row: tokio_postgres::Row) {
229-
statement
230-
.execute(params![row.get::<_, i32>(0), row.get::<_, &str>(1)])
231-
.unwrap();
232-
}
233-
}
234-
235213
struct Pstat;
236214

237215
impl Table for Pstat {
@@ -450,7 +428,6 @@ async fn main() -> anyhow::Result<()> {
450428
&Benchmark,
451429
&Collection,
452430
&CollectorProgress,
453-
&ErrorSeries,
454431
&Error,
455432
&PstatSeries,
456433
&Pstat,

database/src/bin/sqlite-to-postgres.rs

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ struct Error;
218218

219219
#[derive(Serialize)]
220220
struct ErrorRow<'a> {
221-
series: i32,
221+
benchmark: &'a str,
222222
aid: i32,
223223
error: Nullable<&'a str>,
224224
}
@@ -229,7 +229,7 @@ impl Table for Error {
229229
}
230230

231231
fn sqlite_attributes() -> &'static str {
232-
"series, aid, error"
232+
"benchmark, aid, error"
233233
}
234234

235235
fn postgres_generated_id_attribute() -> Option<&'static str> {
@@ -239,45 +239,14 @@ impl Table for Error {
239239
fn write_postgres_csv_row<W: Write>(writer: &mut csv::Writer<W>, row: &rusqlite::Row) {
240240
writer
241241
.serialize(ErrorRow {
242-
series: row.get(0).unwrap(),
242+
benchmark: row.get_ref(0).unwrap().as_str().unwrap(),
243243
aid: row.get(1).unwrap(),
244244
error: row.get_ref(2).unwrap().try_into().unwrap(),
245245
})
246246
.unwrap();
247247
}
248248
}
249249

250-
struct ErrorSeries;
251-
252-
#[derive(Serialize)]
253-
struct ErrorSeriesRow<'a> {
254-
id: i32,
255-
krate: &'a str,
256-
}
257-
258-
impl Table for ErrorSeries {
259-
fn name() -> &'static str {
260-
"error_series"
261-
}
262-
263-
fn sqlite_attributes() -> &'static str {
264-
"id, crate"
265-
}
266-
267-
fn postgres_generated_id_attribute() -> Option<&'static str> {
268-
Some("id")
269-
}
270-
271-
fn write_postgres_csv_row<W: Write>(writer: &mut csv::Writer<W>, row: &rusqlite::Row) {
272-
writer
273-
.serialize(ErrorSeriesRow {
274-
id: row.get(0).unwrap(),
275-
krate: row.get_ref(1).unwrap().as_str().unwrap(),
276-
})
277-
.unwrap();
278-
}
279-
}
280-
281250
struct Pstat;
282251

283252
#[derive(Serialize)]
@@ -664,7 +633,6 @@ async fn main() -> anyhow::Result<()> {
664633
copy::<Benchmark>(&sqlite_tx, &postgres_tx).await;
665634
copy::<Collection>(&sqlite_tx, &postgres_tx).await;
666635
copy::<CollectorProgress>(&sqlite_tx, &postgres_tx).await;
667-
copy::<ErrorSeries>(&sqlite_tx, &postgres_tx).await;
668636
copy::<Error>(&sqlite_tx, &postgres_tx).await;
669637
copy::<PstatSeries>(&sqlite_tx, &postgres_tx).await;
670638
copy::<Pstat>(&sqlite_tx, &postgres_tx).await;

database/src/pool/postgres.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,22 @@ static MIGRATIONS: &[&str] = &[
229229
value double precision not null,
230230
PRIMARY KEY(series, aid, cid)
231231
);
232+
"#,
233+
r#"
234+
create table error_new(
235+
aid integer not null references artifact(id) on delete cascade on update cascade,
236+
benchmark text not null,
237+
error text not null,
238+
primary key(aid, benchmark)
239+
);
240+
insert into error_new(aid, benchmark, error)
241+
select aid, crate, error
242+
from error
243+
join error_series es on error.series = es.id;
244+
245+
drop table error;
246+
drop table error_series;
247+
alter table error_new rename to error;
232248
"#,
233249
];
234250

@@ -368,7 +384,7 @@ impl PostgresConnection {
368384
PostgresConnection {
369385
statements: Arc::new(CachedStatements {
370386
get_pstat: conn
371-
.prepare("
387+
.prepare("
372388
WITH aids AS (
373389
select aid, num from unnest($2::int[]) with ordinality aids(aid, num)
374390
),
@@ -432,8 +448,7 @@ impl PostgresConnection {
432448
)
433449
.await
434450
.unwrap(),
435-
get_error: conn.prepare("select crate, error from error_series
436-
inner join error on error.series = error_series.id and aid = $1").await.unwrap(),
451+
get_error: conn.prepare("select benchmark, error from error where aid = $1").await.unwrap(),
437452
select_self_query_series: conn.prepare("select id from self_profile_query_series where crate = $1 and profile = $2 and cache = $3 and query = $4").await.unwrap(),
438453
insert_self_query_series: conn.prepare("insert into self_profile_query_series (crate, profile, cache, query) VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING RETURNING id").await.unwrap(),
439454
insert_pstat_series: conn.prepare("insert into pstat_series (crate, profile, cache, statistic) VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING RETURNING id").await.unwrap(),
@@ -700,7 +715,7 @@ where
700715
&[&(pr as i32), &include, &exclude, &runs],
701716
)
702717
.await {
703-
log::error!("failed to queue_pr({}, {:?}, {:?}, {:?}): {:?}", pr, include, exclude, runs, e);
718+
log::error!("failed to queue_pr({}, {:?}, {:?}, {:?}): {:?}", pr, include, exclude, runs, e);
704719
}
705720
}
706721
async fn pr_attach_commit(
@@ -907,13 +922,13 @@ where
907922
Some(aid) => aid.get::<_, i32>(0) as u32,
908923
None => {
909924
self.conn()
910-
.query_opt("insert into artifact (name, date, type) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING RETURNING id", &[
911-
&name,
912-
&date,
913-
&ty,
914-
])
915-
.await
916-
.unwrap();
925+
.query_opt("insert into artifact (name, date, type) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING RETURNING id", &[
926+
&name,
927+
&date,
928+
&ty,
929+
])
930+
.await
931+
.unwrap();
917932
self.conn()
918933
.query_one("select id from artifact where name = $1", &[&name])
919934
.await
@@ -984,27 +999,10 @@ where
984999
}
9851000

9861001
async fn record_error(&self, artifact: ArtifactIdNumber, krate: &str, error: &str) {
987-
let sid = self
988-
.conn()
989-
.query_opt(
990-
"insert into error_series (crate) VALUES ($1) ON CONFLICT DO NOTHING RETURNING id",
991-
&[&krate],
992-
)
993-
.await
994-
.unwrap();
995-
let sid: i32 = match sid {
996-
Some(id) => id.get(0),
997-
None => self
998-
.conn()
999-
.query_one("select id from error_series where crate = $1", &[&krate])
1000-
.await
1001-
.unwrap()
1002-
.get(0),
1003-
};
10041002
self.conn()
10051003
.execute(
1006-
"insert into error (series, aid, error) VALUES ($1, $2, $3)",
1007-
&[&sid, &(artifact.0 as i32), &error],
1004+
"insert into error (benchmark, aid, error) VALUES ($1, $2, $3)",
1005+
&[&krate, &(artifact.0 as i32), &error],
10081006
)
10091007
.await
10101008
.unwrap();

database/src/pool/sqlite.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,24 @@ static MIGRATIONS: &[Migration] = &[
339339
);
340340
"#,
341341
),
342+
Migration::new(
343+
r#"
344+
create table error_new(
345+
aid integer references artifact(id) on delete cascade on update cascade,
346+
benchmark text not null,
347+
error text not null,
348+
primary key(aid, benchmark)
349+
);
350+
insert into error_new(aid, benchmark, error)
351+
select aid, crate, error
352+
from error
353+
join error_series es on error.series = es.id;
354+
355+
drop table error;
356+
drop table error_series;
357+
alter table error_new rename to error;
358+
"#,
359+
),
342360
];
343361

344362
#[async_trait::async_trait]
@@ -748,22 +766,8 @@ impl Connection for SqliteConnection {
748766
async fn record_error(&self, artifact: ArtifactIdNumber, krate: &str, error: &str) {
749767
self.raw_ref()
750768
.execute(
751-
"insert or ignore into error_series (crate) VALUES (?)",
752-
params![&krate,],
753-
)
754-
.unwrap();
755-
let sid: i32 = self
756-
.raw_ref()
757-
.query_row(
758-
"select id from error_series where crate = ?",
759-
params![&krate,],
760-
|r| r.get(0),
761-
)
762-
.unwrap();
763-
self.raw_ref()
764-
.execute(
765-
"insert into error (series, aid, error) VALUES (?, ?, ?)",
766-
params![&sid, &artifact.0, &error],
769+
"insert into error (benchmark, aid, error) VALUES (?, ?, ?)",
770+
params![krate, &artifact.0, &error],
767771
)
768772
.unwrap();
769773
}
@@ -907,10 +911,7 @@ impl Connection for SqliteConnection {
907911
}
908912
async fn get_error(&self, aid: crate::ArtifactIdNumber) -> HashMap<String, String> {
909913
self.raw_ref()
910-
.prepare_cached(
911-
"select crate, error from error_series
912-
inner join error on error.series = error_series.id and aid = ?",
913-
)
914+
.prepare_cached("select benchmark, error from error where aid = ?")
914915
.unwrap()
915916
.query_map(params![&aid.0], |row| Ok((row.get(0)?, row.get(1)?)))
916917
.unwrap()

0 commit comments

Comments
 (0)