Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(postgres): read database comments from shared descriptions

Acd-kkb0b037329268
diff --git a/crates/dbx-core/src/schema/mod.rs b/crates/dbx-core/src/schema/mod.rs
index 6daaae888c..308a8a64fd 100644
--- a/crates/dbx-core/src/schema/mod.rs
+++ b/crates/dbx-core/src/schema/mod.rs
@@ -746,6 +746,16 @@ async fn list_databases_once(state: &AppState, connection_id: &str) -> Result<Ve
}
}
+fn uses_shared_database_comments(config: &ConnectionConfig) -> bool {
+ match config.db_type {
+ DatabaseType::Postgres => {
+ matches!(config.driver_profile.as_deref(), None | Some("postgres" | "opengauss" | "gaussdb"))
+ }
+ DatabaseType::OpenGauss | DatabaseType::Gaussdb => true,
+ _ => false,
+ }
+}
+
async fn list_database_metadata_once(state: &AppState, connection_id: &str) -> Result<Vec<db::DatabaseInfo>, String> {
let config = connection_config(state, connection_id).await;
if config.as_ref().is_some_and(|config| {
@@ -768,7 +778,8 @@ async fn list_database_metadata_once(state: &AppState, connection_id: &str) -> R
};
}
if let Some(pool) = extract_pool!(pool_handle.as_ref(), Postgres) {
- return db::postgres::list_database_metadata(&pool).await;
+ let shared_comments = config.as_ref().is_some_and(uses_shared_database_comments);
+ return db::postgres::list_database_metadata(&pool, shared_comments).await;
}
list_databases_once(state, connection_id).await
}
@@ -5001,6 +5012,38 @@ for line in sys.stdin:
assert!(super::is_opengauss_family_config(&profiled_postgres));
}
+ #[test]
+ fn database_metadata_limits_shared_comments_to_verified_postgres_families() {
+ for (db_type, profile, expected) in [
+ (DatabaseType::Postgres, None, true),
+ (DatabaseType::Postgres, Some("postgres"), true),
+ (DatabaseType::Postgres, Some("opengauss"), true),
+ (DatabaseType::Postgres, Some("gaussdb"), true),
+ (DatabaseType::OpenGauss, None, true),
+ (DatabaseType::OpenGauss, Some("opengauss"), true),
+ (DatabaseType::Gaussdb, None, true),
+ (DatabaseType::Gaussdb, Some("gaussdb"), true),
+ (DatabaseType::Postgres, Some("cockroachdb"), false),
+ (DatabaseType::Postgres, Some("redshift"), false),
+ (DatabaseType::Postgres, Some("questdb"), false),
+ (DatabaseType::Postgres, Some("cloudberry"), false),
+ (DatabaseType::Postgres, Some("kwdb"), false),
+ (DatabaseType::Postgres, Some("unknown"), false),
+ (DatabaseType::Redshift, None, false),
+ (DatabaseType::Questdb, None, false),
+ (DatabaseType::Kwdb, None, false),
+ (DatabaseType::Mysql, None, false),
+ ] {
+ let mut config = test_connection_config(db_type);
+ config.driver_profile = profile.map(str::to_string);
+ assert_eq!(
+ super::uses_shared_database_comments(&config),
+ expected,
+ "unexpected shared comment support for {db_type:?} / {profile:?}"
+ );
+ }
+ }
+
#[test]
fn agent_paging_detection_avoids_double_offset_only_when_page_sized() {
assert!(super::agent_paging_likely_applied(true, Some(500), 500));
diff --git a/crates/dbx-driver-postgres/src/postgres.rs b/crates/dbx-driver-postgres/src/postgres.rs
index d3a96ad098..051b170f63 100644
--- a/crates/dbx-driver-postgres/src/postgres.rs
+++ b/crates/dbx-driver-postgres/src/postgres.rs
@@ -3867,8 +3867,12 @@ fn list_opengauss_databases_sql() -> &'static str {
ORDER BY datname"
}
-fn database_metadata_sql() -> &'static str {
- "SELECT d.datname, \
+fn database_metadata_sql(shared_comments: bool) -> String {
+ // Database comments are shared objects on PostgreSQL and openGauss, but
+ // other PG-protocol servers must keep their existing catalog query.
+ let comment_function = if shared_comments { "shobj_description" } else { "obj_description" };
+ format!(
+ "SELECT d.datname, \
CASE \
WHEN has_database_privilege(d.datname, 'CONNECT') \
OR COALESCE(( \
@@ -3879,11 +3883,12 @@ fn database_metadata_sql() -> &'static str {
THEN pg_database_size(d.oid) \
ELSE NULL \
END AS size_bytes, \
- obj_description(d.oid, 'pg_database') AS comment, \
+ {comment_function}(d.oid, 'pg_database') AS comment, \
pg_encoding_to_char(d.encoding) AS default_charset \
FROM pg_database d \
WHERE datallowconn = true \
ORDER BY d.datname"
+ )
}
fn database_storage_sql() -> &'static str {
@@ -3947,10 +3952,11 @@ pub async fn list_opengauss_databases(pool: &Pool) -> Result<Vec<DatabaseInfo>,
.collect())
}
-pub async fn list_database_metadata(pool: &Pool) -> Result<Vec<DatabaseInfo>, String> {
+pub async fn list_database_metadata(pool: &Pool, shared_comments: bool) -> Result<Vec<DatabaseInfo>, String> {
let client = checkout_postgres_client(pool, None, super::connection_timeout()).await?;
let client = &EncodingClient::new(&client);
- let rows = postgres_query_cached(client, database_metadata_sql(), &[]).await.map_err(|e| e.to_string())?;
+ let sql = database_metadata_sql(shared_comments);
+ let rows = postgres_query_cached(client, &sql, &[]).await.map_err(|e| e.to_string())?;
Ok(rows
.iter()
.map(|row| DatabaseInfo {
@@ -12445,6 +12451,22 @@ mod tests {
assert!(!list_databases_sql().contains("pg_database_size"));
}
+ #[test]
+ fn database_metadata_reads_shared_database_comments() {
+ let sql = database_metadata_sql(true);
+ assert!(sql.contains("shobj_description(d.oid, 'pg_database') AS comment"));
+ assert!(sql.contains("FROM pg_database d"));
+ assert!(sql.contains("has_database_privilege"));
+ assert!(sql.contains("pg_database_size(d.oid)"));
+ }
+
+ #[test]
+ fn database_metadata_keeps_legacy_comments_for_other_pg_servers() {
+ let sql = database_metadata_sql(false);
+ assert!(sql.contains("obj_description(d.oid, 'pg_database') AS comment"));
+ assert!(!sql.contains("shobj_description"));
+ }
+
#[test]
fn database_storage_is_scoped_and_permission_guarded() {
let sql = database_storage_sql();