what2watch/src/import_utils.rs

107 lines
3.3 KiB
Rust
Raw Normal View History

2023-07-28 23:15:27 +00:00
use julid::Julid;
2024-01-27 22:38:50 +00:00
use sqlx::{query_scalar, SqliteConnection, SqlitePool};
2023-07-06 15:48:42 +00:00
2024-01-17 04:21:38 +00:00
use crate::{Credit, Star, User, Watch};
//-************************************************************************
// the omega user is the system ID, but has no actual power in the app
//-************************************************************************
2023-07-28 23:15:27 +00:00
const OMEGA_ID: Julid = Julid::omega();
2023-07-04 18:30:42 +00:00
//-************************************************************************
// utility functions for building CLI tools, currently just for benchmarking
//-************************************************************************
2024-01-27 22:38:50 +00:00
pub async fn insert_watch(watch: Watch, db: &mut SqliteConnection) -> Julid {
2024-01-17 04:21:38 +00:00
let q = "insert into watches (kind, title, length, release_date, added_by, metadata_url) values (?, ?, ?, ?, ?, ?) returning id";
sqlx::query_scalar(q)
.bind(watch.kind)
.bind(watch.title)
.bind(watch.length)
.bind(watch.release_date)
.bind(watch.added_by)
.bind(watch.metadata_url)
.fetch_one(db)
.await
.unwrap()
}
2024-01-27 22:38:50 +00:00
pub async fn insert_star(star: &Star, db: &mut SqliteConnection) -> Julid {
2024-01-17 04:21:38 +00:00
let q = "insert into stars (name, metadata_url, born, died) values (?, ?, ?, ?) returning id";
sqlx::query_scalar(q)
.bind(&star.name)
.bind(&star.metadata_url)
.bind(star.born)
.bind(star.died)
.fetch_one(db)
.await
.unwrap()
}
2024-01-27 22:38:50 +00:00
pub async fn insert_credit(credit: &Credit, db: &mut SqliteConnection) {
2024-01-17 04:21:38 +00:00
let q = "insert into credits (star, watch, credit) values (?, ?, ?)";
2024-01-17 04:21:38 +00:00
sqlx::query(q)
.bind(credit.star)
.bind(credit.watch)
.bind(credit.credit.as_deref())
.execute(db)
2023-07-06 15:48:42 +00:00
.await
.map(|_| ())
.or_else(|e| match e {
sqlx::Error::Database(ref db) => {
let exit = db.code().unwrap_or_default().parse().unwrap_or(0u32);
// https://www.sqlite.org/rescode.html codes for unique constraint violations:
if exit == 2067 || exit == 1555 {
Ok(())
} else {
Err(e)
}
}
_ => Err(e),
})
2023-07-06 15:48:42 +00:00
.unwrap();
}
2023-07-28 23:15:27 +00:00
pub async fn ensure_omega(db_pool: &SqlitePool) -> Julid {
if !check_omega_exists(db_pool).await {
2024-01-07 00:54:11 +00:00
User::omega().try_insert(db_pool).await.unwrap();
}
2023-07-28 23:15:27 +00:00
OMEGA_ID
}
async fn check_omega_exists(db_pool: &SqlitePool) -> bool {
2024-01-17 04:21:38 +00:00
const USER_EXISTS_QUERY: &str = "select count(*) from users where id = $1";
let count = query_scalar(USER_EXISTS_QUERY)
2023-07-29 19:27:12 +00:00
.bind(OMEGA_ID)
.fetch_one(db_pool)
.await
.unwrap_or(0);
count > 0
}
//-************************************************************************
//tests
//-************************************************************************
#[cfg(test)]
mod test {
use tokio::runtime::Runtime;
use super::*;
#[test]
fn ensure_omega_user() {
let p = crate::db::get_db_pool();
let rt = Runtime::new().unwrap();
rt.block_on(async {
2024-01-07 00:54:11 +00:00
dbg!("checking omega");
assert!(!check_omega_exists(&p).await);
2024-01-07 00:54:11 +00:00
dbg!("no omega");
ensure_omega(&p).await;
});
2024-01-07 00:54:11 +00:00
dbg!("maybe omega");
2023-07-28 23:15:27 +00:00
assert!(rt.block_on(check_omega_exists(&p)));
}
}