Skip to content

Commit 979cf17

Browse files
committed
Conditional compilation of DB features
This removes postgres as a default feature. The crate now builds with libsql, postgres, or both features enabled.
1 parent d93aebc commit 979cf17

6 files changed

Lines changed: 66 additions & 25 deletions

File tree

crates/devolutions-pedm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ bb8-postgres = { version = "0.9.0", optional = true }
4747

4848

4949
[features]
50-
default = ["libsql", "postgres"]
50+
default = ["libsql"]
5151
libsql = ["dep:libsql"]
5252
postgres = ["dep:tokio-postgres", "dep:bb8", "dep:bb8-postgres"]
5353

crates/devolutions-pedm/src/api/err.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ impl OperationOutput for HandlerError {
4343
type Inner = (StatusCode, Json<HandlerError>);
4444
}
4545

46-
impl From<tokio_postgres::Error> for HandlerError {
47-
fn from(e: tokio_postgres::Error) -> Self {
46+
impl From<DbError> for HandlerError {
47+
fn from(e: DbError) -> Self {
4848
Self(StatusCode::INTERNAL_SERVER_ERROR, Some(e.to_string()))
4949
}
5050
}
51-
impl From<DbError> for HandlerError {
52-
fn from(e: DbError) -> Self {
51+
52+
#[cfg(feature = "postgres")]
53+
impl From<tokio_postgres::Error> for HandlerError {
54+
fn from(e: tokio_postgres::Error) -> Self {
5355
Self(StatusCode::INTERNAL_SERVER_ERROR, Some(e.to_string()))
5456
}
5557
}

crates/devolutions-pedm/src/api/state.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,30 @@ use std::sync::Arc;
44

55
use axum::extract::{FromRef, FromRequestParts};
66
use axum::http::request::Parts;
7-
use bb8::Pool;
8-
use bb8_postgres::PostgresConnectionManager;
97
use camino::Utf8PathBuf;
108
use hyper::StatusCode;
119
use parking_lot::RwLock;
12-
use tokio_postgres::config::SslMode;
13-
use tokio_postgres::NoTls;
1410
use tracing::info;
1511

1612
use crate::config::{Config, ConfigError, DbBackend};
17-
use crate::db::{Database, DbError, LibsqlConn, PgPool};
13+
use crate::db::{Database, DbError};
1814
use crate::policy::{LoadPolicyError, Policy};
1915

16+
#[cfg(feature = "libsql")]
17+
use crate::db::LibsqlConn;
18+
19+
#[cfg(feature = "postgres")]
20+
use bb8::Pool;
21+
#[cfg(feature = "postgres")]
22+
use bb8_postgres::PostgresConnectionManager;
23+
#[cfg(feature = "postgres")]
24+
use tokio_postgres::config::SslMode;
25+
#[cfg(feature = "postgres")]
26+
use tokio_postgres::NoTls;
27+
28+
#[cfg(feature = "postgres")]
29+
use crate::db::PgPool;
30+
2031
#[derive(Clone)]
2132
pub(crate) struct AppState {
2233
/// Request counter.
@@ -38,6 +49,19 @@ impl AppState {
3849
}?;
3950

4051
let db: Arc<dyn Database + Send + Sync> = match config.db {
52+
#[cfg(feature = "libsql")]
53+
DbBackend::Libsql => {
54+
#[expect(clippy::unwrap_used)]
55+
let c = config.libsql.unwrap(); // already checked by `Config::validate` at the end of the load function
56+
let db_obj = libsql::Builder::new_local(&c.path)
57+
.build()
58+
.await
59+
.map_err(DbError::from)?;
60+
let conn = db_obj.connect().map_err(DbError::from)?;
61+
info!("Connecting to LibSQL database at {}", c.path);
62+
Arc::new(LibsqlConn::new(conn))
63+
}
64+
#[cfg(feature = "postgres")]
4165
DbBackend::Postgres => {
4266
#[expect(clippy::unwrap_used)]
4367
let c = config.postgres.unwrap(); // already checked by `Config::validate` at the end of the load function
@@ -59,17 +83,6 @@ impl AppState {
5983
info!("Connecting to Postgres database {} on host {}", c.dbname, c.host);
6084
Arc::new(PgPool::new(pool))
6185
}
62-
DbBackend::Libsql => {
63-
#[expect(clippy::unwrap_used)]
64-
let c = config.libsql.unwrap(); // already checked by `Config::validate` at the end of the load function
65-
let db_obj = libsql::Builder::new_local(&c.path)
66-
.build()
67-
.await
68-
.map_err(DbError::from)?;
69-
let conn = db_obj.connect().map_err(DbError::from)?;
70-
info!("Connecting to LibSQL database at {}", c.path);
71-
Arc::new(LibsqlConn::new(conn))
72-
}
7386
};
7487

7588
let policy = Policy::load()?;

crates/devolutions-pedm/src/config.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl Config {
2323
/// Creates a new config with the default values for a new setup.
2424
fn standard() -> Self {
2525
Self {
26-
db: DbBackend::Libsql,
26+
db: DbBackend::default(),
2727
postgres: None,
2828
libsql: Some(LibsqlConfig {
2929
path: data_dir().join("pedm.sqlite"),
@@ -58,7 +58,9 @@ impl Config {
5858

5959
fn validate(&self) -> Result<(), ConfigError> {
6060
match self.db {
61+
#[cfg(feature = "libsql")]
6162
DbBackend::Libsql if self.libsql.is_none() => Err(ConfigError::MissingSection(DbBackend::Libsql)),
63+
#[cfg(feature = "postgres")]
6264
DbBackend::Postgres if self.postgres.is_none() => Err(ConfigError::MissingSection(DbBackend::Postgres)),
6365
_ => Ok(()),
6466
}
@@ -68,15 +70,20 @@ impl Config {
6870
#[derive(Serialize, Deserialize, Default, Debug)]
6971
#[serde(rename_all = "lowercase")]
7072
pub enum DbBackend {
71-
#[default]
73+
#[cfg_attr(feature = "libsql", default)]
74+
#[cfg(feature = "libsql")]
7275
Libsql,
76+
#[cfg_attr(all(feature = "postgres", not(feature = "libsql")), default)]
77+
#[cfg(feature = "postgres")]
7378
Postgres,
7479
}
7580

7681
impl fmt::Display for DbBackend {
7782
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7883
match self {
84+
#[cfg(feature = "libsql")]
7985
Self::Libsql => write!(f, "libsql"),
86+
#[cfg(feature = "postgres")]
8087
Self::Postgres => write!(f, "postgres"),
8188
}
8289
}

crates/devolutions-pedm/src/db/err.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ use core::fmt;
22

33
#[derive(Debug)]
44
pub enum DbError {
5+
#[cfg(feature = "libsql")]
56
Libsql(libsql::Error),
7+
#[cfg(feature = "postgres")]
68
Bb8(bb8::RunError<tokio_postgres::Error>),
9+
#[cfg(feature = "postgres")]
710
Postgres(tokio_postgres::Error),
811
}
912

1013
impl core::error::Error for DbError {
1114
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
1215
match self {
16+
#[cfg(feature = "libsql")]
1317
Self::Libsql(e) => Some(e),
18+
#[cfg(feature = "postgres")]
1419
Self::Bb8(e) => Some(e),
20+
#[cfg(feature = "postgres")]
1521
Self::Postgres(e) => Some(e),
1622
}
1723
}
@@ -20,23 +26,30 @@ impl core::error::Error for DbError {
2026
impl fmt::Display for DbError {
2127
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2228
match self {
29+
#[cfg(feature = "libsql")]
2330
Self::Libsql(e) => e.fmt(f),
31+
#[cfg(feature = "postgres")]
2432
Self::Bb8(e) => e.fmt(f),
33+
#[cfg(feature = "postgres")]
2534
Self::Postgres(e) => e.fmt(f),
2635
}
2736
}
2837
}
2938

39+
#[cfg(feature = "libsql")]
3040
impl From<libsql::Error> for DbError {
3141
fn from(e: libsql::Error) -> Self {
3242
Self::Libsql(e)
3343
}
3444
}
45+
46+
#[cfg(feature = "postgres")]
3547
impl From<bb8::RunError<tokio_postgres::Error>> for DbError {
3648
fn from(e: bb8::RunError<tokio_postgres::Error>) -> Self {
3749
Self::Bb8(e)
3850
}
3951
}
52+
#[cfg(feature = "postgres")]
4053
impl From<tokio_postgres::Error> for DbError {
4154
fn from(e: tokio_postgres::Error) -> Self {
4255
Self::Postgres(e)

crates/devolutions-pedm/src/db/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
use async_trait::async_trait;
22

33
mod err;
4-
mod libsql;
5-
mod pg;
64

75
pub(crate) use err::DbError;
6+
7+
#[cfg(feature = "libsql")]
8+
mod libsql;
9+
#[cfg(feature = "libsql")]
810
pub(crate) use libsql::LibsqlConn;
11+
12+
#[cfg(feature = "postgres")]
13+
mod pg;
14+
#[cfg(feature = "postgres")]
915
pub(crate) use pg::PgPool;
1016

1117
/// Abstracts database operations for backends such as Postgres or libSQL.

0 commit comments

Comments
 (0)