Skip to content

Commit 7c3e205

Browse files
Export just last month of data to sqlite
This is much less than all of the data, which means it runs much faster and generates a significantly smaller sqlite file. Most of the time people using these exports probably don't care about the full dataset either.
1 parent fc58dfc commit 7c3e205

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ use std::time::Instant;
55
use tokio_postgres::types::Type;
66
use tokio_postgres::Row;
77

8+
const ARTIFACT_JOIN_AND_WHERE: &str =
9+
"join artifact on artifact.id = aid where artifact.date > (CURRENT_TIMESTAMP - interval '4 week')";
10+
const ARTIFACT_WHERE: &str = "where artifact.date > (CURRENT_TIMESTAMP - interval '4 week')";
11+
812
trait Table {
913
fn name() -> &'static str;
1014
fn copy_out() -> &'static str;
1115
fn insert() -> &'static str;
1216
fn types() -> &'static [Type];
1317
fn execute(statement: &mut rusqlite::Statement, row: Row);
18+
fn trailer() -> &'static str {
19+
""
20+
}
1421
}
1522

1623
struct PstatSeries;
@@ -51,6 +58,9 @@ impl Table for Pstat {
5158
fn insert() -> &'static str {
5259
"insert into pstat (series, aid, cid, value) VALUES (?, ?, ?, ?)"
5360
}
61+
fn trailer() -> &'static str {
62+
ARTIFACT_JOIN_AND_WHERE
63+
}
5464
fn types() -> &'static [Type] {
5565
&[Type::INT4, Type::INT2, Type::INT4, Type::FLOAT8]
5666
}
@@ -144,6 +154,9 @@ impl Table for Artifact {
144154
fn insert() -> &'static str {
145155
"insert into artifact (id, name, date, type) VALUES (?, ?, ?, ?)"
146156
}
157+
fn trailer() -> &'static str {
158+
ARTIFACT_WHERE
159+
}
147160
fn types() -> &'static [Type] {
148161
&[Type::INT2, Type::TEXT, Type::TIMESTAMPTZ, Type::TEXT]
149162
}
@@ -217,6 +230,9 @@ impl Table for SelfProfileQuery {
217230
fn insert() -> &'static str {
218231
"insert into self_profile_query (series, aid, cid, self_time, blocked_time, incremental_load_time, number_of_cache_hits, invocation_count) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
219232
}
233+
fn trailer() -> &'static str {
234+
ARTIFACT_JOIN_AND_WHERE
235+
}
220236
fn types() -> &'static [Type] {
221237
&[
222238
Type::INT4,
@@ -287,7 +303,13 @@ async fn copy<R: Table>(
287303
let mut prepared = sqlite.prepare(R::insert()).unwrap();
288304
let rows = postgres
289305
.query_raw(
290-
format!("select {} from {}", R::copy_out(), R::name()).as_str(),
306+
format!(
307+
"select {} from {}{}",
308+
R::copy_out(),
309+
R::name(),
310+
R::trailer()
311+
)
312+
.as_str(),
291313
vec![],
292314
)
293315
.await

0 commit comments

Comments
 (0)