Skip to content

Commit

Permalink
chore:main sqlx migration 1 (#880)
Browse files Browse the repository at this point in the history
* tmp

* tm

* tmp

* testcases

* docker build
  • Loading branch information
CommanderStorm authored Nov 12, 2023
1 parent d1a5c77 commit 92682d3
Show file tree
Hide file tree
Showing 17 changed files with 291 additions and 136 deletions.

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

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

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

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

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

3 changes: 0 additions & 3 deletions server/Cargo.lock

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

1 change: 0 additions & 1 deletion server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ RUN cargo build --release --workspace \
&& rm -fr target/release/deps/navigatum*

# second run of the image build (including our code)
COPY main-api/diesel.toml ./main-api/diesel.toml
COPY calendar/diesel.toml ./calendar/diesel.toml
COPY calendar/migrations calendar/migrations
COPY .sqlx .sqlx
Expand Down
1 change: 0 additions & 1 deletion server/main-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ unicode-truncate = "0.2.0"
serde_yaml = "0.9"

# database
diesel = { version = "2.1.3", features = ["default", "sqlite"] }
libsqlite3-sys = { version = "*", features = ["bundled"] }
sqlx = { version = "0.7.2", features = ["sqlite", "runtime-tokio", "migrate", "macros"] }

Expand Down
8 changes: 0 additions & 8 deletions server/main-api/diesel.toml

This file was deleted.

59 changes: 31 additions & 28 deletions server/main-api/src/entries/get.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
use crate::models::DBRoomKeyAlias;
use crate::utils;
use actix_web::{get, web, HttpResponse};
use diesel::prelude::*;
use log::error;
use sqlx::SqlitePool;

#[get("/api/get/{id}")]
pub async fn get_handler(
params: web::Path<String>,
web::Query(args): web::Query<utils::LangQueryArgs>,
data: web::Data<crate::AppData>,
) -> HttpResponse {
let conn = &mut utils::establish_connection();
let (probable_id, redirect_url) = match get_alias_and_redirect(conn, &params.into_inner()) {
Some(alias_and_redirect) => alias_and_redirect,
None => return HttpResponse::NotFound().body("Not found"),
};
let (probable_id, redirect_url) =
match get_alias_and_redirect(&data.db, &params.into_inner()).await {
Some(alias_and_redirect) => alias_and_redirect,
None => return HttpResponse::NotFound().body("Not found"),
};
let result = match args.should_use_english() {
true => {
use crate::schema::en::dsl;
dsl::en
.filter(dsl::key.eq(&probable_id))
.select(dsl::data)
.load::<String>(conn)
sqlx::query_scalar!("SELECT data FROM en WHERE key = ?", probable_id)
.fetch_optional(&data.db)
.await
}
false => {
use crate::schema::de::dsl;
dsl::de
.filter(dsl::key.eq(&probable_id))
.select(dsl::data)
.load::<String>(conn)
sqlx::query_scalar!("SELECT data FROM de WHERE key = ?", probable_id)
.fetch_optional(&data.db)
.await
}
};
match result {
Ok(d) => match d.len() {
0 => HttpResponse::NotFound().body("Not found"),
_ => {
let mut response_json = d[0].clone();
Ok(d) => match d {
None => HttpResponse::NotFound().body("Not found"),
Some(d) => {
let mut response_json = d.clone();
// We don not want to serialise this data at any point in the server.
// This just flows through the server, but adding redirect_url to the response is necessary
response_json.pop(); // remove last }
Expand All @@ -53,18 +50,24 @@ pub async fn get_handler(
}
}

fn get_alias_and_redirect(conn: &mut SqliteConnection, query: &str) -> Option<(String, String)> {
use crate::schema::aliases::dsl::{alias, aliases, key, type_, visible_id};
let result = aliases
.filter(alias.eq(query).or(key.eq(query)))
.select((key, visible_id, type_))
.distinct()
.load::<DBRoomKeyAlias>(conn);
async fn get_alias_and_redirect(conn: &SqlitePool, query: &str) -> Option<(String, String)> {
let result = sqlx::query_as!(
DBRoomKeyAlias,
r#"
SELECT key, visible_id, type
FROM aliases
WHERE key = ? OR key = ?
"#,
query,
query
)
.fetch_all(conn)
.await;
match result {
Ok(d) => {
let redirect_url = match d.len() {
0 => return None, // not key or alias
1 => extract_redirect_exact_match(&d[0].type_, &d[0].visible_id),
1 => extract_redirect_exact_match(&d[0].r#type, &d[0].visible_id),
_ => {
let keys = d
.clone()
Expand Down
Loading

0 comments on commit 92682d3

Please sign in to comment.