postgres via sqlx - workable?
This commit is contained in:
@@ -1,40 +1,37 @@
|
||||
use rusqlite::OptionalExtension;
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
database,
|
||||
logs::{LogAction, LogEntry},
|
||||
users::{User, UserError},
|
||||
};
|
||||
|
||||
pub fn initialise_reserved_users_if_needed() -> Result<(), UserError> {
|
||||
let mut conn = database::conn()?;
|
||||
let tx = conn.transaction()?;
|
||||
pub async fn initialise_reserved_users_if_needed(pool: &PgPool) -> Result<(), UserError> {
|
||||
let mut tx = pool.begin().await?;
|
||||
|
||||
if tx
|
||||
.prepare("SELECT handle FROM users WHERE id = ?1")?
|
||||
.query_one((&Uuid::nil(),), |_| Ok(()))
|
||||
.optional()?
|
||||
.is_none()
|
||||
{
|
||||
let u = User::create_systemuser(&tx)?;
|
||||
LogEntry::new(&tx, u, LogAction::Initialize)?;
|
||||
let systemuser_exists = sqlx::query("SELECT handle FROM users WHERE id = $1")
|
||||
.bind(Uuid::nil())
|
||||
.fetch_optional(&mut *tx)
|
||||
.await?
|
||||
.is_some();
|
||||
|
||||
if !systemuser_exists {
|
||||
let u = User::create_systemuser(&mut *tx).await?;
|
||||
LogEntry::new(&mut *tx, u, LogAction::Initialize).await?;
|
||||
}
|
||||
|
||||
if tx
|
||||
.prepare("SELECT handle FROM users WHERE id = ?1")?
|
||||
.query_one((&Uuid::max(),), |_| Ok(()))
|
||||
.optional()?
|
||||
.is_none()
|
||||
{
|
||||
User::create_infradmin(&tx)?;
|
||||
LogEntry::new(
|
||||
&tx,
|
||||
User::get_by_id(&tx, Uuid::nil())?,
|
||||
LogAction::RegenInfradmin,
|
||||
)?;
|
||||
let infradmin_exists = sqlx::query("SELECT handle FROM users WHERE id = $1")
|
||||
.bind(Uuid::max())
|
||||
.fetch_optional(&mut *tx)
|
||||
.await?
|
||||
.is_some();
|
||||
|
||||
if !infradmin_exists {
|
||||
User::create_infradmin(&mut *tx).await?;
|
||||
let u = User::get_by_id(&mut *tx, Uuid::max()).await?;
|
||||
LogEntry::new(&mut *tx, u, LogAction::RegenInfradmin).await?;
|
||||
}
|
||||
|
||||
tx.commit()?;
|
||||
tx.commit().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user