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};
|
2023-07-01 22:42:01 +00:00
|
|
|
|
2023-07-05 23:26:20 +00:00
|
|
|
//-************************************************************************
|
|
|
|
// 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
|
|
|
|
2023-07-01 22:42:01 +00:00
|
|
|
//-************************************************************************
|
|
|
|
// utility functions for building CLI tools, currently just for benchmarking
|
|
|
|
//-************************************************************************
|
2023-07-08 20:16:05 +00:00
|
|
|
|
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()
|
2023-07-05 23:26:20 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2024-04-07 21:00:56 +00:00
|
|
|
.bind(&star.born)
|
|
|
|
.bind(&star.died)
|
2024-01-17 04:21:38 +00:00
|
|
|
.fetch_one(db)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2023-07-06 19:24:25 +00:00
|
|
|
}
|
|
|
|
|
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 (?, ?, ?)";
|
2023-07-05 23:26:20 +00:00
|
|
|
|
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
|
2024-02-04 21:09:26 +00:00
|
|
|
.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-05 23:26:20 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 23:15:27 +00:00
|
|
|
pub async fn ensure_omega(db_pool: &SqlitePool) -> Julid {
|
2023-07-01 22:42:01 +00:00
|
|
|
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-01 22:42:01 +00:00
|
|
|
}
|
2023-07-28 23:15:27 +00:00
|
|
|
OMEGA_ID
|
2023-07-01 22:42:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
2023-07-01 22:42:01 +00:00
|
|
|
let count = query_scalar(USER_EXISTS_QUERY)
|
2023-07-29 19:27:12 +00:00
|
|
|
.bind(OMEGA_ID)
|
2023-07-01 22:42:01 +00:00
|
|
|
.fetch_one(db_pool)
|
|
|
|
.await
|
|
|
|
.unwrap_or(0);
|
|
|
|
count > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
//-************************************************************************
|
|
|
|
//tests
|
|
|
|
//-************************************************************************
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2023-07-21 22:15:47 +00:00
|
|
|
use tokio::runtime::Runtime;
|
|
|
|
|
2023-07-01 22:42:01 +00:00
|
|
|
use super::*;
|
|
|
|
|
2023-07-21 22:15:47 +00:00
|
|
|
#[test]
|
|
|
|
fn ensure_omega_user() {
|
2023-07-19 00:37:24 +00:00
|
|
|
let p = crate::db::get_db_pool();
|
2023-07-21 22:15:47 +00:00
|
|
|
let rt = Runtime::new().unwrap();
|
|
|
|
rt.block_on(async {
|
2024-01-07 00:54:11 +00:00
|
|
|
dbg!("checking omega");
|
2023-07-21 22:15:47 +00:00
|
|
|
assert!(!check_omega_exists(&p).await);
|
2024-01-07 00:54:11 +00:00
|
|
|
dbg!("no omega");
|
2023-07-21 22:15:47 +00:00
|
|
|
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)));
|
2023-07-01 22:42:01 +00:00
|
|
|
}
|
|
|
|
}
|