Skip to content

Commit 1c7b62d

Browse files
authored
fix: Allow default database name to be configured (#609)
1 parent ada952e commit 1c7b62d

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ pub struct Config {
167167
/// The name of the postgres database to use for the inflight activation store.
168168
pub pg_database_name: String,
169169

170+
/// The default postgres database to use for migrations..
171+
pub pg_default_database_name: String,
172+
170173
/// Extra query parameters that can be added to the postgres connection string. Should be in the format of "key=value&key2=value2".
171174
/// For example, "sslmode=require&sslrootcert=/path/to/root.crt".
172175
pub pg_extra_query_params: Option<String>,
@@ -347,6 +350,7 @@ impl Default for Config {
347350
pg_username: "postgres".to_owned(),
348351
pg_password: "password".to_owned(),
349352
pg_database_name: "default".to_owned(),
353+
pg_default_database_name: "postgres".to_owned(),
350354
pg_extra_query_params: None,
351355
db_write_failure_backoff_ms: 4000,
352356
db_insert_batch_max_len: 256,

src/store/adapters/postgres.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@ pub async fn create_postgres_pool(
115115

116116
pub async fn create_default_postgres_pool(
117117
connection: &PgConnectOptions,
118+
default_database_name: &str,
118119
) -> Result<Pool<Postgres>, Error> {
119-
let conn_opts = connection.clone().database("postgres");
120+
let conn_opts = connection.clone().database(default_database_name);
120121
let default_pool = PgPoolOptions::new()
121122
.max_connections(64)
122123
.connect_with(conn_opts)
@@ -127,6 +128,7 @@ pub async fn create_default_postgres_pool(
127128
pub struct PostgresActivationStoreConfig {
128129
pub pg_connection: PgConnectOptions,
129130
pub pg_database_name: String,
131+
pub pg_default_database_name: String,
130132
pub run_migrations: bool,
131133
pub max_processing_attempts: usize,
132134
pub processing_deadline_grace_sec: u64,
@@ -152,6 +154,7 @@ impl PostgresActivationStoreConfig {
152154
Self {
153155
pg_connection: conn_opts,
154156
pg_database_name: config.pg_database_name.clone(),
157+
pg_default_database_name: config.pg_default_database_name.clone(),
155158
run_migrations: config.run_migrations,
156159
max_processing_attempts: config.max_processing_attempts,
157160
vacuum_page_count: config.vacuum_page_count,
@@ -182,7 +185,11 @@ impl PostgresActivationStore {
182185

183186
pub async fn new(config: PostgresActivationStoreConfig) -> Result<Self, Error> {
184187
if config.run_migrations {
185-
let default_pool = create_default_postgres_pool(&config.pg_connection).await?;
188+
let default_pool = create_default_postgres_pool(
189+
&config.pg_connection,
190+
&config.pg_default_database_name,
191+
)
192+
.await?;
186193

187194
// Create the database if it doesn't exist
188195
let row: (bool,) = sqlx::query_as(
@@ -978,7 +985,11 @@ impl InflightActivationStore for PostgresActivationStore {
978985
async fn remove_db(&self) -> Result<(), Error> {
979986
self.read_pool.close().await;
980987
self.write_pool.close().await;
981-
let default_pool = create_default_postgres_pool(&self.config.pg_connection).await?;
988+
let default_pool = create_default_postgres_pool(
989+
&self.config.pg_connection,
990+
&self.config.pg_default_database_name,
991+
)
992+
.await?;
982993
let _ = sqlx::query(format!("DROP DATABASE {}", &self.config.pg_database_name).as_str())
983994
.bind(&self.config.pg_database_name)
984995
.execute(&default_pool)

0 commit comments

Comments
 (0)