From db56118bfb50b18843798da43a75e0f0858f8528 Mon Sep 17 00:00:00 2001 From: jude Date: Tue, 27 Oct 2020 17:35:01 +0000 Subject: [PATCH] changed to how the guild query works so that errors are more useful and it doesnt do dumb shit --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/models.rs | 57 +++++++++++++++++++++++++++++---------------------- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eaf4109..79c65b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1175,7 +1175,7 @@ dependencies = [ [[package]] name = "reminder_rs" -version = "1.1.4" +version = "1.1.5" dependencies = [ "Inflector", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index 976fe29..e96a436 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "reminder_rs" -version = "1.1.4" +version = "1.1.5" authors = ["jellywx "] edition = "2018" diff --git a/src/models.rs b/src/models.rs index 199f261..885c004 100644 --- a/src/models.rs +++ b/src/models.rs @@ -41,13 +41,10 @@ SELECT prefix FROM guilds WHERE guild = ? } } - pub async fn from_guild( - guild: Guild, - pool: &MySqlPool, - ) -> Result> { + pub async fn from_guild(guild: Guild, pool: &MySqlPool) -> Result { let guild_id = guild.id.as_u64().to_owned(); - if let Ok(g) = sqlx::query_as!( + match sqlx::query_as!( Self, " SELECT id, name, prefix FROM guilds WHERE guild = ? @@ -57,30 +54,40 @@ SELECT id, name, prefix FROM guilds WHERE guild = ? .fetch_one(pool) .await { - g.name = guild.name; + Ok(mut g) => { + g.name = Some(guild.name); - Ok(g) - } else { - sqlx::query!( - " + Ok(g) + } + + Err(sqlx::Error::RowNotFound) => { + sqlx::query!( + " INSERT INTO guilds (guild, name, prefix) VALUES (?, ?, ?) - ", - guild_id, - guild.name, - *DEFAULT_PREFIX - ) - .execute(&pool.clone()) - .await?; + ", + guild_id, + guild.name, + *DEFAULT_PREFIX + ) + .execute(&pool.clone()) + .await?; - Ok(sqlx::query_as!( - Self, - " + Ok(sqlx::query_as!( + Self, + " SELECT id, name, prefix FROM guilds WHERE guild = ? - ", - guild_id - ) - .fetch_one(pool) - .await?) + ", + guild_id + ) + .fetch_one(pool) + .await?) + } + + Err(e) => { + error!("Unexpected error in guild query: {:?}", e); + + Err(e) + } } }