Skip to content

Commit 149897b

Browse files
authored
Merge pull request #1836 from s7tya/support-runtime-related-tables-migration
Support missing tables migration
2 parents a172930 + c4a7f5a commit 149897b

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed

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

+84
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,87 @@ impl Table for RustcCompilation {
358358
}
359359
}
360360

361+
struct RuntimePstat;
362+
363+
impl Table for RuntimePstat {
364+
fn name(&self) -> &'static str {
365+
"runtime_pstat"
366+
}
367+
368+
fn postgres_select_statement(&self, since_weeks_ago: Option<u32>) -> String {
369+
let s = "select series, aid, cid, value from ".to_string() + self.name();
370+
with_filter_clause_maybe(s, ARTIFACT_JOIN_AND_WHERE, since_weeks_ago)
371+
}
372+
373+
fn sqlite_insert_statement(&self) -> &'static str {
374+
"insert into runtime_pstat (series, aid, cid, value) VALUES (?, ?, ?, ?)"
375+
}
376+
377+
fn sqlite_execute_insert(&self, statement: &mut rusqlite::Statement, row: tokio_postgres::Row) {
378+
statement
379+
.execute(params![
380+
row.get::<_, i32>(0),
381+
row.get::<_, i32>(1),
382+
row.get::<_, i32>(2),
383+
row.get::<_, f64>(3),
384+
])
385+
.unwrap();
386+
}
387+
}
388+
389+
struct RuntimePstatSeries;
390+
391+
impl Table for RuntimePstatSeries {
392+
fn name(&self) -> &'static str {
393+
"runtime_pstat_series"
394+
}
395+
396+
fn postgres_select_statement(&self, _since_weeks_ago: Option<u32>) -> String {
397+
"select id, benchmark, metric from ".to_string() + self.name()
398+
}
399+
400+
fn sqlite_insert_statement(&self) -> &'static str {
401+
"insert into runtime_pstat_series (id, benchmark, metric) VALUES (?, ?, ?)"
402+
}
403+
404+
fn sqlite_execute_insert(&self, statement: &mut rusqlite::Statement, row: tokio_postgres::Row) {
405+
statement
406+
.execute(params![
407+
row.get::<_, i32>(0),
408+
row.get::<_, &str>(1),
409+
row.get::<_, &str>(2),
410+
])
411+
.unwrap();
412+
}
413+
}
414+
415+
struct ArtifactSize;
416+
417+
impl Table for ArtifactSize {
418+
fn name(&self) -> &'static str {
419+
"artifact_size"
420+
}
421+
422+
fn postgres_select_statement(&self, since_weeks_ago: Option<u32>) -> String {
423+
let s = "select aid, component, size from ".to_string() + self.name();
424+
with_filter_clause_maybe(s, ARTIFACT_JOIN_AND_WHERE, since_weeks_ago)
425+
}
426+
427+
fn sqlite_insert_statement(&self) -> &'static str {
428+
"insert into artifact_size (aid, component, size) VALUES (?, ?, ?)"
429+
}
430+
431+
fn sqlite_execute_insert(&self, statement: &mut rusqlite::Statement, row: tokio_postgres::Row) {
432+
statement
433+
.execute(params![
434+
row.get::<_, i32>(0),
435+
row.get::<_, &str>(1),
436+
row.get::<_, i32>(2),
437+
])
438+
.unwrap();
439+
}
440+
}
441+
361442
#[tokio::main]
362443
async fn main() -> anyhow::Result<()> {
363444
env_logger::init();
@@ -375,6 +456,9 @@ async fn main() -> anyhow::Result<()> {
375456
&PullRequestBuild,
376457
&RawSelfProfile,
377458
&RustcCompilation,
459+
&RuntimePstatSeries,
460+
&RuntimePstat,
461+
&ArtifactSize,
378462
];
379463

380464
let table_names: Vec<_> = tables.iter().map(|table| table.name()).collect();

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

+116
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,119 @@ impl Table for RustcCompilation {
492492
}
493493
}
494494

