|
1 |
+ |
//! The `SyncStore` integration (Groups p4 / GO -> SyncStore, M2).
|
|
2 |
+ |
//!
|
|
3 |
+ |
//! GoingsOn hand-rolled its cloud sync in `sync_service/` against the raw
|
|
4 |
+ |
//! `SyncKitClient`. To gain group sync (and shed the glue), it migrates to the
|
|
5 |
+ |
//! engine's `SyncStore`, which owns the changelog, triggers, HLC, apply, blobs,
|
|
6 |
+ |
//! and scheduler. This module assembles that store from GO's pieces:
|
|
7 |
+ |
//! - the [`goingson_schema`] manifest (declared in M1),
|
|
8 |
+ |
//! - [`AttachmentBlobs`], GO's blob policy,
|
|
9 |
+ |
//! - [`GoSyncObserver`], which forwards engine events to the frontend,
|
|
10 |
+ |
//! - and [`run_cutover`], the one-time local migration from GO's bookkeeping to
|
|
11 |
+ |
//! the engine's.
|
|
12 |
+ |
//!
|
|
13 |
+ |
//! The engine opens its own rusqlite connection (WAL) to the same `goingson.db`
|
|
14 |
+ |
//! file the sqlx pool uses; the two coexist by design.
|
|
15 |
+ |
|
|
16 |
+ |
// Assembled here; the M2 cutover wires build_store/run_cutover/GoSyncObserver into
|
|
17 |
+ |
// AppState, lib.rs setup, and commands/sync.rs. Until that flip lands they are
|
|
18 |
+ |
// only referenced across the module, so silence the not-yet-wired lint.
|
|
19 |
+ |
#![allow(dead_code)]
|
|
20 |
+ |
|
|
21 |
+ |
mod blobs;
|
|
22 |
+ |
mod observer;
|
|
23 |
+ |
|
|
24 |
+ |
use std::path::{Path, PathBuf};
|
|
25 |
+ |
use std::sync::Arc;
|
|
26 |
+ |
|
|
27 |
+ |
use rusqlite::{Connection, OptionalExtension};
|
|
28 |
+ |
use synckit_client::{DbSource, SyncKitClient, SyncStore};
|
|
29 |
+ |
|
|
30 |
+ |
use crate::sync_service::manifest::goingson_schema;
|
|
31 |
+ |
use blobs::AttachmentBlobs;
|
|
32 |
+ |
#[allow(unused_imports)] // wired into lib.rs setup when the M2 flip lands.
|
|
33 |
+ |
pub(crate) use observer::GoSyncObserver;
|
|
34 |
+ |
|
|
35 |
+ |
/// Build GoingsOn's `SyncStore` over the app's SQLite file. The device name and
|
|
36 |
+ |
/// platform default to the host's (the engine's default), matching GO's prior
|
|
37 |
+ |
/// registration behaviour.
|
|
38 |
+ |
pub(crate) fn build_store(
|
|
39 |
+ |
db_path: PathBuf,
|
|
40 |
+ |
data_dir: PathBuf,
|
|
41 |
+ |
client: SyncKitClient,
|
|
42 |
+ |
) -> Arc<SyncStore<SyncKitClient>> {
|
|
43 |
+ |
let store = SyncStore::builder(DbSource::path(db_path), client, goingson_schema())
|
|
44 |
+ |
.blob_policy(Arc::new(AttachmentBlobs::new(data_dir)))
|
|
45 |
+ |
.build();
|
|
46 |
+ |
Arc::new(store)
|
|
47 |
+ |
}
|
|
48 |
+ |
|
|
49 |
+ |
/// One-time migration of a device's local sync bookkeeping from GoingsOn's
|
|
50 |
+ |
/// hand-rolled triggers/changelog to the engine's. Idempotent and guarded by the
|
|
51 |
+ |
/// `syncstore_migrated` flag, so it is a cheap no-op on every launch after the
|
|
52 |
+ |
/// first. Runs synchronously (rusqlite) at app init, after the sqlx migrations
|
|
53 |
+ |
/// and before the scheduler spawns.
|
|
54 |
+ |
///
|
|
55 |
+ |
/// Steps:
|
|
56 |
+ |
/// 1. Open a configured connection — this runs the engine's `ensure_scope_schema`,
|
|
57 |
+ |
/// which adds `sync_changelog.scope` to a legacy changelog and seeds
|
|
58 |
+ |
/// `sync_scope_cursor('')` from the old single `pull_cursor`.
|
|
59 |
+ |
/// 2. Drop GO's legacy capture triggers (two naming conventions; see
|
|
60 |
+ |
/// [`legacy_sync_triggers`]).
|
|
61 |
+ |
/// 3. Run the engine's generated DDL: bookkeeping tables (`CREATE IF NOT EXISTS`)
|
|
62 |
+ |
/// and one trigger set per table (each with its own `DROP IF EXISTS`).
|
|
63 |
+ |
/// 4. Re-baseline. The server changelog is authoritative, so reset the personal
|
|
64 |
+ |
/// pull cursor to 0 (re-pull and re-apply idempotently) and clear
|
|
65 |
+ |
/// `initial_snapshot_done` (re-snapshot every local row for the first push, so
|
|
66 |
+ |
/// any local-only data survives the cutover). This is the one-time cost the
|
|
67 |
+ |
/// migration plan accepts.
|
|
68 |
+ |
///
|
|
69 |
+ |
/// The whole rewrite runs in one transaction; a failure leaves the legacy
|
|
70 |
+ |
/// triggers in place and the `syncstore_migrated` flag unset, so the next launch
|
|
71 |
+ |
/// retries from a clean state.
|
|
72 |
+ |
pub(crate) fn run_cutover(db_path: &Path) -> Result<(), String> {
|
|
73 |
+ |
let conn = DbSource::path(db_path).open().map_err(str_err)?;
|
|
74 |
+ |
|
|
75 |
+ |
if already_migrated(&conn)? {
|
|
76 |
+ |
return Ok(());
|
|
77 |
+ |
}
|
|
78 |
+ |
|
|
79 |
+ |
let legacy = legacy_sync_triggers(&conn)?;
|
|
80 |
+ |
|
|
81 |
+ |
let tx = conn.unchecked_transaction().map_err(str_err)?;
|
|
82 |
+ |
for name in &legacy {
|
|
83 |
+ |
tx.execute_batch(&format!("DROP TRIGGER IF EXISTS \"{name}\";"))
|
|
84 |
+ |
.map_err(str_err)?;
|
|
85 |
+ |
}
|
|
86 |
+ |
tx.execute_batch(&goingson_schema().migration_sql())
|
|
87 |
+ |
.map_err(str_err)?;
|
|
88 |
+ |
tx.execute_batch(
|
|
89 |
+ |
"UPDATE sync_scope_cursor SET cursor = 0 WHERE scope = '';\n\
|
|
90 |
+ |
UPDATE sync_state SET value = '0' WHERE key = 'pull_cursor';\n\
|
|
91 |
+ |
UPDATE sync_state SET value = '0' WHERE key = 'initial_snapshot_done';\n\
|
|
92 |
+ |
INSERT INTO sync_state (key, value) VALUES ('syncstore_migrated', '1')\n\
|
|
93 |
+ |
ON CONFLICT(key) DO UPDATE SET value = '1';",
|
|
94 |
+ |
)
|
|
95 |
+ |
.map_err(str_err)?;
|
|
96 |
+ |
tx.commit().map_err(str_err)?;
|
|
97 |
+ |
Ok(())
|
|
98 |
+ |
}
|
|
99 |
+ |
|
|
100 |
+ |
fn str_err<E: std::fmt::Display>(e: E) -> String {
|
|
101 |
+ |
e.to_string()
|
|
102 |
+ |
}
|
|
103 |
+ |
|
|
104 |
+ |
fn already_migrated(conn: &Connection) -> Result<bool, String> {
|
|
105 |
+ |
let v: Option<String> = conn
|
|
106 |
+ |
.query_row(
|
|
107 |
+ |
"SELECT value FROM sync_state WHERE key = 'syncstore_migrated'",
|
|
108 |
+ |
[],
|
|
109 |
+ |
|r| r.get(0),
|
|
110 |
+ |
)
|
|
111 |
+ |
.optional()
|
|
112 |
+ |
.map_err(str_err)?;
|
|
113 |
+ |
Ok(v.as_deref() == Some("1"))
|
|
114 |
+ |
}
|
|
115 |
+ |
|
|
116 |
+ |
/// GO's hand-written capture triggers use two naming conventions: `sync_trg_*`
|
|
117 |
+ |
/// (migration 030 and later) and `*_changelog` (migration 037's `sync_accounts`).
|
|
118 |
+ |
/// Both write to `sync_changelog` and must be dropped before the engine's own
|
|
119 |
+ |
/// triggers replace them. The engine's generated triggers are named
|
|
120 |
+ |
/// `sync_<table>_<op>` — no `sync_trg_` prefix, no `_changelog` suffix — so this
|
|
121 |
+ |
/// query never matches them. The FTS triggers on `emails` (`emails_ai/ad/au`)
|
|
122 |
+ |
/// also never match, so email full-text search is untouched.
|
|
123 |
+ |
fn legacy_sync_triggers(conn: &Connection) -> Result<Vec<String>, String> {
|
|
124 |
+ |
let mut stmt = conn
|
|
125 |
+ |
.prepare(
|
|
126 |
+ |
"SELECT name FROM sqlite_master WHERE type = 'trigger' \
|
|
127 |
+ |
AND (name LIKE 'sync\\_trg\\_%' ESCAPE '\\' \
|
|
128 |
+ |
OR name LIKE '%\\_changelog' ESCAPE '\\')",
|
|
129 |
+ |
)
|
|
130 |
+ |
.map_err(str_err)?;
|
|
131 |
+ |
let names = stmt
|
|
132 |
+ |
.query_map([], |r| r.get::<_, String>(0))
|
|
133 |
+ |
.map_err(str_err)?;
|
|
134 |
+ |
names.collect::<rusqlite::Result<Vec<_>>>().map_err(str_err)
|
|
135 |
+ |
}
|