Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SQLx in ping query #1999

Merged
merged 6 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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
Loading