Skip to content

Commit 8c2e907

Browse files
committed
fix!: define bootstrap superuser as postgres
1 parent 9ef5d21 commit 8c2e907

File tree

5 files changed

+65
-41
lines changed

5 files changed

+65
-41
lines changed

Cargo.lock

Lines changed: 34 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

postgresql_embedded/src/postgresql.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::error::Error::{DatabaseInitializationError, DatabaseStartError, DatabaseStopError};
22
use crate::error::Result;
3-
use crate::settings::Settings;
3+
use crate::settings::{Settings, BOOTSTRAP_SUPERUSER};
44
use postgresql_archive::{extract, get_archive};
55
use postgresql_archive::{get_version, Version};
66
use postgresql_commands::initdb::InitDbBuilder;
@@ -222,6 +222,7 @@ impl PostgreSQL {
222222

223223
let initdb = InitDbBuilder::from(&self.settings)
224224
.pgdata(&self.settings.data_dir)
225+
.username(BOOTSTRAP_SUPERUSER)
225226
.auth("password")
226227
.pwfile(&self.settings.password_file)
227228
.encoding("UTF8");
@@ -310,9 +311,8 @@ impl PostgreSQL {
310311
);
311312
let psql = PsqlBuilder::from(&self.settings)
312313
.command(format!("CREATE DATABASE \"{}\"", database_name.as_ref()))
313-
.no_psqlrc()
314-
.no_align()
315-
.tuples_only();
314+
.username(BOOTSTRAP_SUPERUSER)
315+
.no_psqlrc();
316316

317317
match self.execute_command(psql).await {
318318
Ok((_stdout, _stderr)) => {
@@ -337,18 +337,14 @@ impl PostgreSQL {
337337
self.settings.host,
338338
self.settings.port
339339
);
340-
let psql = PsqlBuilder::new()
340+
let psql = PsqlBuilder::from(&self.settings)
341341
.program_dir(self.settings.binary_dir())
342342
.command(format!(
343343
"SELECT 1 FROM pg_database WHERE datname='{}'",
344344
database_name.as_ref()
345345
))
346-
.host(&self.settings.host)
347-
.port(self.settings.port)
348-
.username(&self.settings.username)
349-
.pg_password(&self.settings.password)
346+
.username(BOOTSTRAP_SUPERUSER)
350347
.no_psqlrc()
351-
.no_align()
352348
.tuples_only();
353349

354350
match self.execute_command(psql).await {
@@ -379,9 +375,7 @@ impl PostgreSQL {
379375
.port(self.settings.port)
380376
.username(&self.settings.username)
381377
.pg_password(&self.settings.password)
382-
.no_psqlrc()
383-
.no_align()
384-
.tuples_only();
378+
.no_psqlrc();
385379

386380
match self.execute_command(psql).await {
387381
Ok((_stdout, _stderr)) => {

postgresql_embedded/src/settings.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ use std::str::FromStr;
1111
use std::time::Duration;
1212
use url::Url;
1313

14+
/// PostgreSQL's superuser
15+
pub const BOOTSTRAP_SUPERUSER: &str = "postgres";
16+
1417
/// Database settings
1518
#[derive(Clone, Debug, PartialEq, PartialOrd)]
1619
pub struct Settings {
@@ -72,7 +75,7 @@ impl Settings {
7275
data_dir,
7376
host: "localhost".to_string(),
7477
port: 0,
75-
username: "postgres".to_string(),
78+
username: BOOTSTRAP_SUPERUSER.to_string(),
7679
password,
7780
temporary: true,
7881
timeout: Some(Duration::from_secs(5)),
@@ -203,7 +206,7 @@ mod tests {
203206
assert!(settings.password_file.ends_with(".pgpass"));
204207
assert!(!settings.data_dir.to_str().unwrap_or_default().is_empty());
205208
assert_eq!(0, settings.port);
206-
assert_eq!("postgres", settings.username);
209+
assert_eq!(BOOTSTRAP_SUPERUSER, settings.username);
207210
assert!(!settings.password.is_empty());
208211
assert_ne!("password", settings.password);
209212
assert!(settings.binary_dir().ends_with("bin"));
@@ -229,7 +232,7 @@ mod tests {
229232

230233
let settings = Settings::from_url(url)?;
231234

232-
assert_eq!("postgres", settings.username);
235+
assert_eq!(BOOTSTRAP_SUPERUSER, settings.username);
233236
assert_eq!("password", settings.password);
234237
assert_eq!("localhost", settings.host);
235238
assert_eq!(5432, settings.port);

postgresql_embedded/tests/postgresql.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,21 @@ async fn postgres_concurrency() -> anyhow::Result<()> {
143143
}
144144
Ok(())
145145
}
146+
147+
#[test(tokio::test)]
148+
async fn test_username_setting() -> Result<()> {
149+
let username = "admin".to_string();
150+
let settings = Settings {
151+
username,
152+
..Default::default()
153+
};
154+
let mut postgresql = PostgreSQL::new(LATEST, settings);
155+
postgresql.setup().await?;
156+
postgresql.start().await?;
157+
158+
let database_name = "test";
159+
postgresql.create_database(database_name).await?;
160+
let database_exists = postgresql.database_exists(database_name).await?;
161+
assert!(database_exists);
162+
Ok(())
163+
}

0 commit comments

Comments
 (0)