diff options
| author | Joe Carstairs <me@joeac.net> | 2024-10-22 07:46:43 +0100 |
|---|---|---|
| committer | Joe Carstairs <me@joeac.net> | 2024-10-22 07:46:43 +0100 |
| commit | 5824c353f978a5b9ab9696451ee3014c6a614ffc (patch) | |
| tree | bc39754ca417e84eb00b0aa9a1ae2330da0bf282 /backend/core/src/queries/accounts/insert.rs | |
| parent | 7adaace61a4fe983eba5ded3aaaf624ce6fb9014 (diff) | |
Refactors importer to use core stuff
Diffstat (limited to 'backend/core/src/queries/accounts/insert.rs')
| -rw-r--r-- | backend/core/src/queries/accounts/insert.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/backend/core/src/queries/accounts/insert.rs b/backend/core/src/queries/accounts/insert.rs new file mode 100644 index 0000000..7020b68 --- /dev/null +++ b/backend/core/src/queries/accounts/insert.rs @@ -0,0 +1,28 @@ +use anyhow::{Context, Result}; +use crate::{ + models::account::Account, + schema::accounts::dsl::accounts as accounts_table, +}; +use diesel::prelude::*; + +pub fn insert_accounts( + accounts: &[Account], + connection: &mut SqliteConnection, +) -> Result<usize> { + let num_accounts_inserted = diesel::insert_into(accounts_table) + .values(accounts) + .execute(connection) + .with_context(|| err_msg(accounts))?; + Ok(num_accounts_inserted) +} + +fn err_msg(accounts: &[Account]) -> String { + format!( + "failed to insert accounts: [{}]", + accounts + .iter() + .map(|a| format!("\"{}\": \"{}\"", a.id, a.name)) + .collect::<Vec<String>>() + .join(", ") + ) +} |
