|
1 | | -use std::ops::Deref; |
2 | | - |
3 | 1 | use serde::{Deserialize, Serialize}; |
4 | 2 | use stackable_operator::{ |
5 | 3 | database_connections::{ |
| 4 | + self, TemplatingMechanism, |
6 | 5 | databases::{ |
7 | 6 | derby::DerbyConnection, mysql::MysqlConnection, postgresql::PostgresqlConnection, |
8 | 7 | }, |
9 | | - drivers::jdbc::JdbcDatabaseConnection, |
| 8 | + drivers::jdbc::{JdbcDatabaseConnection, JdbcDatabaseConnectionDetails}, |
10 | 9 | }, |
11 | 10 | schemars::{self, JsonSchema}, |
12 | 11 | }; |
@@ -47,14 +46,74 @@ impl MetadataDatabaseConnection { |
47 | 46 | } |
48 | 47 | } |
49 | 48 |
|
50 | | -impl Deref for MetadataDatabaseConnection { |
51 | | - type Target = dyn JdbcDatabaseConnection; |
52 | | - |
53 | | - fn deref(&self) -> &Self::Target { |
| 49 | +impl JdbcDatabaseConnection for MetadataDatabaseConnection { |
| 50 | + /// We do *not* implement [`std::ops::Deref`]` for [`MetadataDatabaseConnection`], as we need |
| 51 | + /// some special handling for Derby. |
| 52 | + fn jdbc_connection_details_with_templating( |
| 53 | + &self, |
| 54 | + unique_database_name: &str, |
| 55 | + templating_mechanism: &TemplatingMechanism, |
| 56 | + ) -> Result<JdbcDatabaseConnectionDetails, database_connections::Error> { |
54 | 57 | match self { |
55 | | - Self::Postgresql(p) => p, |
56 | | - Self::Mysql(m) => m, |
57 | | - Self::Derby(d) => d, |
| 58 | + Self::Postgresql(p) => p.jdbc_connection_details_with_templating( |
| 59 | + unique_database_name, |
| 60 | + templating_mechanism, |
| 61 | + ), |
| 62 | + Self::Mysql(m) => m.jdbc_connection_details_with_templating( |
| 63 | + unique_database_name, |
| 64 | + templating_mechanism, |
| 65 | + ), |
| 66 | + Self::Derby(d) => { |
| 67 | + // According to the [Druid docs](https://druid.apache.org/docs/latest/design/metadata-storage/#derby) |
| 68 | + // we should configure something like |
| 69 | + // `jdbc:derby://localhost:1527//opt/var/druid_state/derby;create=true` |
| 70 | + // instead of the usual `jdbc:derby:/opt/var/druid_state/derby;create=true`. |
| 71 | + // |
| 72 | + // It looks like Druid always starts Derby at `localhost:1527`, regardless of what we configure here, |
| 73 | + // so we can hardcode it here. |
| 74 | + d.jdbc_connection_details_with_host_part(unique_database_name, "localhost:1527") |
| 75 | + } |
58 | 76 | } |
59 | 77 | } |
60 | 78 | } |
| 79 | + |
| 80 | +#[cfg(test)] |
| 81 | +mod tests { |
| 82 | + use rstest::rstest; |
| 83 | + use stackable_operator::utils::yaml_from_str_singleton_map; |
| 84 | + |
| 85 | + use super::*; |
| 86 | + |
| 87 | + #[rstest] |
| 88 | + #[case::postgres( |
| 89 | + "postgresql: |
| 90 | + host: druid-postgresql |
| 91 | + database: druid |
| 92 | + credentialsSecretName: druid-credentials", |
| 93 | + "jdbc:postgresql://druid-postgresql:5432/druid" |
| 94 | + )] |
| 95 | + #[case::derby( |
| 96 | + "derby: {}", |
| 97 | + "jdbc:derby://localhost:1527//tmp/derby/METADATA/derby.db;create=true" |
| 98 | + )] |
| 99 | + #[case::derby_custom_location( |
| 100 | + "derby: |
| 101 | + location: /user/provided.db", |
| 102 | + "jdbc:derby://localhost:1527//user/provided.db;create=true" |
| 103 | + )] |
| 104 | + fn test_connection_url( |
| 105 | + #[case] database_connection_yaml: &str, |
| 106 | + #[case] expected_connection_url: &str, |
| 107 | + ) { |
| 108 | + let database_connection: MetadataDatabaseConnection = |
| 109 | + yaml_from_str_singleton_map(database_connection_yaml).expect("invalid YAML"); |
| 110 | + |
| 111 | + let jdbc_connection_details = database_connection |
| 112 | + .jdbc_connection_details("METADATA") |
| 113 | + .expect("failed to get JDBC connection details"); |
| 114 | + assert_eq!( |
| 115 | + jdbc_connection_details.connection_url.as_str(), |
| 116 | + expected_connection_url |
| 117 | + ); |
| 118 | + } |
| 119 | +} |
0 commit comments