fix(db): normalize default database handling
This commit is contained in:
1 parent
5cffe12014
commit
af029daf57
8 files changed
+144
-30
No files matched your search
@@ -30,6 +30,7 @@ impl GaussdbClient {
|
||||
}
|
||||
|
||||
pub async fn connect(host: &str, port: u16, database: &str, user: &str, pass: &str) -> Result<GaussdbClient, String> {
|
||||
let database = normalize_database(database);
|
||||
let dsn = format!("host={host} port={port} user={user} password={pass} dbname={database}");
|
||||
|
||||
let result = tokio::time::timeout(std::time::Duration::from_secs(CONNECTION_TIMEOUT_SECS), async {
|
||||
@@ -41,6 +42,15 @@ pub async fn connect(host: &str, port: u16, database: &str, user: &str, pass: &s
|
||||
result.map(|client| GaussdbClient { client })
|
||||
}
|
||||
|
||||
fn normalize_database(database: &str) -> &str {
|
||||
let database = database.trim();
|
||||
if database.is_empty() {
|
||||
"postgres"
|
||||
} else {
|
||||
database
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn list_databases(client: &mut GaussdbClient) -> Result<Vec<DatabaseInfo>, String> {
|
||||
let rows = client
|
||||
.query_single_column("SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname")
|
||||
@@ -276,3 +286,19 @@ pub async fn execute_query(client: &mut GaussdbClient, sql: &str) -> Result<Quer
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::normalize_database;
|
||||
|
||||
#[test]
|
||||
fn normalize_database_defaults_blank_to_postgres() {
|
||||
assert_eq!(normalize_database(""), "postgres");
|
||||
assert_eq!(normalize_database(" "), "postgres");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_database_keeps_explicit_database() {
|
||||
assert_eq!(normalize_database("app"), "app");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user