fix: 改为远端优先实时镜像并避免自动同步反向覆盖

This commit is contained in:
JSR
2026-03-20 17:34:27 +08:00
parent 7771177f9c
commit 75679e76c5
2 changed files with 30 additions and 16 deletions
+28 -14
View File
@@ -598,24 +598,38 @@ pub async fn realtime_dual_write_sync(app_state: &Arc<RwLock<AppState>>) -> Resu
state_guard.app_handle.clone() state_guard.app_handle.clone()
}; };
let can_sync = current_remote_and_local_from_state(&app_handle, app_state) let Some((local_conn, remote_conn)) =
.await? current_remote_and_local_from_state(&app_handle, app_state).await?
.is_some(); else {
if !can_sync {
return Ok(()); return Ok(());
};
// PostgreSQL 远端为主库,实时同步只做远端 -> 本地镜像,避免本地旧快照反向覆盖远端新值。
let remote_students = load_students(&remote_conn).await?;
for student in remote_students.values() {
let _ = upsert_student(&local_conn, student).await?;
} }
let result = db_sync_apply_internal( let remote_reasons = load_reasons(&remote_conn).await?;
ConflictStrategy::KeepRemote, for reason in remote_reasons.values() {
app_handle, let _ = upsert_reason(&local_conn, reason).await?;
app_state.clone(),
)
.await?;
if !result.success {
return Err(result
.message
.unwrap_or_else(|| "实时双写同步失败".to_string()));
} }
let remote_tags = load_tags(&remote_conn).await?;
for tag in remote_tags.values() {
let _ = upsert_tag(&local_conn, tag).await?;
}
let remote_events = load_events(&remote_conn).await?;
for event in remote_events.values() {
let _ = upsert_event(&local_conn, event).await?;
}
let remote_pairs = load_student_tag_pairs(&remote_conn).await?;
for pair in &remote_pairs {
let _ = ensure_student_tag_pair(&local_conn, pair).await?;
}
Ok(()) Ok(())
} }
+2 -2
View File
@@ -189,7 +189,7 @@ function MainContent(): React.JSX.Element {
if (conflicts.length > 0) { if (conflicts.length > 0) {
const recentLocalMutation = Date.now() - lastLocalMutationAtRef.current < 15000 const recentLocalMutation = Date.now() - lastLocalMutationAtRef.current < 15000
if (recentLocalMutation) { if (recentLocalMutation) {
const autoApplyRes = await api.dbSyncApply("keep_local") const autoApplyRes = await api.dbSyncApply("keep_remote")
if ( if (
autoApplyRes?.success && autoApplyRes?.success &&
autoApplyRes?.data?.success && autoApplyRes?.data?.success &&
@@ -206,7 +206,7 @@ function MainContent(): React.JSX.Element {
return return
} }
const applyRes = await api.dbSyncApply("keep_local") const applyRes = await api.dbSyncApply("keep_remote")
if (applyRes?.success && applyRes?.data?.success && applyRes?.data?.synced_records > 0) { if (applyRes?.success && applyRes?.data?.success && applyRes?.data?.synced_records > 0) {
window.dispatchEvent( window.dispatchEvent(
new CustomEvent("ss:data-updated", { detail: { category: "all", source: "sync" } }) new CustomEvent("ss:data-updated", { detail: { category: "all", source: "sync" } })