495+
struct RuntimePstat;
496+
497+
#[derive(Serialize)]
498+
struct RuntimePstatRow {
499+
series: i32,
500+
aid: i32,
501+
cid: i32,
502+
value: f64,
503+
}
504+
505+
impl Table for RuntimePstat {
506+
fn name() -> &'static str {
507+
"runtime_pstat"
508+
}
509+
510+
fn sqlite_attributes() -> &'static str {
511+
"series, aid, cid, value"
512+
}
513+
514+
fn postgres_attributes() -> &'static str {
515+
"series, aid, cid, value"
516+
}
517+
518+
fn postgres_generated_id_attribute() -> Option<&'static str> {
519+
None
520+
}
521+
522+
fn write_postgres_csv_row<W: Write>(writer: &mut csv::Writer<W>, row: &rusqlite::Row) {
523+
writer
524+
.serialize(PstatRow {
525+
series: row.get(0).unwrap(),
526+
aid: row.get(1).unwrap(),
527+
cid: row.get(2).unwrap(),
528+
value: row.get(3).unwrap(),
529+
})
530+
.unwrap();
531+
}
532+
}
533+
534+
struct RuntimePstatSeries;
535+
536+
#[derive(Serialize)]
537+
struct RuntimePstatSeriesRow<'a> {
538+
id: i32,
539+
benchmark: &'a str,
540+
metric: &'a str,
541+
}
542+
543+
impl Table for RuntimePstatSeries {
544+
fn name() -> &'static str {
545+
"runtime_pstat_series"
546+
}
547+
548+
fn sqlite_attributes() -> &'static str {
549+
"id, benchmark, metric"
550+
}
551+
552+
fn postgres_attributes() -> &'static str {
553+
"id, benchmark, metric"
554+
}
555+
556+
fn postgres_generated_id_attribute() -> Option<&'static str> {
557+
Some("id")
558+
}
559+
560+
fn write_postgres_csv_row<W: Write>(writer: &mut csv::Writer<W>, row: &rusqlite::Row) {
561+
writer
562+
.serialize(RuntimePstatSeriesRow {
563+
id: row.get(0).unwrap(),
564+
benchmark: row.get_ref(1).unwrap().as_str().unwrap(),
565+
metric: row.get_ref(2).unwrap().as_str().unwrap(),
566+
})
567+
.unwrap();
568+
}
569+
}
570+
571+
struct ArtifactSize;
572+
573+
#[derive(Serialize)]
574+
struct ArtifactSizeRow<'a> {
575+
aid: i32,
576+
component: &'a str,
577+
size: i32,
578+
}
579+
580+
impl Table for ArtifactSize {
581+
fn name() -> &'static str {
582+
"artifact_size"
583+
}
584+
585+
fn sqlite_attributes() -> &'static str {
586+
"aid, component, size"
587+
}
588+
589+
fn postgres_attributes() -> &'static str {
590+
"aid, component, size"
591+
}
592+
593+
fn postgres_generated_id_attribute() -> Option<&'static str> {
594+
None
595+
}
596+
597+
fn write_postgres_csv_row<W: Write>(writer: &mut csv::Writer<W>, row: &rusqlite::Row) {
598+
writer
599+
.serialize(ArtifactSizeRow {
600+
aid: row.get(0).unwrap(),
601+
component: row.get_ref(1).unwrap().as_str().unwrap(),
602+
size: row.get(2).unwrap(),
603+
})
604+
.unwrap();
605+
}
606+
}
607+
495608
// `Nullable<T>` helps to work around the fact that the `csv` crate (and the CSV
496609
// format in general) doesn't distinguish between nulls and empty strings, while
497610
// the Postgres CSV format does.
@@ -620,6 +733,9 @@ async fn main() -> anyhow::Result<()> {
620733
copy::<PullRequestBuild>(&sqlite_tx, &postgres_tx).await;
621734
copy::<RawSelfProfile>(&sqlite_tx, &postgres_tx).await;
622735
copy::<RustcCompilation>(&sqlite_tx, &postgres_tx).await;
736+
copy::<RuntimePstatSeries>(&sqlite_tx, &postgres_tx).await;
737+
copy::<RuntimePstat>(&sqlite_tx, &postgres_tx).await;
738+
copy::<ArtifactSize>(&sqlite_tx, &postgres_tx).await;
623739
enable_table_triggers(&postgres_tx, &tables).await;
624740

625741
// This is overly paranoid, but don't commit the Postgres transaction until

0 commit comments

Comments
 (0)