feat(dolt): improve system tables and SQL completion

This commit is contained in:
hanxuanyu authored and GitHub committed 2026-08-14 22:18:28 +08:00
1 parent c5eea21821
commit a574459f8b
30 files changed
+789 -70

No files matched your search

+112 -2
View File
@@ -2,9 +2,9 @@ use mysql_async::prelude::Queryable;
use std::collections::HashSet;
use crate::models::connection::{ConnectionConfig, DatabaseType};
use crate::types::DatabaseInfo;
use crate::types::{DatabaseInfo, TableInfo};
use super::mysql::{get_conn_with_timeout, quote_identifier, MySqlPool};
use super::mysql::{get_conn_with_timeout, list_table_names_show_filtered_with_conn, quote_identifier, MySqlPool};
const ENABLE_BRANCH_DATABASES_SQL: &str = "SET @@dolt_show_branch_databases = 1";
// 与设置对称地复位 session 变量:连接从池中取出后是单条会被复用的 session 连接,
@@ -12,11 +12,29 @@ const ENABLE_BRANCH_DATABASES_SQL: &str = "SET @@dolt_show_branch_databases = 1"
// 会意外看到 branch 数据库。复位是 best-effort,失败不应让 list_databases 整体失败。
const DISABLE_BRANCH_DATABASES_SQL: &str = "SET @@dolt_show_branch_databases = 0";
const SHOW_DATABASES_SQL: &str = "SHOW DATABASES";
const ENABLE_SYSTEM_TABLES_SQL: &str = "SET @@dolt_show_system_tables = 1";
const DISABLE_SYSTEM_TABLES_SQL: &str = "SET @@dolt_show_system_tables = 0";
const SHOW_SYSTEM_TABLES_CONFIG_KEY: &str = "doltShowSystemTables";
const SYSTEM_TABLE_PREFIX: &str = "dolt";
pub fn is_config(config: &ConnectionConfig) -> bool {
is_profile(&config.db_type, config.driver_profile.as_deref())
}
pub fn system_tables_visible(config: &ConnectionConfig) -> bool {
is_config(config)
&& config
.external_config
.as_ref()
.and_then(|value| value.get(SHOW_SYSTEM_TABLES_CONFIG_KEY))
.and_then(serde_json::Value::as_bool)
.unwrap_or(false)
}
pub fn requests_system_tables(include_patterns: Option<&[String]>) -> bool {
include_patterns.is_some_and(|patterns| patterns.iter().any(|pattern| pattern.trim().eq_ignore_ascii_case("dolt%")))
}
fn is_profile(db_type: &DatabaseType, driver_profile: Option<&str>) -> bool {
*db_type == DatabaseType::Mysql && driver_profile.is_some_and(|profile| profile.eq_ignore_ascii_case("dolt"))
}
@@ -36,6 +54,30 @@ pub async fn list_databases(pool: &MySqlPool) -> Result<Vec<DatabaseInfo>, Strin
Ok(database_infos(databases))
}
pub async fn list_system_tables(
pool: &MySqlPool,
database: &str,
filter: Option<&str>,
) -> Result<Vec<TableInfo>, String> {
// The visibility flag is session-scoped, so setup, metadata lookup, and
// cleanup must use the same pooled connection.
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
conn.query_drop(ENABLE_SYSTEM_TABLES_SQL).await.map_err(|error| error.to_string())?;
let server_filter = filter.map(str::trim).filter(|filter| !filter.is_empty()).unwrap_or(SYSTEM_TABLE_PREFIX);
let tables = list_table_names_show_filtered_with_conn(&mut conn, database, Some(server_filter), &[])
.await
.map(retain_system_tables);
if let Err(error) = conn.query_drop(DISABLE_SYSTEM_TABLES_SQL).await {
log::warn!("Failed to reset Dolt system table session variable before returning connection: {error}");
}
tables
}
fn retain_system_tables(mut tables: Vec<TableInfo>) -> Vec<TableInfo> {
tables.retain(|table| table.name.to_ascii_lowercase().starts_with(SYSTEM_TABLE_PREFIX));
tables
}
async fn list_databases_from_branches(conn: &mut mysql_async::Conn) -> Result<Vec<DatabaseInfo>, String> {
let base_names = query_database_names(conn).await?;
let discovery_names: Vec<String> =
@@ -98,6 +140,21 @@ fn list_branches_sql(database: &str) -> String {
mod tests {
use super::*;
fn dolt_config() -> ConnectionConfig {
serde_json::from_value(serde_json::json!({
"id": "dolt",
"name": "Dolt",
"db_type": "mysql",
"driver_profile": "dolt",
"host": "127.0.0.1",
"port": 3306,
"username": "root",
"password": "",
"database": "app"
}))
.unwrap()
}
#[test]
fn profile_is_isolated_from_standard_mysql() {
assert!(is_profile(&DatabaseType::Mysql, Some("Dolt")));
@@ -105,6 +162,59 @@ mod tests {
assert!(!is_profile(&DatabaseType::Postgres, Some("dolt")));
}
#[test]
fn system_table_visibility_uses_dolt_profile_config_only() {
let mut config = dolt_config();
assert!(!system_tables_visible(&config));
config.external_config = Some(serde_json::json!({ "doltShowSystemTables": true }));
assert!(system_tables_visible(&config));
config.driver_profile = Some("mysql".to_string());
assert!(!system_tables_visible(&config));
}
#[test]
fn system_table_requests_require_the_dedicated_include_pattern() {
assert!(requests_system_tables(Some(&["dolt%".to_string()])));
assert!(requests_system_tables(Some(&[" DOLT% ".to_string()])));
assert!(!requests_system_tables(Some(&["%dolt%".to_string()])));
assert!(!requests_system_tables(Some(&[])));
assert!(!requests_system_tables(None));
}
#[test]
fn system_table_results_exclude_ordinary_tables_after_show_fallbacks() {
let tables = vec![
TableInfo {
name: "dolt_log".to_string(),
table_type: "BASE TABLE".to_string(),
comment: None,
parent_schema: None,
parent_name: None,
},
TableInfo {
name: "DOLT_BRANCHES".to_string(),
table_type: "BASE TABLE".to_string(),
comment: None,
parent_schema: None,
parent_name: None,
},
TableInfo {
name: "orders".to_string(),
table_type: "BASE TABLE".to_string(),
comment: Some("ordinary".to_string()),
parent_schema: None,
parent_name: None,
},
];
assert_eq!(
retain_system_tables(tables).into_iter().map(|table| table.name).collect::<Vec<_>>(),
vec!["dolt_log", "DOLT_BRANCHES"]
);
}
#[test]
fn fallback_only_discovers_base_user_databases() {
assert!(is_branch_discovery_database("inventory"));
+9
View File
@@ -2764,6 +2764,15 @@ async fn list_table_names_show_filtered(
exact_names: &[String],
) -> Result<Vec<TableInfo>, String> {
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
list_table_names_show_filtered_with_conn(&mut conn, database, filter, exact_names).await
}
pub(super) async fn list_table_names_show_filtered_with_conn(
conn: &mut mysql_async::Conn,
database: &str,
filter: Option<&str>,
exact_names: &[String],
) -> Result<Vec<TableInfo>, String> {
let mut last_error = None;
let mut rows = None;
for attempt in show_tables_query_attempts(database, filter, exact_names) {
+10
View File
@@ -2162,6 +2162,16 @@ async fn list_tables_once(
.await
.map(|tables| filter_table_infos(tables, filter, limit, offset, object_types, table_name_filter))
}
PoolKind::Mysql(p, _)
if db_config.as_ref().is_some_and(db::dolt::system_tables_visible)
&& db::dolt::requests_system_tables(
table_name_filter.map(|filter| filter.include_patterns.as_slice()),
) =>
{
db::dolt::list_system_tables(p, mysql_table_metadata_catalog(database, schema), filter)
.await
.map(|tables| filter_table_infos(tables, filter, limit, offset, object_types, table_name_filter))
}
PoolKind::Mysql(p, mode) => {
if *mode == MysqlMode::OceanBaseOracle {
let tables = db::ob_oracle::list_tables(p, schema).await?;