Skip to content

Commit

Permalink
editoast: deprecate DbPool V1 and remove it from the AppState
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Valais <leo.valais97@gmail.com>
  • Loading branch information
leovalais committed Dec 3, 2024
1 parent 02ccfc8 commit 614ab93
Show file tree
Hide file tree
Showing 22 changed files with 52 additions and 83 deletions.
13 changes: 5 additions & 8 deletions editoast/editoast_models/src/db_connection_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ use url::Url;
use tokio::sync::OwnedRwLockWriteGuard;
use tokio::sync::RwLock;

use super::DbConnectionPool;
use super::DieselConnection;

pub type DbConnectionConfig = AsyncDieselConnectionManager<AsyncPgConnection>;

#[derive(Clone)]
Expand Down Expand Up @@ -110,7 +107,7 @@ impl DerefMut for WriteHandle {
///
/// # Testing pool
///
/// In test mode, the [DbConnectionPool::get] function will always return the same connection that has
/// In test mode, the [Pool::<AsyncPgConnection>::get] function will always return the same connection that has
/// been setup to drop all modification once the test ends.
/// Since this connection will not commit any changes to the database, we ensure the isolation of each test.
///
Expand Down Expand Up @@ -423,17 +420,17 @@ pub async fn ping_database(conn: &mut DbConnection) -> Result<(), PingError> {
Ok(())
}

pub fn create_connection_pool(
fn create_connection_pool(
url: Url,
max_size: usize,
) -> Result<DbConnectionPool, DatabasePoolBuildError> {
) -> Result<Pool<AsyncPgConnection>, DatabasePoolBuildError> {
let mut manager_config = ManagerConfig::default();
manager_config.custom_setup = Box::new(establish_connection);
let manager = DbConnectionConfig::new_with_config(url, manager_config);
Ok(Pool::builder(manager).max_size(max_size).build()?)
}

fn establish_connection(config: &str) -> BoxFuture<ConnectionResult<DieselConnection>> {
fn establish_connection(config: &str) -> BoxFuture<ConnectionResult<AsyncPgConnection>> {
let fut = async {
let mut connector_builder = SslConnector::builder(SslMethod::tls()).unwrap();
connector_builder.set_verify(SslVerifyMode::NONE);
Expand All @@ -448,7 +445,7 @@ fn establish_connection(config: &str) -> BoxFuture<ConnectionResult<DieselConnec
tracing::error!("connection error: {}", e);
}
});
DieselConnection::try_from(client).await
AsyncPgConnection::try_from(client).await
};
fut.boxed()
}
8 changes: 1 addition & 7 deletions editoast/editoast_models/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
use diesel_async::pooled_connection::deadpool::Pool;
use diesel_async::AsyncPgConnection;

pub mod db_connection_pool;
pub mod tables;

pub use db_connection_pool::DbConnection;
pub use db_connection_pool::DbConnectionPoolV2;

type DieselConnection = AsyncPgConnection;
pub type DbConnectionPool = Pool<DieselConnection>;

