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()
};
let can_sync = current_remote_and_local_from_state(&app_handle, app_state)
.await?
.is_some();
if !can_sync {
let Some((local_conn, remote_conn)) =
current_remote_and_local_from_state(&app_handle, app_state).await?
else {
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(
ConflictStrategy::KeepRemote,
app_handle,
app_state.clone(),
)
.await?;
if !result.success {
return Err(result
.message
.unwrap_or_else(|| "实时双写同步失败".to_string()));
let remote_reasons = load_reasons(&remote_conn).await?;
for reason in remote_reasons.values() {
let _ = upsert_reason(&local_conn, reason).await?;
}
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(())
}
+2 -2
View File
@@ -189,7 +189,7 @@ function MainContent(): React.JSX.Element {
if (conflicts.length > 0) {
const recentLocalMutation = Date.now() - lastLocalMutationAtRef.current < 15000
if (recentLocalMutation) {
const autoApplyRes = await api.dbSyncApply("keep_local")
const autoApplyRes = await api.dbSyncApply("keep_remote")
if (
autoApplyRes?.success &&
autoApplyRes?.data?.success &&
@@ -206,7 +206,7 @@ function MainContent(): React.JSX.Element {
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) {
window.dispatchEvent(
new CustomEvent("ss:data-updated", { detail: { category: "all", source: "sync" } })