Skip to content

Commit

Permalink
Merge pull request #1999 from scpwiki/sqlx-ping
Browse files Browse the repository at this point in the history
Use SQLx in ping query
  • Loading branch information
emmiegit authored Jul 16, 2024
2 parents e02bf73 + 6129bb0 commit fddef4a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 10 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion deepwell/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion deepwell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ keywords = ["wikijump", "api", "backend", "wiki"]
categories = ["asynchronous", "database", "web-programming::http-server"]
exclude = [".gitignore", ".editorconfig"]

version = "2024.5.12"
version = "2024.7.15"
authors = ["Emmie Smith <[email protected]>"]
edition = "2021"

Expand Down
8 changes: 8 additions & 0 deletions deepwell/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ If you have [`sea-orm-cli`](https://www.sea-ql.org/SeaORM/docs/generate-entity/s
$ scripts/generate-models.sh
```

When adding SQLx queries, they need to be cached. After `source .env` (to set `DATABASE_URL`), run the following to update the query list:

```sh
$ cargo sqlx prepare
```

Then commit any new files into the branch.

#### Structure

The primary organization of the crate is as follows:
Expand Down
10 changes: 3 additions & 7 deletions deepwell/src/endpoints/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@

use super::prelude::*;
use crate::info;
use sea_orm::{ConnectionTrait, DatabaseBackend, Statement};
use std::path::PathBuf;
use wikidot_normalize::normalize;

async fn postgres_check(ctx: &ServiceContext<'_>) -> Result<()> {
ctx.transaction()
.execute(Statement::from_string(
DatabaseBackend::Postgres,
str!("SELECT 1"),
))
.await?;
let mut txn = ctx.sqlx().await?;
let _ = sqlx::query!(r"SELECT 1 AS x").fetch_one(&mut *txn).await?;
txn.commit().await?;

debug!("Successfully pinged Postgres");
Ok(())
Expand Down
6 changes: 6 additions & 0 deletions deepwell/src/services/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,10 @@ impl<'txn> ServiceContext<'txn> {
pub fn transaction(&self) -> &'txn DatabaseTransaction {
self.transaction
}

#[inline]
pub async fn sqlx(&self) -> Result<sqlx::Transaction<sqlx::Postgres>> {
let txn = self.state.database_sqlx.begin().await?;
Ok(txn)
}
}
5 changes: 4 additions & 1 deletion deepwell/src/services/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub enum Error {
#[error("Database error: {0}")]
Database(DbErr),

#[error("Database error: {0}")]
Database2(#[from] sqlx::Error),

#[error("Redis error: {0}")]
Redis(#[from] redis::RedisError),

Expand Down Expand Up @@ -340,7 +343,7 @@ impl Error {

// 3200 -- Backend issues
Error::Serde(_) => 3200,
Error::Database(_) => 3201,
Error::Database(_) | Error::Database2(_) => 3201,
Error::Cryptography(_) => 3202,
Error::Magic(_) => 3204,
Error::Otp(_) => 3205,
Expand Down

0 comments on commit fddef4a

Please sign in to comment.