/// Generic error type to forward errors from the database
///
/// Useful for functions which only points of failure are the DB calls.
#[derive(Debug, thiserror::Error)]
#[error("an error occured while querying the database: {0}")]
#[error("an error occurred while querying the database: {0}")]
pub struct DatabaseError(#[from] diesel::result::Error);
2 changes: 1 addition & 1 deletion editoast/src/models/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ops::DerefMut;

use chrono::Utc;
use editoast_models::DbConnection;
use editoast_models::DbConnectionPool;

use editoast_models::DbConnectionPoolV2;
use editoast_schemas::infra::Direction;
use editoast_schemas::infra::DirectionalTrackRange;
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/views/infra/attached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async fn attached(
Path(InfraAttachedParams { infra_id, track_id }): Path<InfraAttachedParams>,
State(AppState {
infra_caches,
db_pool_v2: db_pool,
db_pool,
..
}): State<AppState>,
Extension(auth): AuthenticationExt,
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/views/infra/auto_fixes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async fn list_auto_fixes(
Path(infra_id): Path<i64>,
State(AppState {
infra_caches,
db_pool_v2: db_pool,
db_pool,
..
}): State<AppState>,
Extension(auth): AuthenticationExt,
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/views/infra/delimited_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async fn delimited_area(
Extension(auth): AuthenticationExt,
State(AppState {
infra_caches,
db_pool_v2: db_pool,
db_pool,
..
}): State<AppState>,
Path(InfraIdParam { infra_id }): Path<InfraIdParam>,
Expand Down
4 changes: 2 additions & 2 deletions editoast/src/views/infra/edition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ crate::routes! {
async fn edit<'a>(
Path(InfraIdParam { infra_id }): Path<InfraIdParam>,
State(AppState {
db_pool_v2: db_pool,
db_pool,
infra_caches,
valkey,
map_layers,
Expand Down Expand Up @@ -126,7 +126,7 @@ async fn edit<'a>(
pub async fn split_track_section<'a>(
Path(InfraIdParam { infra_id }): Path<InfraIdParam>,
State(AppState {
db_pool_v2: db_pool,
db_pool,
infra_caches,
valkey,
map_layers,
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/views/infra/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async fn get_line_bbox(
Path((infra_id, line_code)): Path<(i64, i64)>,
State(AppState {
infra_caches,
db_pool_v2: db_pool,
db_pool,
..
}): State<AppState>,
Extension(auth): AuthenticationExt,
Expand Down
12 changes: 6 additions & 6 deletions editoast/src/views/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async fn refresh(
return Err(AuthorizationError::Unauthorized.into());
}

let db_pool = app_state.db_pool_v2.clone();
let db_pool = app_state.db_pool.clone();
let valkey_client = app_state.valkey.clone();
let infra_caches = app_state.infra_caches.clone();
let map_layers = app_state.map_layers.clone();
Expand Down Expand Up @@ -212,7 +212,7 @@ async fn list(
if !authorized {
return Err(AuthorizationError::Unauthorized.into());
}
let db_pool = app_state.db_pool_v2.clone();
let db_pool = app_state.db_pool.clone();
let osrdyne_client = app_state.osrdyne_client.clone();

let settings = pagination_params
Expand Down Expand Up @@ -307,7 +307,7 @@ async fn get(
return Err(AuthorizationError::Unauthorized.into());
}

let db_pool = app_state.db_pool_v2.clone();
let db_pool = app_state.db_pool.clone();
let osrdyne_client = app_state.osrdyne_client.clone();

let infra_id = infra.infra_id;
Expand Down Expand Up @@ -433,7 +433,7 @@ async fn delete(
return Err(AuthorizationError::Unauthorized.into());
}

let db_pool = app_state.db_pool_v2.clone();
let db_pool = app_state.db_pool.clone();
let infra_caches = app_state.infra_caches.clone();
let infra_id = infra.infra_id;
if Infra::fast_delete_static(db_pool.get().await?, infra_id).await? {
Expand Down Expand Up @@ -513,7 +513,7 @@ async fn get_switch_types(
return Err(AuthorizationError::Unauthorized.into());
}

let db_pool = app_state.db_pool_v2.clone();
let db_pool = app_state.db_pool.clone();
let conn = &mut db_pool.get().await?;
let infra_caches = app_state.infra_caches.clone();

Expand Down Expand Up @@ -724,7 +724,7 @@ async fn load(
return Err(AuthorizationError::Unauthorized.into());
}

let db_pool = app_state.db_pool_v2.clone();
let db_pool = app_state.db_pool.clone();
let core_client = app_state.core_client.clone();

let infra_id = path.infra_id;
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/views/infra/pathfinding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async fn pathfinding_view(
return Err(AuthorizationError::Unauthorized.into());
}

let db_pool = app_state.db_pool_v2.clone();
let db_pool = app_state.db_pool.clone();
let infra_caches = app_state.infra_caches.clone();

// Parse and check input
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/views/infra/railjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ async fn post_railjson(
return Err(AuthorizationError::Unauthorized.into());
}

let db_pool = app_state.db_pool_v2.clone();
let db_pool = app_state.db_pool.clone();
let infra_caches = app_state.infra_caches.clone();
if railjson.version != RAILJSON_VERSION {
return Err(ListErrorsRailjson::WrongRailjsonVersionProvided.into());
Expand Down
4 changes: 2 additions & 2 deletions editoast/src/views/infra/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ async fn get_routes_track_ranges(
return Err(AuthorizationError::Unauthorized.into());
}

let db_pool = app_state.db_pool_v2.clone();
let db_pool = app_state.db_pool.clone();
let infra_caches = app_state.infra_caches.clone();
let infra_id = infra;
let infra = Infra::retrieve_or_fail(&mut db_pool.get().await?, infra_id, || {
Expand Down Expand Up @@ -219,7 +219,7 @@ async fn get_routes_nodes(
return Err(AuthorizationError::Unauthorized.into());
}

let db_pool = app_state.db_pool_v2.clone();
let db_pool = app_state.db_pool.clone();
let infra_caches = app_state.infra_caches.clone();

let infra = Infra::retrieve_or_fail(&mut db_pool.get().await?, params.infra_id, || {
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/views/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ struct TileParams {
async fn cache_and_get_mvt_tile(
State(AppState {
map_layers,
db_pool_v2: db_pool,
db_pool,
valkey,
..
}): State<AppState>,
Expand Down
23 changes: 10 additions & 13 deletions editoast/src/views/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use dashmap::DashMap;
use editoast_authz::authorizer::Authorizer;
use editoast_authz::authorizer::UserInfo;
use editoast_authz::BuiltinRole;
use editoast_models::DbConnectionPool;

use editoast_osrdyne_client::OsrdyneClient;
use futures::TryFutureExt;
pub use openapi::OpenApiRoot;
Expand Down Expand Up @@ -225,7 +225,7 @@ async fn authenticate(

async fn authentication_middleware(
State(AppState {
db_pool_v2: db_pool,
db_pool,
disable_authorization,
..
}): State<AppState>,
Expand Down Expand Up @@ -278,7 +278,7 @@ pub enum AppHealthError {
)]
async fn health(
State(AppState {
db_pool_v2: db_pool,
db_pool,
valkey,
health_check_timeout,
core_client,
Expand Down Expand Up @@ -384,8 +384,7 @@ pub struct Server {
pub struct AppState {
pub config: Arc<ServerConfig>,

pub db_pool_v1: Arc<DbConnectionPool>,
pub db_pool_v2: Arc<DbConnectionPoolV2>,
pub db_pool: Arc<DbConnectionPoolV2>,
pub valkey: Arc<ValkeyClient>,
pub infra_caches: Arc<DashMap<i64, InfraCache>>,
pub map_layers: Arc<MapLayers>,
Expand All @@ -398,7 +397,7 @@ pub struct AppState {

impl FromRef<AppState> for DbConnectionPoolV2 {
fn from_ref(input: &AppState) -> Self {
(*input.db_pool_v2).clone()
(*input.db_pool).clone()
}
}

Expand All @@ -409,16 +408,15 @@ impl AppState {
// Config database
let valkey = ValkeyClient::new(config.valkey_config.clone())?.into();

// Create both database pools
let db_pool_v2 = {
// Create database pool
let db_pool = {
let PostgresConfig {
database_url,
pool_size,
} = config.postgres_config.clone();
DbConnectionPoolV2::try_initialize(database_url, pool_size).await?
let pool = DbConnectionPoolV2::try_initialize(database_url, pool_size).await?;
Arc::new(pool)
};
let db_pool_v1 = db_pool_v2.pool_v1();
let db_pool_v2 = Arc::new(db_pool_v2);

// Setup infra cache map
let infra_caches = DashMap::<i64, InfraCache>::default().into();
Expand Down Expand Up @@ -449,8 +447,7 @@ impl AppState {

Ok(Self {
valkey,
db_pool_v1,
db_pool_v2,
db_pool,
infra_caches,
core_client,
osrdyne_client,
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/views/path/pathfinding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub enum PathfindingFailure {
)]
async fn post(
State(AppState {
db_pool_v2: db_pool,
db_pool,
valkey,
core_client,
..
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/views/path/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ type Properties = EnumSet<Property>;
)]
async fn post(
State(AppState {
db_pool_v2: db_pool,
db_pool,
valkey,
core_client,
..
Expand Down
4 changes: 2 additions & 2 deletions editoast/src/views/stdcm_search_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async fn overwrite(
return Err(AuthorizationError::Unauthorized.into());
}

let db_pool = app_state.db_pool_v2.clone();
let db_pool = app_state.db_pool.clone();
let conn = &mut db_pool.get().await?;

let changeset: Changeset<StdcmSearchEnvironment> = form.into();
Expand Down Expand Up @@ -146,7 +146,7 @@ async fn retrieve_latest(
return Err(AuthorizationError::Unauthorized.into());
}

let db_pool = app_state.db_pool_v2.clone();
let db_pool = app_state.db_pool.clone();
let conn = &mut db_pool.get().await?;

let search_env = StdcmSearchEnvironment::retrieve_latest(conn).await;
Expand Down
Loading

0 comments on commit 614ab93

Please sign in to comment.