Basic sqlx setup migrations and init

This commit is contained in:
2025-11-30 20:57:31 +01:00
parent e5d935ec85
commit 5c3c7a0e02
8 changed files with 53 additions and 5 deletions

View File

@@ -14,6 +14,7 @@ rand_core = { version = "0.6", features = ["getrandom"] }
regex = "1.12.2"
serde = { version = "1.0.228", features = ["derive", "serde_derive"] }
serde_json = "1.0.145"
sqlx = { version = "0.8.6", features = ["runtime-tokio-rustls", "postgres", "uuid", "chrono", "migrate"] }
tokio = { version = "1.48.0", features = ["macros", "rt-multi-thread"] }
tower = { version = "0.5.2", features = ["tokio", "tracing"] }
tower-http = { version = "0.6.7", features = ["cors"] }

View File

@@ -0,0 +1,7 @@
CREATE TABLE system_state (
id INTEGER PRIMARY KEY CHECK (id = 1),
initialized BOOLEAN NOT NULL DEFAULT false
);
INSERT INTO system_state (id, initialized) VALUES (1, true)
ON CONFLICT (id) DO NOTHING;

View File

@@ -1,5 +1,23 @@
use crate::prelude::*;
use std::sync::Arc;
use crate::{domain::user::NewUser, state::AppState};
use axum::http::StatusCode;
use validator::Validate;
pub async fn create(state: Arc<AppState>, new_user: NewUser) -> Result<User> {}
use crate::{
domain::user::{InternalNewUser, NewUser, User},
state::AppState,
};
pub async fn create(state: Arc<AppState>, new_user: NewUser) -> Result<User, StatusCode> {
if let Err(_) = new_user.validate() {
return Err(StatusCode::BAD_REQUEST);
}
let internal = InternalNewUser::try_from(new_user).map_err(|e| {
error!("Conversion to InternalUser failed: {e}");
StatusCode::INTERNAL_SERVER_ERROR
})?;
todo!("Hook up return once db setup ready");
}

View File

@@ -0,0 +1,20 @@
use std::time::Duration;
use sqlx::{PgPool, postgres::PgPoolOptions};
use anyhow::Result;
pub async fn connect(database_url: &str) -> Result<PgPool> {
let pool = PgPoolOptions::new()
.max_connections(10)
.acquire_timeout(Duration::from_secs(5))
.connect(database_url)
.await?;
Ok(pool)
}
pub async fn migrate(pool: &PgPool) -> Result<()> {
sqlx::migrate!().run(&pool).await?;
Ok(())
}

View File

@@ -0,0 +1 @@
pub mod db;

View File

@@ -1,5 +1,7 @@
pub mod auth;
pub mod core;
pub mod domain;
pub mod infra;
pub mod prelude;
pub mod router;
pub mod state;

View File

@@ -0,0 +1 @@
pub use tracing::{debug, error, info, warn};

View File

@@ -23,7 +23,5 @@ pub async fn create(
StatusCode::INTERNAL_SERVER_ERROR
})?;
let user = User::from(internal);
Ok(Json(user))
todo!("Hook up return once db setup ready");
}