feat: use a transaction to bulk insert teh data.

This commit is contained in:
silver 2025-06-29 14:23:32 +01:00
parent b714571b85
commit cd710e5df7
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D

View file

@ -1,6 +1,6 @@
use ldap3::{LdapConn, Scope, SearchEntry};
use skynet_ldap_backend::{db_init, get_config, AccountWolves, Accounts, Config};
use sqlx::{Pool, Sqlite};
use sqlx::{Pool, Sqlite, Transaction};
use std::collections::HashMap;
use wolves_oxidised::WolvesUser;
@ -22,8 +22,13 @@ async fn main() -> tide::Result<()> {
async fn update_wolves(config: &Config, db: &Pool<Sqlite>) {
let wolves = wolves_oxidised::Client::new(&config.wolves_url, None);
if let Ok(mut transaction) = db.begin().await {
for account in wolves.get_members(&config.wolves_key).await {
update_account(db, &account).await;
update_account(&mut transaction, &account).await;
}
if let Err(e) = transaction.commit().await {
dbg!(e);
}
}
}
@ -103,6 +108,8 @@ async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
vec!["uid", "uidNumber", "skDiscord", "skMemberOf", "mail", "skID", "userPassword"],
) {
if let Ok((rs, _res)) = x.success() {
// start transaction here
if let Ok(mut transaction) = db.begin().await {
for entry in rs {
let tmp = SearchEntry::construct(entry);
@ -135,7 +142,7 @@ async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
if !tmp_account.user.is_empty() {
match existing_accounts.get(&tmp_account.user) {
None => {
update_accounts(db, &tmp_account).await;
update_accounts(&mut transaction, &tmp_account).await;
}
Some(old) => {
let mut differnt = false;
@ -157,12 +164,18 @@ async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
}
if differnt {
update_accounts(db, &tmp_account).await;
update_accounts(&mut transaction, &tmp_account).await;
}
}
}
}
}
// end transaction ehre
if let Err(e) = transaction.commit().await {
dbg!(e);
}
}
}
}
@ -170,7 +183,7 @@ async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
ldap.unbind().unwrap();
}
async fn update_accounts(db: &Pool<Sqlite>, tmp_account: &Accounts) {
async fn update_accounts(transaction: &mut Transaction<'_, Sqlite>, tmp_account: &Accounts) {
sqlx::query_as::<_, Accounts>(
"
INSERT OR REPLACE INTO accounts (user, uid, mail, student_id, secure)
@ -182,12 +195,12 @@ async fn update_accounts(db: &Pool<Sqlite>, tmp_account: &Accounts) {
.bind(&tmp_account.mail)
.bind(&tmp_account.student_id)
.bind(tmp_account.secure)
.fetch_optional(db)
.fetch_optional(&mut **transaction)
.await
.ok();
}
async fn update_account(db: &Pool<Sqlite>, account: &WolvesUser) {
async fn update_account(transaction: &mut Transaction<'_, Sqlite>, account: &WolvesUser) {
sqlx::query_as::<_, AccountWolves>(
"
INSERT OR REPLACE INTO accounts_wolves (id_wolves, id_student, email, expiry, name_first, name_second)
@ -200,7 +213,7 @@ async fn update_account(db: &Pool<Sqlite>, account: &WolvesUser) {
.bind(&account.expiry)
.bind(&account.first_name)
.bind(&account.last_name)
.fetch_optional(db)
.fetch_optional(&mut **transaction)
.await
.ok();
}