mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-08-04 11:43:57 +08:00
fix: avoid FK constraint failure when restoring provider_health during WebDAV sync
Split LOCAL_ONLY_TABLES into SYNC_SKIP_TABLES (export) and SYNC_PRESERVE_TABLES (import). provider_health is skipped on export but no longer restored on import, since it has a foreign key on providers(id, app_type) that may not match the remote dataset. Health data is ephemeral and rebuilds automatically at runtime.
This commit is contained in:
@@ -15,9 +15,8 @@ use tempfile::NamedTempFile;
|
|||||||
|
|
||||||
const CC_SWITCH_SQL_EXPORT_HEADER: &str = "-- CC Switch SQLite 导出";
|
const CC_SWITCH_SQL_EXPORT_HEADER: &str = "-- CC Switch SQLite 导出";
|
||||||
|
|
||||||
/// Tables that are local-only (not synced via WebDAV snapshots).
|
/// Tables whose data rows are skipped when exporting for WebDAV sync.
|
||||||
/// Schema is still exported, but data rows are skipped.
|
const SYNC_SKIP_TABLES: &[&str] = &[
|
||||||
const LOCAL_ONLY_TABLES: &[&str] = &[
|
|
||||||
"proxy_request_logs",
|
"proxy_request_logs",
|
||||||
"stream_check_logs",
|
"stream_check_logs",
|
||||||
"provider_health",
|
"provider_health",
|
||||||
@@ -25,6 +24,15 @@ const LOCAL_ONLY_TABLES: &[&str] = &[
|
|||||||
"usage_daily_rollups",
|
"usage_daily_rollups",
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/// Tables whose local data is preserved (restored from local snapshot) during WebDAV import.
|
||||||
|
/// Excludes ephemeral tables like provider_health that can safely rebuild at runtime.
|
||||||
|
const SYNC_PRESERVE_TABLES: &[&str] = &[
|
||||||
|
"proxy_request_logs",
|
||||||
|
"stream_check_logs",
|
||||||
|
"proxy_live_backup",
|
||||||
|
"usage_daily_rollups",
|
||||||
|
];
|
||||||
|
|
||||||
/// A database backup entry for the UI
|
/// A database backup entry for the UI
|
||||||
#[derive(Debug, serde::Serialize)]
|
#[derive(Debug, serde::Serialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
@@ -44,7 +52,7 @@ impl Database {
|
|||||||
/// Export SQL for sync (WebDAV), skipping local-only tables' data
|
/// Export SQL for sync (WebDAV), skipping local-only tables' data
|
||||||
pub fn export_sql_string_for_sync(&self) -> Result<String, AppError> {
|
pub fn export_sql_string_for_sync(&self) -> Result<String, AppError> {
|
||||||
let snapshot = self.snapshot_to_memory()?;
|
let snapshot = self.snapshot_to_memory()?;
|
||||||
Self::dump_sql(&snapshot, LOCAL_ONLY_TABLES)
|
Self::dump_sql(&snapshot, SYNC_SKIP_TABLES)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 导出为 SQLite 兼容的 SQL 文本
|
/// 导出为 SQLite 兼容的 SQL 文本
|
||||||
@@ -80,7 +88,7 @@ impl Database {
|
|||||||
/// Import SQL generated for sync, then restore local-only tables from the
|
/// Import SQL generated for sync, then restore local-only tables from the
|
||||||
/// current device snapshot before replacing the main database.
|
/// current device snapshot before replacing the main database.
|
||||||
pub(crate) fn import_sql_string_for_sync(&self, sql_raw: &str) -> Result<String, AppError> {
|
pub(crate) fn import_sql_string_for_sync(&self, sql_raw: &str) -> Result<String, AppError> {
|
||||||
self.import_sql_string_inner(sql_raw, LOCAL_ONLY_TABLES)
|
self.import_sql_string_inner(sql_raw, SYNC_PRESERVE_TABLES)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn import_sql_string_inner(
|
fn import_sql_string_inner(
|
||||||
|
|||||||
Reference in New Issue
Block a user