feat(cloud-sync): add selective backup and restore
This commit is contained in:
1 parent
e432304dbc
commit
a75d343666
23 files changed
+2427
-216
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -319,19 +319,28 @@ pub struct MigrationReport {
|
||||
/// re-encryption inside the storage boundary.
|
||||
pub(crate) struct SyncImportPlan {
|
||||
pub connections: Vec<ConnectionConfig>,
|
||||
pub merge_connections: bool,
|
||||
pub tunnel_profiles: Option<Vec<TransportLayerConfig>>,
|
||||
pub tunnel_secret_profiles: Option<Vec<TransportLayerConfig>>,
|
||||
pub merge_tunnel_profiles: bool,
|
||||
pub sidebar_layout: Option<serde_json::Value>,
|
||||
pub pinned_tree_node_ids: Vec<String>,
|
||||
pub pinned_tree_node_ids: Option<Vec<String>>,
|
||||
pub saved_sql: SavedSqlLibrary,
|
||||
pub merge_saved_sql: bool,
|
||||
pub desktop_settings: DesktopSettings,
|
||||
pub desktop_settings_keys: Option<Vec<String>>,
|
||||
pub editor_settings: Option<serde_json::Value>,
|
||||
pub merge_editor_settings: bool,
|
||||
pub editor_settings_keys: Option<Vec<String>>,
|
||||
pub connection_secrets: Option<Vec<SyncImportSecret>>,
|
||||
pub connection_secret_ids: Option<Vec<String>>,
|
||||
pub preserve_local_connection_strings: Vec<String>,
|
||||
/// Keep destination plugin credentials when the transport payload
|
||||
/// intentionally omitted plugin secrets.
|
||||
pub preserve_plugin_secrets: bool,
|
||||
pub sync_credentials: Option<Vec<SyncImportCredential>>,
|
||||
pub ai_configs: Option<Vec<AiConfigItem>>,
|
||||
pub merge_ai_configs: bool,
|
||||
}
|
||||
|
||||
pub(crate) struct SyncImportSecret {
|
||||
@@ -5924,25 +5933,47 @@ impl Storage {
|
||||
self.with_conn(move |conn| {
|
||||
let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate).map_err(|e| e.to_string())?;
|
||||
|
||||
apply_sync_connections_in_tx(&tx, &codec, &plan.connections)?;
|
||||
apply_sync_connections_in_tx(&tx, &codec, &plan.connections, plan.merge_connections)?;
|
||||
if let Some(profiles) = &plan.tunnel_profiles {
|
||||
apply_sync_tunnel_profiles_in_tx(&tx, &codec, profiles, plan.tunnel_secret_profiles.as_deref())?;
|
||||
apply_sync_tunnel_profiles_in_tx(
|
||||
&tx,
|
||||
&codec,
|
||||
profiles,
|
||||
plan.tunnel_secret_profiles.as_deref(),
|
||||
plan.merge_tunnel_profiles,
|
||||
)?;
|
||||
}
|
||||
if let Some(layout) = &plan.sidebar_layout {
|
||||
let json = serde_json::to_string(layout).map_err(|e| e.to_string())?;
|
||||
tx.execute("INSERT OR REPLACE INTO sidebar_layout (id, layout_json) VALUES (1, ?1)", [json])
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
let pinned = serde_json::to_string(&plan.pinned_tree_node_ids).map_err(|e| e.to_string())?;
|
||||
update_app_settings_key_in_tx(
|
||||
&tx,
|
||||
"pinned_tree_node_ids",
|
||||
serde_json::from_str(&pinned).map_err(|e| e.to_string())?,
|
||||
)?;
|
||||
apply_saved_sql_in_tx(&tx, &plan.saved_sql)?;
|
||||
apply_desktop_settings_in_tx(&tx, &plan.desktop_settings)?;
|
||||
if let Some(pinned_tree_node_ids) = &plan.pinned_tree_node_ids {
|
||||
let pinned = serde_json::to_string(pinned_tree_node_ids).map_err(|e| e.to_string())?;
|
||||
update_app_settings_key_in_tx(
|
||||
&tx,
|
||||
"pinned_tree_node_ids",
|
||||
serde_json::from_str(&pinned).map_err(|e| e.to_string())?,
|
||||
)?;
|
||||
}
|
||||
apply_saved_sql_in_tx(&tx, &plan.saved_sql, plan.merge_saved_sql)?;
|
||||
apply_desktop_settings_in_tx(&tx, &plan.desktop_settings, plan.desktop_settings_keys.as_deref())?;
|
||||
if let Some(editor_settings) = &plan.editor_settings {
|
||||
let value = serde_json::to_string(editor_settings).map_err(|e| e.to_string())?;
|
||||
let mut value = editor_settings.clone();
|
||||
if plan.merge_editor_settings {
|
||||
let current = tx
|
||||
.query_row(
|
||||
"SELECT value_json FROM app_state WHERE key = ?1",
|
||||
[APP_STATE_EDITOR_SETTINGS_KEY],
|
||||
|row| row.get::<_, String>(0),
|
||||
)
|
||||
.optional()
|
||||
.map_err(|e| e.to_string())?
|
||||
.and_then(|json| serde_json::from_str::<serde_json::Value>(&json).ok())
|
||||
.unwrap_or_else(|| serde_json::json!({}));
|
||||
value = merge_json_object_fields(current, value, plan.editor_settings_keys.as_deref());
|
||||
}
|
||||
let value = serde_json::to_string(&value).map_err(|e| e.to_string())?;
|
||||
tx.execute(
|
||||
"INSERT OR REPLACE INTO app_state (key, value_json) VALUES (?1, ?2)",
|
||||
params![APP_STATE_EDITOR_SETTINGS_KEY, value],
|
||||
@@ -5950,10 +5981,15 @@ impl Storage {
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
if let Some(ai_configs) = &plan.ai_configs {
|
||||
apply_ai_configs_in_tx(&tx, &codec, ai_configs)?;
|
||||
apply_ai_configs_in_tx(&tx, &codec, ai_configs, plan.merge_ai_configs)?;
|
||||
}
|
||||
if let Some(secrets) = &plan.connection_secrets {
|
||||
clear_sync_connection_secrets_in_tx(&tx, &plan.connections, plan.preserve_plugin_secrets)?;
|
||||
clear_sync_connection_secrets_in_tx(
|
||||
&tx,
|
||||
plan.connection_secret_ids.as_deref().unwrap_or(&[]),
|
||||
&plan.preserve_local_connection_strings,
|
||||
plan.preserve_plugin_secrets,
|
||||
)?;
|
||||
for secret in secrets {
|
||||
if secret.secret.is_empty() {
|
||||
continue;
|
||||
@@ -7949,12 +7985,17 @@ fn apply_sync_connections_in_tx(
|
||||
tx: &Transaction<'_>,
|
||||
codec: &SecretCodec,
|
||||
configs: &[ConnectionConfig],
|
||||
merge_existing: bool,
|
||||
) -> Result<(), String> {
|
||||
let replacement_ids = configs.iter().map(|config| config.id.clone()).collect::<HashSet<_>>();
|
||||
let mut retained_ids = preserve_unreadable_connections_for_replacement(tx, &replacement_ids)?;
|
||||
let mut retained_ids = if merge_existing {
|
||||
Vec::new()
|
||||
} else {
|
||||
let replacement_ids = configs.iter().map(|config| config.id.clone()).collect::<HashSet<_>>();
|
||||
preserve_unreadable_connections_for_replacement(tx, &replacement_ids)?
|
||||
};
|
||||
for config in configs {
|
||||
let config = config.canonicalized();
|
||||
if !config.save_password {
|
||||
if !config.save_password && !merge_existing {
|
||||
persist_secret_in_tx(tx, codec, &config.id, "password", "")?;
|
||||
delete_secret_prefix_in_tx(tx, &config.id, NACOS_AUTH_SECRET_PREFIX)?;
|
||||
}
|
||||
@@ -7965,29 +8006,51 @@ fn apply_sync_connections_in_tx(
|
||||
}
|
||||
let sanitized = sanitized_connection_config(&config);
|
||||
let json = serde_json::to_string(&sanitized).map_err(|e| e.to_string())?;
|
||||
tx.execute("INSERT INTO connections (id, config_json) VALUES (?1, ?2)", params![config.id, json])
|
||||
tx.execute("INSERT OR REPLACE INTO connections (id, config_json) VALUES (?1, ?2)", params![config.id, json])
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
retained_ids.extend(configs.iter().map(|config| config.id.clone()));
|
||||
delete_unreferenced_connection_secrets_in_tx(tx, &retained_ids)
|
||||
if merge_existing {
|
||||
Ok(())
|
||||
} else {
|
||||
delete_unreferenced_connection_secrets_in_tx(tx, &retained_ids)
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_sync_connection_secrets_in_tx(
|
||||
tx: &Transaction<'_>,
|
||||
configs: &[ConnectionConfig],
|
||||
connection_ids: &[String],
|
||||
preserve_connection_strings: &[String],
|
||||
preserve_plugin_secrets: bool,
|
||||
) -> Result<(), String> {
|
||||
for config in configs {
|
||||
if preserve_plugin_secrets {
|
||||
let preserve_connection_strings = preserve_connection_strings.iter().map(String::as_str).collect::<HashSet<_>>();
|
||||
for connection_id in connection_ids {
|
||||
let preserve_connection_string = preserve_connection_strings.contains(connection_id.as_str());
|
||||
if preserve_plugin_secrets && preserve_connection_string {
|
||||
tx.execute(
|
||||
"DELETE FROM connection_secrets
|
||||
WHERE connection_id = ?1
|
||||
AND key NOT LIKE 'plugin_connection.%'
|
||||
AND key <> 'connection_string'",
|
||||
[connection_id],
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
} else if preserve_plugin_secrets {
|
||||
tx.execute(
|
||||
"DELETE FROM connection_secrets
|
||||
WHERE connection_id = ?1
|
||||
AND key NOT LIKE 'plugin_connection.%'",
|
||||
[&config.id],
|
||||
[connection_id],
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
} else if preserve_connection_string {
|
||||
tx.execute(
|
||||
"DELETE FROM connection_secrets WHERE connection_id = ?1 AND key <> 'connection_string'",
|
||||
[connection_id],
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
} else {
|
||||
tx.execute("DELETE FROM connection_secrets WHERE connection_id = ?1", [&config.id])
|
||||
tx.execute("DELETE FROM connection_secrets WHERE connection_id = ?1", [connection_id])
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
}
|
||||
@@ -7999,6 +8062,7 @@ fn apply_sync_tunnel_profiles_in_tx(
|
||||
codec: &SecretCodec,
|
||||
profiles: &[TransportLayerConfig],
|
||||
secret_profiles: Option<&[TransportLayerConfig]>,
|
||||
merge_existing: bool,
|
||||
) -> Result<(), String> {
|
||||
let mut existing = HashMap::<String, TransportLayerConfig>::new();
|
||||
let mut statement = tx.prepare("SELECT id, config_json FROM tunnel_profiles").map_err(|e| e.to_string())?;
|
||||
@@ -8029,6 +8093,9 @@ fn apply_sync_tunnel_profiles_in_tx(
|
||||
Some(profile) => (profile, true),
|
||||
None => (profile.clone(), false),
|
||||
};
|
||||
if let Some(previous) = existing.get(profile.id()) {
|
||||
preserve_tunnel_profile_local_paths(&mut profile, previous);
|
||||
}
|
||||
if !has_synced_secrets {
|
||||
if let Some(previous) = existing.get(profile.id()) {
|
||||
merge_missing_tunnel_profile_secrets(&mut profile, previous);
|
||||
@@ -8037,10 +8104,19 @@ fn apply_sync_tunnel_profiles_in_tx(
|
||||
effective.push(profile);
|
||||
}
|
||||
|
||||
tx.execute("DELETE FROM tunnel_profiles", []).map_err(|e| e.to_string())?;
|
||||
tx.execute("DELETE FROM connection_secrets WHERE connection_id LIKE 'tunnel_profile.%'", [])
|
||||
.map_err(|e| e.to_string())?;
|
||||
if !merge_existing {
|
||||
tx.execute("DELETE FROM tunnel_profiles", []).map_err(|e| e.to_string())?;
|
||||
tx.execute("DELETE FROM connection_secrets WHERE connection_id LIKE 'tunnel_profile.%'", [])
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
for profile in effective {
|
||||
if merge_existing {
|
||||
tx.execute(
|
||||
"DELETE FROM connection_secrets WHERE connection_id = ?1",
|
||||
[format!("{TUNNEL_SECRET_NAMESPACE_PREFIX}{}", profile.id())],
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
let mut sanitized = profile.clone();
|
||||
sanitized.scrub_secrets();
|
||||
let json = serde_json::to_string(&sanitized).map_err(|e| e.to_string())?;
|
||||
@@ -8059,14 +8135,64 @@ fn apply_sync_tunnel_profiles_in_tx(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn apply_ai_configs_in_tx(tx: &Transaction<'_>, codec: &SecretCodec, configs: &[AiConfigItem]) -> Result<(), String> {
|
||||
tx.execute("DELETE FROM ai_configs", []).map_err(|e| e.to_string())?;
|
||||
tx.execute("DELETE FROM ai_config", []).map_err(|e| e.to_string())?;
|
||||
tx.execute("DELETE FROM ai_provider_configs", []).map_err(|e| e.to_string())?;
|
||||
tx.execute("DELETE FROM connection_secrets WHERE connection_id LIKE 'ai_config.%'", [])
|
||||
.map_err(|e| e.to_string())?;
|
||||
fn preserve_tunnel_profile_local_paths(remote: &mut TransportLayerConfig, local: &TransportLayerConfig) {
|
||||
if let (TransportLayerConfig::Ssh(remote), TransportLayerConfig::Ssh(local)) = (remote, local) {
|
||||
if remote.key_path.is_empty() {
|
||||
remote.key_path.clone_from(&local.key_path);
|
||||
}
|
||||
if remote.ssh_agent_sock_path.is_empty() {
|
||||
remote.ssh_agent_sock_path.clone_from(&local.ssh_agent_sock_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_ai_configs_in_tx(
|
||||
tx: &Transaction<'_>,
|
||||
codec: &SecretCodec,
|
||||
configs: &[AiConfigItem],
|
||||
merge_existing: bool,
|
||||
) -> Result<(), String> {
|
||||
let existing_configs = {
|
||||
let mut statement = tx.prepare("SELECT id, config_json FROM ai_configs").map_err(|error| error.to_string())?;
|
||||
let rows = statement
|
||||
.query_map([], |row| Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?)))
|
||||
.map_err(|error| error.to_string())?
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.map_err(|error| error.to_string())?;
|
||||
let mut existing = HashMap::with_capacity(rows.len());
|
||||
for (id, json) in rows {
|
||||
let config: AiConfig = serde_json::from_str(&json).map_err(|error| error.to_string())?;
|
||||
existing.insert(id, config);
|
||||
}
|
||||
existing
|
||||
};
|
||||
if !merge_existing {
|
||||
tx.execute("DELETE FROM ai_configs", []).map_err(|e| e.to_string())?;
|
||||
tx.execute("DELETE FROM ai_config", []).map_err(|e| e.to_string())?;
|
||||
tx.execute("DELETE FROM ai_provider_configs", []).map_err(|e| e.to_string())?;
|
||||
tx.execute("DELETE FROM connection_secrets WHERE connection_id LIKE 'ai_config.%'", [])
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
for item in configs {
|
||||
let (sanitized, secrets) = split_ai_config_secrets(&item.config)?;
|
||||
if merge_existing {
|
||||
tx.execute("DELETE FROM connection_secrets WHERE connection_id = ?1", [format!("ai_config.{}", item.id)])
|
||||
.map_err(|e| e.to_string())?;
|
||||
if item.is_default {
|
||||
tx.execute("UPDATE ai_configs SET is_default = 0", []).map_err(|e| e.to_string())?;
|
||||
}
|
||||
}
|
||||
let mut config = item.config.clone();
|
||||
clear_ai_config_device_paths(&mut config);
|
||||
let local = existing_configs.get(&item.id).or_else(|| {
|
||||
let mut matching_provider =
|
||||
existing_configs.values().filter(|local| local.provider.as_str() == config.provider.as_str());
|
||||
let candidate = matching_provider.next()?;
|
||||
matching_provider.next().is_none().then_some(candidate)
|
||||
});
|
||||
if let Some(local) = local {
|
||||
preserve_ai_config_device_paths(&mut config, local);
|
||||
}
|
||||
let (sanitized, secrets) = split_ai_config_secrets(&config)?;
|
||||
let json = serde_json::to_string(&sanitized).map_err(|e| e.to_string())?;
|
||||
let models_json = serde_json::to_string(&sanitized.models).map_err(|e| e.to_string())?;
|
||||
tx.execute(
|
||||
@@ -8087,6 +8213,39 @@ fn apply_ai_configs_in_tx(tx: &Transaction<'_>, codec: &SecretCodec, configs: &[
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn clear_ai_config_device_paths(config: &mut AiConfig) {
|
||||
config.codex_cli_path = None;
|
||||
config.claude_code_cli_path = None;
|
||||
config.pi_agent_cli_path = None;
|
||||
config.opencode_cli_path = None;
|
||||
config.cursor_cli_path = None;
|
||||
config.grok_cli_path = None;
|
||||
config.codebuddy_cli_path = None;
|
||||
config.qoder_cli_path = None;
|
||||
}
|
||||
|
||||
fn preserve_ai_config_device_paths(remote: &mut AiConfig, local: &AiConfig) {
|
||||
match (&remote.provider, &local.provider) {
|
||||
(AiProvider::CodexCli, AiProvider::CodexCli) => remote.codex_cli_path.clone_from(&local.codex_cli_path),
|
||||
(AiProvider::ClaudeCodeCli, AiProvider::ClaudeCodeCli) => {
|
||||
remote.claude_code_cli_path.clone_from(&local.claude_code_cli_path)
|
||||
}
|
||||
(AiProvider::PiAgentCli, AiProvider::PiAgentCli) => {
|
||||
remote.pi_agent_cli_path.clone_from(&local.pi_agent_cli_path)
|
||||
}
|
||||
(AiProvider::OpenCodeCli, AiProvider::OpenCodeCli) => {
|
||||
remote.opencode_cli_path.clone_from(&local.opencode_cli_path)
|
||||
}
|
||||
(AiProvider::CursorCli, AiProvider::CursorCli) => remote.cursor_cli_path.clone_from(&local.cursor_cli_path),
|
||||
(AiProvider::GrokCli, AiProvider::GrokCli) => remote.grok_cli_path.clone_from(&local.grok_cli_path),
|
||||
(AiProvider::CodeBuddyCli, AiProvider::CodeBuddyCli) => {
|
||||
remote.codebuddy_cli_path.clone_from(&local.codebuddy_cli_path)
|
||||
}
|
||||
(AiProvider::QoderCli, AiProvider::QoderCli) => remote.qoder_cli_path.clone_from(&local.qoder_cli_path),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn update_app_settings_key_in_tx(tx: &Transaction<'_>, key: &str, value: serde_json::Value) -> Result<(), String> {
|
||||
let current: Option<String> = tx
|
||||
.query_row("SELECT settings_json FROM app_settings WHERE id = 1", [], |row| row.get(0))
|
||||
@@ -8107,7 +8266,27 @@ fn update_app_settings_key_in_tx(tx: &Transaction<'_>, key: &str, value: serde_j
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn apply_desktop_settings_in_tx(tx: &Transaction<'_>, settings: &DesktopSettings) -> Result<(), String> {
|
||||
fn merge_json_object_fields(
|
||||
local: serde_json::Value,
|
||||
remote: serde_json::Value,
|
||||
selected_keys: Option<&[String]>,
|
||||
) -> serde_json::Value {
|
||||
let Some(remote) = remote.as_object() else { return remote };
|
||||
let mut merged = local.as_object().cloned().unwrap_or_default();
|
||||
for (key, value) in remote {
|
||||
if selected_keys.is_some_and(|keys| !keys.iter().any(|selected| selected == key)) {
|
||||
continue;
|
||||
}
|
||||
merged.insert(key.clone(), value.clone());
|
||||
}
|
||||
serde_json::Value::Object(merged)
|
||||
}
|
||||
|
||||
fn apply_desktop_settings_in_tx(
|
||||
tx: &Transaction<'_>,
|
||||
settings: &DesktopSettings,
|
||||
selected_keys: Option<&[String]>,
|
||||
) -> Result<(), String> {
|
||||
let mut values = serde_json::Map::new();
|
||||
values.insert("show_tray_icon".to_string(), serde_json::Value::Bool(settings.show_tray_icon));
|
||||
values.insert("icon_theme".to_string(), serde_json::to_value(settings.icon_theme).map_err(|e| e.to_string())?);
|
||||
@@ -8158,6 +8337,9 @@ fn apply_desktop_settings_in_tx(tx: &Transaction<'_>, settings: &DesktopSettings
|
||||
.transpose()?
|
||||
.unwrap_or_default();
|
||||
for (key, value) in values {
|
||||
if selected_keys.is_some_and(|keys| !keys.iter().any(|selected| selected == &key)) {
|
||||
continue;
|
||||
}
|
||||
merged.insert(key, value);
|
||||
}
|
||||
tx.execute(
|
||||
@@ -8168,19 +8350,34 @@ fn apply_desktop_settings_in_tx(tx: &Transaction<'_>, settings: &DesktopSettings
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn apply_saved_sql_in_tx(tx: &Transaction<'_>, library: &SavedSqlLibrary) -> Result<(), String> {
|
||||
tx.execute("DELETE FROM saved_sql_files", []).map_err(|e| e.to_string())?;
|
||||
tx.execute("DELETE FROM saved_sql_folders", []).map_err(|e| e.to_string())?;
|
||||
fn apply_saved_sql_in_tx(tx: &Transaction<'_>, library: &SavedSqlLibrary, merge_existing: bool) -> Result<(), String> {
|
||||
let mut folder_ids = HashSet::with_capacity(library.folders.len());
|
||||
for folder in &library.folders {
|
||||
if !folder_ids.insert(folder.id.as_str()) {
|
||||
return Err(format!("duplicate saved SQL folder id: {}", folder.id));
|
||||
}
|
||||
}
|
||||
let mut file_ids = HashSet::with_capacity(library.files.len());
|
||||
for file in &library.files {
|
||||
if !file_ids.insert(file.id.as_str()) {
|
||||
return Err(format!("duplicate saved SQL file id: {}", file.id));
|
||||
}
|
||||
}
|
||||
|
||||
if !merge_existing {
|
||||
tx.execute("DELETE FROM saved_sql_files", []).map_err(|e| e.to_string())?;
|
||||
tx.execute("DELETE FROM saved_sql_folders", []).map_err(|e| e.to_string())?;
|
||||
}
|
||||
for folder in &library.folders {
|
||||
tx.execute(
|
||||
"INSERT INTO saved_sql_folders (id, connection_id, parent_folder_id, name, order_index, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
"INSERT OR REPLACE INTO saved_sql_folders (id, connection_id, parent_folder_id, name, order_index, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
params![folder.id, folder.connection_id, folder.parent_folder_id, folder.name, folder.order_index, folder.created_at, folder.updated_at],
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
for file in &library.files {
|
||||
tx.execute(
|
||||
"INSERT INTO saved_sql_files (id, connection_id, folder_id, name, database_name, catalog_name, schema_name, sql_text, order_index, open_count, opened_at, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
"INSERT OR REPLACE INTO saved_sql_files (id, connection_id, folder_id, name, database_name, catalog_name, schema_name, sql_text, order_index, open_count, opened_at, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
params![file.id, file.connection_id, file.folder_id, file.name, file.database, file.catalog, file.schema, file.sql, file.order_index, file.open_count, file.opened_at, file.created_at, file.updated_at],
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
@@ -13402,10 +13599,12 @@ mod tests {
|
||||
incoming.url_params = Some("applicationName=dbx&sslmode=require".to_string());
|
||||
let plan = SyncImportPlan {
|
||||
connections: vec![incoming],
|
||||
merge_connections: false,
|
||||
tunnel_profiles: Some(Vec::new()),
|
||||
tunnel_secret_profiles: None,
|
||||
merge_tunnel_profiles: false,
|
||||
sidebar_layout: None,
|
||||
pinned_tree_node_ids: Vec::new(),
|
||||
pinned_tree_node_ids: Some(Vec::new()),
|
||||
saved_sql: SavedSqlLibrary {
|
||||
folders: vec![
|
||||
SavedSqlFolder {
|
||||
@@ -13429,12 +13628,19 @@ mod tests {
|
||||
],
|
||||
files: Vec::new(),
|
||||
},
|
||||
merge_saved_sql: false,
|
||||
desktop_settings: settings,
|
||||
desktop_settings_keys: None,
|
||||
editor_settings: None,
|
||||
merge_editor_settings: false,
|
||||
editor_settings_keys: None,
|
||||
connection_secrets: None,
|
||||
connection_secret_ids: None,
|
||||
preserve_local_connection_strings: Vec::new(),
|
||||
preserve_plugin_secrets: false,
|
||||
sync_credentials: None,
|
||||
ai_configs: None,
|
||||
merge_ai_configs: false,
|
||||
};
|
||||
assert!(storage.apply_sync_import_transaction(plan).await.is_err());
|
||||
let connections = storage.load_connections().await.unwrap();
|
||||
|
||||
@@ -1337,6 +1337,8 @@ async fn serve() {
|
||||
post(routes::cloud_sync::forget_webdav_sync_secrets_passphrase),
|
||||
)
|
||||
.route("/cloud-sync/webdav/upload", post(routes::cloud_sync::webdav_sync_upload))
|
||||
.route("/cloud-sync/catalog/local", post(routes::cloud_sync::cloud_sync_local_catalog))
|
||||
.route("/cloud-sync/webdav/inspect", post(routes::cloud_sync::webdav_sync_inspect))
|
||||
.route("/cloud-sync/webdav/download", post(routes::cloud_sync::webdav_sync_download))
|
||||
.route("/cloud-sync/snippet/test", post(routes::cloud_sync::snippet_sync_test))
|
||||
.route("/cloud-sync/snippet/token-status", post(routes::cloud_sync::snippet_token_status))
|
||||
@@ -1346,6 +1348,7 @@ async fn serve() {
|
||||
.route("/cloud-sync/snippet/save-id", post(routes::cloud_sync::save_snippet_sync_id))
|
||||
.route("/cloud-sync/snippet/retry-legacy-cleanup", post(routes::cloud_sync::retry_snippet_legacy_cleanup))
|
||||
.route("/cloud-sync/snippet/upload", post(routes::cloud_sync::snippet_sync_upload))
|
||||
.route("/cloud-sync/snippet/inspect", post(routes::cloud_sync::snippet_sync_inspect))
|
||||
.route("/cloud-sync/snippet/download", post(routes::cloud_sync::snippet_sync_download));
|
||||
|
||||
// Do not expose DuckDB-only handlers from builds that omit DuckDB sidecar support.
|
||||
|
||||
@@ -3,15 +3,16 @@ use std::sync::Arc;
|
||||
use axum::extract::State;
|
||||
use axum::Json;
|
||||
use dbx_core::cloud_sync::{
|
||||
apply_sync_snapshot, build_sync_snapshot_with_options, finalize_snippet_migration, forget_snippet_token,
|
||||
forget_webdav_password, forget_webdav_sync_secrets_passphrase as core_forget_webdav_sync_secrets_passphrase,
|
||||
resolve_snippet_token, resolve_webdav_password, resolve_webdav_sync_secrets_passphrase,
|
||||
retry_pending_snippet_cleanup, save_snippet_sync_id_for_instance as core_save_snippet_sync_id, save_snippet_token,
|
||||
save_webdav_password, save_webdav_sync_secrets_preference as core_save_webdav_sync_secrets_preference,
|
||||
snippet_saved_token_status, snippet_sync_settings_for_instance as core_snippet_sync_settings,
|
||||
webdav_saved_password_status, webdav_sync_secrets_status as core_webdav_sync_secrets_status, ApplySnapshotOptions,
|
||||
ApplySnapshotSummary, SnippetProvider, SnippetSyncClient, SnippetSyncConfig, SnippetSyncSettings,
|
||||
SnippetSyncSummary, SnippetTokenStatus, WebDavClient, WebDavConfig, WebDavPasswordStatus, WebDavSyncSecretsStatus,
|
||||
apply_sync_snapshot_with_selection, build_sync_snapshot_with_selection, describe_local_sync_state,
|
||||
describe_sync_snapshot, finalize_snippet_migration, forget_snippet_token, forget_webdav_password,
|
||||
forget_webdav_sync_secrets_passphrase as core_forget_webdav_sync_secrets_passphrase, resolve_snippet_token,
|
||||
resolve_webdav_password, resolve_webdav_sync_secrets_passphrase, retry_pending_snippet_cleanup,
|
||||
save_snippet_sync_id_for_instance as core_save_snippet_sync_id, save_snippet_token, save_webdav_password,
|
||||
save_webdav_sync_secrets_preference as core_save_webdav_sync_secrets_preference, snippet_saved_token_status,
|
||||
snippet_sync_settings_for_instance as core_snippet_sync_settings, webdav_saved_password_status,
|
||||
webdav_sync_secrets_status as core_webdav_sync_secrets_status, ApplySnapshotOptions, ApplySnapshotSummary,
|
||||
SnippetProvider, SnippetSyncClient, SnippetSyncConfig, SnippetSyncSettings, SnippetSyncSummary, SnippetTokenStatus,
|
||||
SyncSelection, SyncSnapshotCatalog, WebDavClient, WebDavConfig, WebDavPasswordStatus, WebDavSyncSecretsStatus,
|
||||
WebDavSyncSummary,
|
||||
};
|
||||
use dbx_core::storage::DesktopSettings;
|
||||
@@ -60,6 +61,8 @@ pub struct WebDavUploadRequest {
|
||||
/// Explicit opt-in for exporting credentials.
|
||||
#[serde(default)]
|
||||
pub include_secrets: bool,
|
||||
#[serde(default)]
|
||||
pub selection: Option<SyncSelection>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
@@ -72,6 +75,21 @@ pub struct WebDavDownloadRequest {
|
||||
/// restore behavior.
|
||||
#[serde(default = "default_restore_secrets")]
|
||||
pub restore_secrets: bool,
|
||||
#[serde(default)]
|
||||
pub selection: Option<SyncSelection>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct WebDavInspectRequest {
|
||||
pub config: WebDavConfig,
|
||||
pub secrets_passphrase: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct LocalCatalogRequest {
|
||||
pub editor_settings: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
fn default_restore_secrets() -> bool {
|
||||
@@ -107,6 +125,8 @@ pub struct SnippetUploadRequest {
|
||||
#[serde(default)]
|
||||
pub include_secrets: bool,
|
||||
pub secrets_passphrase: Option<String>,
|
||||
#[serde(default)]
|
||||
pub selection: Option<SyncSelection>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
@@ -117,6 +137,16 @@ pub struct SnippetDownloadRequest {
|
||||
#[serde(default)]
|
||||
pub restore_secrets: bool,
|
||||
pub secrets_passphrase: Option<String>,
|
||||
#[serde(default)]
|
||||
pub selection: Option<SyncSelection>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SnippetInspectRequest {
|
||||
pub config: SnippetSyncConfig,
|
||||
pub snippet_passphrase: Option<String>,
|
||||
pub secrets_passphrase: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
@@ -202,7 +232,7 @@ pub async fn webdav_sync_upload(
|
||||
None
|
||||
};
|
||||
let passphrase = explicit_passphrase.or(saved_passphrase.as_deref());
|
||||
let snapshot = build_sync_snapshot_with_options(
|
||||
let snapshot = build_sync_snapshot_with_selection(
|
||||
&state.app.storage,
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
req.editor_settings,
|
||||
@@ -213,12 +243,39 @@ pub async fn webdav_sync_upload(
|
||||
include_tunnel_secrets: req.include_secrets,
|
||||
include_plugin_secrets: req.include_secrets,
|
||||
},
|
||||
req.selection.as_ref(),
|
||||
Some(&state.app.plugins),
|
||||
)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
WebDavClient::new(req.config).put_snapshot(&snapshot).await.map(Json).map_err(AppError::from)
|
||||
}
|
||||
|
||||
pub async fn cloud_sync_local_catalog(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(req): Json<LocalCatalogRequest>,
|
||||
) -> Result<Json<SyncSnapshotCatalog>, AppError> {
|
||||
describe_local_sync_state(&state.app.storage, req.editor_settings, Some(&state.app.plugins))
|
||||
.await
|
||||
.map(Json)
|
||||
.map_err(AppError::from)
|
||||
}
|
||||
|
||||
pub async fn webdav_sync_inspect(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(mut req): Json<WebDavInspectRequest>,
|
||||
) -> Result<Json<SyncSnapshotCatalog>, AppError> {
|
||||
resolve_webdav_password(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
|
||||
let (snapshot, _) = WebDavClient::new(req.config).get_snapshot().await.map_err(AppError::from)?;
|
||||
let explicit = req.secrets_passphrase.as_deref().map(str::trim).filter(|value| !value.is_empty());
|
||||
let saved = if explicit.is_none() {
|
||||
resolve_webdav_sync_secrets_passphrase(&state.app.storage).await.map_err(AppError::from)?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
describe_sync_snapshot(&snapshot, explicit.or(saved.as_deref())).map(Json).map_err(AppError::from)
|
||||
}
|
||||
|
||||
pub async fn webdav_sync_download(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(mut req): Json<WebDavDownloadRequest>,
|
||||
@@ -231,20 +288,22 @@ pub async fn webdav_sync_download(
|
||||
} else {
|
||||
resolve_webdav_sync_secrets_passphrase(&state.app.storage).await.map_err(AppError::from)?
|
||||
};
|
||||
let apply_summary = apply_sync_snapshot(
|
||||
let apply_summary = apply_sync_snapshot_with_selection(
|
||||
&state.app.storage,
|
||||
&snapshot,
|
||||
ApplySnapshotOptions {
|
||||
secrets_passphrase: explicit_passphrase.or(saved_passphrase.as_deref()),
|
||||
restore_secrets: req.restore_secrets,
|
||||
},
|
||||
req.selection.as_ref(),
|
||||
Some(&state.app.plugins),
|
||||
)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
Ok(Json(WebDavDownloadResult {
|
||||
summary,
|
||||
editor_settings: snapshot.editor_settings,
|
||||
desktop_settings: snapshot.desktop_settings,
|
||||
editor_settings: state.app.storage.load_editor_settings().await.map_err(AppError::from)?,
|
||||
desktop_settings: state.app.storage.load_desktop_settings().await.map_err(AppError::from)?,
|
||||
apply_summary,
|
||||
}))
|
||||
}
|
||||
@@ -328,7 +387,7 @@ pub async fn snippet_sync_upload(
|
||||
None
|
||||
};
|
||||
let include_secrets = req.include_secrets;
|
||||
let snapshot = build_sync_snapshot_with_options(
|
||||
let snapshot = build_sync_snapshot_with_selection(
|
||||
&state.app.storage,
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
req.editor_settings,
|
||||
@@ -339,6 +398,8 @@ pub async fn snippet_sync_upload(
|
||||
include_tunnel_secrets: include_secrets,
|
||||
include_plugin_secrets: include_secrets,
|
||||
},
|
||||
req.selection.as_ref(),
|
||||
Some(&state.app.plugins),
|
||||
)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
@@ -361,24 +422,45 @@ pub async fn snippet_sync_download(
|
||||
.get_snapshot(req.snippet_passphrase.as_deref())
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
let apply_summary = apply_sync_snapshot(
|
||||
let apply_summary = apply_sync_snapshot_with_selection(
|
||||
&state.app.storage,
|
||||
&snapshot,
|
||||
ApplySnapshotOptions {
|
||||
secrets_passphrase: req.secrets_passphrase.as_deref(),
|
||||
restore_secrets: req.restore_secrets,
|
||||
},
|
||||
req.selection.as_ref(),
|
||||
Some(&state.app.plugins),
|
||||
)
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
Ok(Json(SnippetDownloadResult {
|
||||
summary,
|
||||
editor_settings: snapshot.editor_settings,
|
||||
desktop_settings: snapshot.desktop_settings,
|
||||
editor_settings: state.app.storage.load_editor_settings().await.map_err(AppError::from)?,
|
||||
desktop_settings: state.app.storage.load_desktop_settings().await.map_err(AppError::from)?,
|
||||
apply_summary,
|
||||
}))
|
||||
}
|
||||
|
||||
pub async fn snippet_sync_inspect(
|
||||
State(state): State<Arc<WebState>>,
|
||||
Json(mut req): Json<SnippetInspectRequest>,
|
||||
) -> Result<Json<SyncSnapshotCatalog>, AppError> {
|
||||
resolve_snippet_token(&state.app.storage, &mut req.config).await.map_err(AppError::from)?;
|
||||
let (snapshot, _) = SnippetSyncClient::new(req.config)
|
||||
.map_err(AppError::from)?
|
||||
.get_snapshot(req.snippet_passphrase.as_deref())
|
||||
.await
|
||||
.map_err(AppError::from)?;
|
||||
let explicit = req.secrets_passphrase.as_deref().map(str::trim).filter(|value| !value.is_empty());
|
||||
let saved = if explicit.is_none() {
|
||||
resolve_webdav_sync_secrets_passphrase(&state.app.storage).await.map_err(AppError::from)?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
describe_sync_snapshot(&snapshot, explicit.or(saved.as_deref())).map(Json).map_err(AppError::from)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
Reference in new issue
Block a user