Skip to content

Commit c6bdce6

Browse files
committed
refactor: Switch derby location from String to PathBuf
1 parent 31cc9f5 commit c6bdce6

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

  • crates/stackable-operator/src/databases/databases

crates/stackable-operator/src/databases/databases/derby.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1+
use std::path::PathBuf;
2+
13
use schemars::JsonSchema;
24
use serde::{Deserialize, Serialize};
3-
use snafu::{ResultExt, Snafu};
5+
use snafu::{OptionExt, ResultExt, Snafu};
46

57
use crate::{
68
databases::{
79
TemplatingMechanism,
810
drivers::jdbc::{JdbcDatabaseConnection, JdbcDatabaseConnectionDetails},
911
},
10-
utils::OptionExt,
12+
utils::OptionExt as _,
1113
};
1214

1315
#[derive(Debug, Snafu)]
1416
pub enum Error {
1517
#[snafu(display("failed to parse connection URL"))]
1618
ParseConnectionUrl { source: url::ParseError },
19+
20+
#[snafu(display("invalid derby database location, likely as it contains non-utf8 characters"))]
21+
NonUtf8Location { location: PathBuf },
1722
}
1823

1924
/// Connection settings for an embedded [Apache Derby](https://db.apache.org/derby/) database.
@@ -30,7 +35,7 @@ pub struct DerbyConnection {
3035
/// The `{unique_database_name}` part is automatically handled by the operator and is added to
3136
/// prevent clashing database files. The `create=true` flag is always appended to the JDBC URL,
3237
/// so the database is created automatically if it does not yet exist at this location.
33-
pub location: Option<String>,
38+
pub location: Option<PathBuf>,
3439
}
3540

3641
impl JdbcDatabaseConnection for DerbyConnection {
@@ -39,9 +44,12 @@ impl JdbcDatabaseConnection for DerbyConnection {
3944
unique_database_name: &str,
4045
_templating_mechanism: &TemplatingMechanism,
4146
) -> Result<JdbcDatabaseConnectionDetails, crate::databases::Error> {
42-
let location = self
43-
.location
44-
.as_ref_or_else(|| format!("/tmp/derby/{unique_database_name}/derby.db"));
47+
let location = self.location.as_ref_or_else(|| {
48+
PathBuf::from(format!("/tmp/derby/{unique_database_name}/derby.db"))
49+
});
50+
let location = location.to_str().with_context(|| NonUtf8LocationSnafu {
51+
location: location.to_path_buf(),
52+
})?;
4553
let connection_uri = format!("jdbc:derby:{location};create=true",);
4654
let connection_uri = connection_uri.parse().context(ParseConnectionUrlSnafu)?;
4755

0 commit comments

Comments
 (0)