Skip to content

Commit 0e9daa9

Browse files
committed
Fix CLI connection to the postgres database
We connect to the postgres database to create/drop databases, but when the user supplied a username and password, we would connect to postgres://user:pass@localhost, which psql assumes means you also want to connect to the `user` database. So, now we are always explicit about what database we're connecting to.
1 parent 1427b9f commit 0e9daa9

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

diesel_cli/src/database.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub fn backend(database_url: &String) -> &str {
163163
fn split_pg_connection_string(database_url: &String) -> (String, String) {
164164
let mut split: Vec<&str> = database_url.split("/").collect();
165165
let database = split.pop().unwrap();
166-
let postgres_url = split.join("/");
166+
let postgres_url = format!("{}/{}", split.join("/"), "postgres");
167167
(database.to_owned(), postgres_url)
168168
}
169169

@@ -179,8 +179,18 @@ mod tests {
179179
#[test]
180180
fn split_pg_connection_string_returns_postgres_url_and_database() {
181181
let database = "database".to_owned();
182-
let postgres_url = "postgresql://localhost:5432".to_owned();
183-
let database_url = format!("{}/{}", postgres_url, database);
182+
let base_url = "postgresql://localhost:5432".to_owned();
183+
let database_url = format!("{}/{}", base_url, database);
184+
let postgres_url = format!("{}/{}", base_url, "postgres");
185+
assert_eq!((database, postgres_url), split_pg_connection_string(&database_url));
186+
}
187+
188+
#[test]
189+
fn split_pg_connection_string_handles_user_and_password() {
190+
let database = "database".to_owned();
191+
let base_url = "postgresql://user:password@localhost:5432".to_owned();
192+
let database_url = format!("{}/{}", base_url, database);
193+
let postgres_url = format!("{}/{}", base_url, "postgres");
184194
assert_eq!((database, postgres_url), split_pg_connection_string(&database_url));
185195
}
186196
}

diesel_cli/tests/database_reset.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,22 @@ fn reset_runs_database_setup() {
4343
assert!(db.table_exists("users"));
4444
assert!(db.table_exists("__diesel_schema_migrations"));
4545
}
46+
47+
#[test]
48+
#[cfg(feature = "postgres")]
49+
fn reset_handles_postgres_urls_with_username_and_password() {
50+
let p = project("handles_postgres_urls")
51+
.folder("migrations")
52+
.build();
53+
let db = database(&p.database_url()).create();
54+
55+
db.execute("DROP ROLE IF EXISTS foo");
56+
db.execute("CREATE ROLE foo WITH LOGIN SUPERUSER PASSWORD 'password'");
57+
58+
let result = p.command("database")
59+
.arg("reset")
60+
.env("DATABASE_URL", &format!("postgres://foo:password@localhost/{}", p.name))
61+
.run();
62+
63+
assert!(result.is_success(), "Result was unsuccessful {:?}", result.stdout());
64+
}

diesel_cli/tests/support/project_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl ProjectBuilder {
4545

4646
pub struct Project {
4747
directory: TempDir,
48-
name: String,
48+
pub name: String,
4949
}
5050

5151
impl Project {

0 commit comments

Comments
 (0)