Skip to content

Commit

Permalink
Merge pull request #4321 from zaira-bibi/database-drop-fix
Browse files Browse the repository at this point in the history
fix: connect to template1 when database is postgres
  • Loading branch information
weiznich authored Nov 11, 2024
2 parents 2090344 + 5632671 commit a459c31
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions diesel_cli/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,13 @@ fn drop_database(database_url: &str) -> Result<(), crate::errors::Error> {
match Backend::for_url(database_url) {
#[cfg(feature = "postgres")]
Backend::Pg => {
let (database, postgres_url) = change_database_of_url(database_url, "postgres")?;
let (current_database, _) = get_database_and_url(database_url)?;
let default_database = if current_database.eq("postgres") {
"template1"
} else {
"postgres"
};
let (database, postgres_url) = change_database_of_url(database_url, default_database)?;
let mut conn = PgConnection::establish(&postgres_url).map_err(|e| {
crate::errors::Error::ConnectionError {
error: e,
Expand Down Expand Up @@ -422,18 +428,24 @@ fn change_database_of_url(
database_url: &str,
default_database: &str,
) -> Result<(String, String), crate::errors::Error> {
let (database, base) = get_database_and_url(database_url)?;
let mut new_url = base
.join(default_database)
.expect("The provided database is always valid");
new_url.set_query(base.query());
Ok((database, new_url.into()))
}

#[cfg(any(feature = "postgres", feature = "mysql"))]
fn get_database_and_url(database_url: &str) -> Result<(String, url::Url), crate::errors::Error> {
let base = url::Url::parse(database_url)?;
let database = base
.path_segments()
.expect("The database url has at least one path segment")
.last()
.expect("The database url has at least one path segment")
.to_owned();
let mut new_url = base
.join(default_database)
.expect("The provided database is always valid");
new_url.set_query(base.query());
Ok((database, new_url.into()))
Ok((database, base))
}

#[cfg(feature = "sqlite")]
Expand Down

0 comments on commit a459c31

Please sign in to comment.