feat(sqlite): support attached database schema changes

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-07-20 10:51:16 +08:00
1 parent 890fd70c05
commit 85836f8e95
41 files changed
+1219 -129

No files matched your search

+124 -14
View File
@@ -162,18 +162,15 @@ async fn connect_agent_pool(
#[cfg(test)]
mod tests {
#[cfg(feature = "sqlite-sqlcipher")]
use super::connect_sqlite_from_config;
use super::{
mark_mongo_legacy_driver, mongo_legacy_connect_params, MONGO_LEGACY_DRIVER_LABEL, MONGO_LEGACY_DRIVER_PROFILE,
};
use dbx_core::models::connection::{ConnectionConfig, DatabaseType};
#[cfg(feature = "mq-admin")]
use {
super::{load_connection_configs, save_connection_configs},
dbx_core::connection::{AppState, PoolKind},
dbx_core::storage::Storage,
use super::load_connection_configs;
use super::{
connect_sqlite_from_config, mark_mongo_legacy_driver, mongo_legacy_connect_params, save_connection_configs,
MONGO_LEGACY_DRIVER_LABEL, MONGO_LEGACY_DRIVER_PROFILE,
};
use dbx_core::connection::{AppState, PoolKind};
use dbx_core::models::connection::{AttachedDatabaseConfig, ConnectionConfig, DatabaseType};
use dbx_core::storage::Storage;
fn mongodb_config() -> ConnectionConfig {
ConnectionConfig {
@@ -231,7 +228,6 @@ mod tests {
}
}
#[cfg(feature = "sqlite-sqlcipher")]
fn sqlite_config(path: &std::path::Path, password: &str) -> ConnectionConfig {
let mut config = mongodb_config();
config.id = "sqlite".to_string();
@@ -249,6 +245,91 @@ mod tests {
config
}
#[tokio::test]
async fn sqlite_connect_from_config_restores_attached_databases() {
let dir = std::env::temp_dir().join(format!("dbx-tauri-sqlite-attach-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap();
let main_path = dir.join("main.sqlite");
let attached_path = dir.join("analytics.sqlite");
drop(dbx_core::db::sqlite::connect_path_create_if_missing(main_path.to_str().unwrap()).await.unwrap());
let attached =
dbx_core::db::sqlite::connect_path_create_if_missing(attached_path.to_str().unwrap()).await.unwrap();
dbx_core::db::sqlite::execute_query(&attached, "CREATE TABLE events(id INTEGER PRIMARY KEY);").await.unwrap();
drop(attached);
let mut config = sqlite_config(&main_path, "");
config.attached_databases.push(AttachedDatabaseConfig {
name: "analytics".to_string(),
path: attached_path.to_string_lossy().to_string(),
});
let pool = connect_sqlite_from_config(&config).await.expect("open SQLite connection with attachments");
let count = pool
.with_connection(|conn| {
conn.query_row(
"SELECT count(*) FROM analytics.sqlite_master WHERE type = 'table' AND name = 'events'",
[],
|row| row.get::<_, i64>(0),
)
.map_err(|error| error.to_string())
})
.expect("query attached SQLite database");
assert_eq!(count, 1);
drop(pool);
let _ = std::fs::remove_dir_all(dir);
}
#[tokio::test]
async fn sqlite_connect_from_config_rejects_sqlcipher_attachments_before_opening_files() {
let mut config = sqlite_config(std::path::Path::new("/missing/main.sqlite"), "secret");
config.attached_databases.push(AttachedDatabaseConfig {
name: "analytics".to_string(),
path: "/missing/analytics.sqlite".to_string(),
});
let error = match connect_sqlite_from_config(&config).await {
Ok(_) => panic!("SQLCipher attachments must be rejected"),
Err(error) => error,
};
assert!(error.contains("SQLCipher"), "{error}");
}
#[tokio::test]
async fn saving_memory_sqlite_attachments_keeps_the_live_pool_intact() {
let dir = std::env::temp_dir().join(format!("dbx-tauri-sqlite-memory-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap();
let storage = Storage::open(&dir.join("storage.db")).await.unwrap();
let state = AppState::new_with_plugin_dir(storage, dir.join("plugins"));
let initial = sqlite_config(std::path::Path::new(":memory:"), "");
let pool = dbx_core::db::sqlite::connect_path(":memory:").await.unwrap();
dbx_core::db::sqlite::execute_query(
&pool,
"CREATE TABLE retained(value TEXT); INSERT INTO retained VALUES ('yes');",
)
.await
.unwrap();
state.configs.write().await.insert(initial.id.clone(), initial.clone());
state.connections.write().await.insert(initial.id.clone(), PoolKind::Sqlite(pool.clone()));
let mut invalid = initial.clone();
invalid.attached_databases.push(AttachedDatabaseConfig {
name: "analytics".to_string(),
path: dir.join("analytics.sqlite").to_string_lossy().to_string(),
});
let error = save_connection_configs(&state, &[invalid]).await.unwrap_err();
assert!(error.contains("in-memory main database"), "{error}");
assert!(state.connections.read().await.contains_key(&initial.id));
assert_eq!(state.configs.read().await.get(&initial.id), Some(&initial));
let retained = dbx_core::db::sqlite::execute_query(&pool, "SELECT value FROM retained;").await.unwrap();
assert_eq!(retained.rows[0][0], serde_json::json!("yes"));
drop(pool);
drop(state);
let _ = std::fs::remove_dir_all(dir);
}
#[cfg(feature = "mq-admin")]
fn mq_config(id: &str, admin_url: &str) -> ConnectionConfig {
let mut config = mongodb_config();
@@ -466,6 +547,15 @@ pub async fn save_connections(state: State<'_, Arc<AppState>>, configs: Vec<Conn
}
async fn save_connection_configs(state: &AppState, configs: &[ConnectionConfig]) -> Result<(), String> {
for config in configs {
if config.db_type == DatabaseType::Sqlite {
db::sqlite::validate_persistent_attachments(
&config.host,
&config.password,
!config.attached_databases.is_empty(),
)?;
}
}
state.storage.save_connections(configs).await?;
let sync = sync_connection_configs(state, configs).await;
remove_connection_pools_for_connection_ids(state, &sync.connection_pool_ids_to_drop).await;
@@ -588,12 +678,18 @@ fn sqlite_extension_specs_from_config(config: &ConnectionConfig) -> Vec<db::sqli
}
async fn connect_sqlite_from_config(config: &ConnectionConfig) -> Result<db::sqlite::SqliteHandle, String> {
db::sqlite::connect_path_with_cipher_key_and_extensions(
&expand_tilde(&config.host),
let sqlite_path = expand_tilde(&config.host);
db::sqlite::validate_persistent_attachments(&sqlite_path, &config.password, !config.attached_databases.is_empty())?;
let pool = db::sqlite::connect_path_with_cipher_key_and_extensions(
&sqlite_path,
&config.password,
sqlite_extension_specs_from_config(config),
)
.await
.await?;
for attached in &config.attached_databases {
db::sqlite::attach_database(&pool, &attached.name, &expand_tilde(&attached.path))?;
}
Ok(pool)
}
#[tauri::command]
@@ -960,6 +1056,13 @@ pub async fn connect_db(
client_attempt: Option<u64>,
) -> Result<String, String> {
let config = config.canonicalized();
if config.db_type == DatabaseType::Sqlite {
db::sqlite::validate_persistent_attachments(
&config.host,
&config.password,
!config.attached_databases.is_empty(),
)?;
}
let id = config.id.clone();
let db_config = metadata_connection_config(&config);
let attempt = state.begin_connection_attempt_with_client_attempt(&id, client_attempt).await;
@@ -1260,6 +1363,13 @@ pub async fn connection_final_proxy_port(
if !runtime_config.has_effective_transport_layers() {
return Err("Connection has no configured transport layers".to_string());
}
if runtime_config.db_type == DatabaseType::Sqlite {
db::sqlite::validate_persistent_attachments(
&runtime_config.host,
&runtime_config.password,
!runtime_config.attached_databases.is_empty(),
)?;
}
let connection_id = runtime_config.id.clone();
let db_config = metadata_connection_config(&runtime_config);
+7
View File
@@ -350,6 +350,13 @@ pub fn build_duckdb_attach_database_sql(
Ok(dbx_core::db_admin_sql::build_duckdb_attach_database_sql(options))
}
#[tauri::command]
pub fn build_sqlite_attach_database_sql(
options: dbx_core::db_admin_sql::SqliteAttachDatabaseSqlOptions,
) -> Result<String, String> {
Ok(dbx_core::db_admin_sql::build_sqlite_attach_database_sql(options))
}
#[tauri::command]
pub fn build_drop_object_sql(options: dbx_core::db_admin_sql::DropObjectSqlOptions) -> Result<String, String> {
Ok(dbx_core::db_admin_sql::build_drop_object_sql(options))