feat: use a transaction to bulk insert teh data.
This commit is contained in:
parent
b714571b85
commit
cd710e5df7
1 changed files with 70 additions and 57 deletions
|
@ -1,6 +1,6 @@
|
||||||
use ldap3::{LdapConn, Scope, SearchEntry};
|
use ldap3::{LdapConn, Scope, SearchEntry};
|
||||||
use skynet_ldap_backend::{db_init, get_config, AccountWolves, Accounts, Config};
|
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 std::collections::HashMap;
|
||||||
use wolves_oxidised::WolvesUser;
|
use wolves_oxidised::WolvesUser;
|
||||||
|
|
||||||
|
@ -22,8 +22,13 @@ async fn main() -> tide::Result<()> {
|
||||||
|
|
||||||
async fn update_wolves(config: &Config, db: &Pool<Sqlite>) {
|
async fn update_wolves(config: &Config, db: &Pool<Sqlite>) {
|
||||||
let wolves = wolves_oxidised::Client::new(&config.wolves_url, None);
|
let wolves = wolves_oxidised::Client::new(&config.wolves_url, None);
|
||||||
for account in wolves.get_members(&config.wolves_key).await {
|
if let Ok(mut transaction) = db.begin().await {
|
||||||
update_account(db, &account).await;
|
for account in wolves.get_members(&config.wolves_key).await {
|
||||||
|
update_account(&mut transaction, &account).await;
|
||||||
|
}
|
||||||
|
if let Err(e) = transaction.commit().await {
|
||||||
|
dbg!(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,65 +108,73 @@ async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
|
||||||
vec!["uid", "uidNumber", "skDiscord", "skMemberOf", "mail", "skID", "userPassword"],
|
vec!["uid", "uidNumber", "skDiscord", "skMemberOf", "mail", "skID", "userPassword"],
|
||||||
) {
|
) {
|
||||||
if let Ok((rs, _res)) = x.success() {
|
if let Ok((rs, _res)) = x.success() {
|
||||||
for entry in rs {
|
// start transaction here
|
||||||
let tmp = SearchEntry::construct(entry);
|
if let Ok(mut transaction) = db.begin().await {
|
||||||
|
for entry in rs {
|
||||||
|
let tmp = SearchEntry::construct(entry);
|
||||||
|
|
||||||
let mut tmp_account = Accounts {
|
let mut tmp_account = Accounts {
|
||||||
user: "".to_string(),
|
user: "".to_string(),
|
||||||
uid: 0,
|
uid: 0,
|
||||||
mail: "".to_string(),
|
mail: "".to_string(),
|
||||||
student_id: "".to_string(),
|
student_id: "".to_string(),
|
||||||
secure: false,
|
secure: false,
|
||||||
id_wolves: 0,
|
id_wolves: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
// pull out the required info
|
// pull out the required info
|
||||||
if tmp.attrs.contains_key("uid") && !tmp.attrs["uid"].is_empty() {
|
if tmp.attrs.contains_key("uid") && !tmp.attrs["uid"].is_empty() {
|
||||||
tmp_account.user = tmp.attrs["uid"][0].clone();
|
tmp_account.user = tmp.attrs["uid"][0].clone();
|
||||||
}
|
}
|
||||||
if tmp.attrs.contains_key("uidNumber") && !tmp.attrs["uidNumber"].is_empty() {
|
if tmp.attrs.contains_key("uidNumber") && !tmp.attrs["uidNumber"].is_empty() {
|
||||||
tmp_account.uid = tmp.attrs["uidNumber"][0].clone().parse().unwrap_or(0);
|
tmp_account.uid = tmp.attrs["uidNumber"][0].clone().parse().unwrap_or(0);
|
||||||
}
|
}
|
||||||
if tmp.attrs.contains_key("mail") && !tmp.attrs["mail"].is_empty() {
|
if tmp.attrs.contains_key("mail") && !tmp.attrs["mail"].is_empty() {
|
||||||
tmp_account.mail = tmp.attrs["mail"][0].clone();
|
tmp_account.mail = tmp.attrs["mail"][0].clone();
|
||||||
}
|
}
|
||||||
if tmp.attrs.contains_key("skID") && !tmp.attrs["skID"].is_empty() {
|
if tmp.attrs.contains_key("skID") && !tmp.attrs["skID"].is_empty() {
|
||||||
tmp_account.student_id = tmp.attrs["skID"][0].clone();
|
tmp_account.student_id = tmp.attrs["skID"][0].clone();
|
||||||
}
|
}
|
||||||
if tmp.attrs.contains_key("userPassword") && !tmp.attrs["userPassword"].is_empty() {
|
if tmp.attrs.contains_key("userPassword") && !tmp.attrs["userPassword"].is_empty() {
|
||||||
tmp_account.secure = tmp.attrs["userPassword"][0].starts_with("{SSHA512}")
|
tmp_account.secure = tmp.attrs["userPassword"][0].starts_with("{SSHA512}")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !tmp_account.user.is_empty() {
|
if !tmp_account.user.is_empty() {
|
||||||
match existing_accounts.get(&tmp_account.user) {
|
match existing_accounts.get(&tmp_account.user) {
|
||||||
None => {
|
None => {
|
||||||
update_accounts(db, &tmp_account).await;
|
update_accounts(&mut transaction, &tmp_account).await;
|
||||||
}
|
}
|
||||||
Some(old) => {
|
Some(old) => {
|
||||||
let mut differnt = false;
|
let mut differnt = false;
|
||||||
|
|
||||||
if old.mail != tmp_account.mail {
|
if old.mail != tmp_account.mail {
|
||||||
differnt = true;
|
differnt = true;
|
||||||
}
|
}
|
||||||
if old.secure != tmp_account.secure {
|
if old.secure != tmp_account.secure {
|
||||||
differnt = true;
|
differnt = true;
|
||||||
}
|
}
|
||||||
if old.uid != tmp_account.uid {
|
if old.uid != tmp_account.uid {
|
||||||
differnt = true;
|
differnt = true;
|
||||||
}
|
}
|
||||||
if old.student_id != tmp_account.student_id {
|
if old.student_id != tmp_account.student_id {
|
||||||
differnt = true;
|
differnt = true;
|
||||||
}
|
}
|
||||||
if old.id_wolves != tmp_account.id_wolves {
|
if old.id_wolves != tmp_account.id_wolves {
|
||||||
differnt = true;
|
differnt = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if differnt {
|
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();
|
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>(
|
sqlx::query_as::<_, Accounts>(
|
||||||
"
|
"
|
||||||
INSERT OR REPLACE INTO accounts (user, uid, mail, student_id, secure)
|
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.mail)
|
||||||
.bind(&tmp_account.student_id)
|
.bind(&tmp_account.student_id)
|
||||||
.bind(tmp_account.secure)
|
.bind(tmp_account.secure)
|
||||||
.fetch_optional(db)
|
.fetch_optional(&mut **transaction)
|
||||||
.await
|
.await
|
||||||
.ok();
|
.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_account(db: &Pool<Sqlite>, account: &WolvesUser) {
|
async fn update_account(transaction: &mut Transaction<'_, Sqlite>, account: &WolvesUser) {
|
||||||
sqlx::query_as::<_, AccountWolves>(
|
sqlx::query_as::<_, AccountWolves>(
|
||||||
"
|
"
|
||||||
INSERT OR REPLACE INTO accounts_wolves (id_wolves, id_student, email, expiry, name_first, name_second)
|
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.expiry)
|
||||||
.bind(&account.first_name)
|
.bind(&account.first_name)
|
||||||
.bind(&account.last_name)
|
.bind(&account.last_name)
|
||||||
.fetch_optional(db)
|
.fetch_optional(&mut **transaction)
|
||||||
.await
|
.await
|
||||||
.ok();
|
.ok();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue