Try bulk insert with query builder
This commit is contained in:
+55
-45
@@ -1,7 +1,8 @@
|
||||
use chrono::{DateTime, DurationRound, NaiveDate, TimeDelta, Utc};
|
||||
use sqlx::{Connection, PgConnection};
|
||||
use sqlx::{Connection, PgConnection, Postgres, QueryBuilder};
|
||||
use toggl::TogglApi;
|
||||
use crate::toggl::types::{TimeEntry, TogglReportFilters};
|
||||
use itertools::Itertools;
|
||||
|
||||
mod toggl;
|
||||
mod sensitive;
|
||||
@@ -72,7 +73,7 @@ impl Worker {
|
||||
|
||||
pub async fn fetch_changed_since(&mut self, look_back: TimeDelta) -> Result<(), AppError> {
|
||||
if look_back > TimeDelta::days(90) {
|
||||
return Err(AppError::LookBackTooLarge)
|
||||
return Err(AppError::LookBackTooLarge);
|
||||
}
|
||||
|
||||
self.update_time_entries(Utc::now() - look_back).await
|
||||
@@ -287,36 +288,41 @@ impl Worker {
|
||||
async fn update_clients(&mut self) -> Result<(), AppError> {
|
||||
let clients = self.toggl_api.get_clients().await?;
|
||||
|
||||
for client in clients {
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO tracking_clients (id, updated_at, archived, creator_id, integration_provider, notes, name, server_deleted_at, workspace_id, permissions)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
updated_at = excluded.updated_at,
|
||||
archived = excluded.archived,
|
||||
creator_id = excluded.creator_id,
|
||||
integration_provider = excluded.integration_provider,
|
||||
notes = excluded.notes,
|
||||
name = excluded.name,
|
||||
server_deleted_at = excluded.server_deleted_at,
|
||||
workspace_id = excluded.workspace_id,
|
||||
permissions = excluded.permissions
|
||||
"#,
|
||||
client.id,
|
||||
client.updated_at,
|
||||
client.archived,
|
||||
client.creator_id,
|
||||
client.integration_provider,
|
||||
client.notes,
|
||||
client.name,
|
||||
client.server_deleted_at,
|
||||
client.workspace_id,
|
||||
client.permissions,
|
||||
)
|
||||
.execute(&mut self.db)
|
||||
.await?;
|
||||
}
|
||||
let mut query_builder: QueryBuilder<Postgres> = QueryBuilder::new(
|
||||
// Note the trailing space
|
||||
"INSERT INTO tracking_clients (id, updated_at, archived, creator_id, integration_provider, notes, name, server_deleted_at, workspace_id, permissions) "
|
||||
);
|
||||
|
||||
query_builder.push_values(clients.into_iter(), |mut b, client| {
|
||||
b.push_bind(client.id)
|
||||
.push_bind(client.updated_at)
|
||||
.push_bind(client.archived)
|
||||
.push_bind(client.creator_id)
|
||||
.push_bind(client.integration_provider)
|
||||
.push_bind(client.notes)
|
||||
.push_bind(client.name)
|
||||
.push_bind(client.server_deleted_at)
|
||||
.push_bind(client.workspace_id)
|
||||
.push_bind(client.permissions);
|
||||
});
|
||||
|
||||
query_builder.push(r#"
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
updated_at = excluded.updated_at,
|
||||
archived = excluded.archived,
|
||||
creator_id = excluded.creator_id,
|
||||
integration_provider = excluded.integration_provider,
|
||||
notes = excluded.notes,
|
||||
name = excluded.name,
|
||||
server_deleted_at = excluded.server_deleted_at,
|
||||
workspace_id = excluded.workspace_id,
|
||||
permissions = excluded.permissions
|
||||
"#);
|
||||
|
||||
let query = query_builder.build();
|
||||
|
||||
query.execute(&mut self.db)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -343,18 +349,22 @@ async fn main() {
|
||||
toggl_api: api,
|
||||
};
|
||||
|
||||
let start = NaiveDate::from_ymd_opt(2024, 2, 1)
|
||||
.expect("Invalid date")
|
||||
.and_hms_opt(0, 0, 0)
|
||||
.expect("Invalid time")
|
||||
.and_utc();
|
||||
worker.update_clients()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let end = NaiveDate::from_ymd_opt(2024, 5, 1)
|
||||
.expect("Invalid date")
|
||||
.and_hms_opt(0, 0, 0)
|
||||
.expect("Invalid time")
|
||||
.and_utc();
|
||||
|
||||
worker.fetch_within(start, end).await
|
||||
.expect("Failed to fetch time entries");
|
||||
// let start = NaiveDate::from_ymd_opt(2024, 2, 1)
|
||||
// .expect("Invalid date")
|
||||
// .and_hms_opt(0, 0, 0)
|
||||
// .expect("Invalid time")
|
||||
// .and_utc();
|
||||
//
|
||||
// let end = NaiveDate::from_ymd_opt(2024, 5, 1)
|
||||
// .expect("Invalid date")
|
||||
// .and_hms_opt(0, 0, 0)
|
||||
// .expect("Invalid time")
|
||||
// .and_utc();
|
||||
//
|
||||
// worker.fetch_within(start, end).await
|
||||
// .expect("Failed to fetch time entries");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user