Skip to content

Build your personal knowledge base with Trilium Notes

fix(sync): mark the database as initialized when a resumed initial sync converges

Elian Doranf4fc51bb372c
diff --git a/packages/trilium-core/src/services/sync.spec.ts b/packages/trilium-core/src/services/sync.spec.ts
index 6328d59d20..ff69e6dadb 100644
--- a/packages/trilium-core/src/services/sync.spec.ts
+++ b/packages/trilium-core/src/services/sync.spec.ts
@@ -11,6 +11,7 @@ import options from "./options.js";
import { type ExecOpts, initRequest, type RequestProvider } from "./request.js";
import { getSql } from "./sql/index.js";
import setupService from "./setup.js";
+import sqlInit from "./sql_init.js";
import syncService, { estimateEntityChangeRecordSize } from "./sync.js";
import syncOptions from "./sync_options.js";
import dateUtils from "./utils/date.js";
@@ -115,6 +116,25 @@ describe("sync service", () => {
expect(urls.some((u) => u.includes("/api/sync/check"))).toBe(true);
});
+ it("marks the database as initialized once a sync converges", async () => {
+ // An initial sync-from-server interrupted by a crash resumes via sync/now or the sync
+ // timer — NOT via setup.triggerSync(), which is the only other place that sets the flag.
+ // If sync() itself doesn't set it on convergence, a resumed initial sync completes but
+ // the DB stays uninitialized and the setup screen waits on "finalizing" forever.
+ const initSpy = vi.spyOn(sqlInit, "setDbAsInitialized").mockImplementation(() => {});
+
+ await expect(runSync()).resolves.toEqual({ success: true });
+ expect(initSpy).toHaveBeenCalled();
+ });
+
+ it("does not mark the database as initialized when the sync fails", async () => {
+ const initSpy = vi.spyOn(sqlInit, "setDbAsInitialized").mockImplementation(() => {});
+ config.loginThrows = new Error("kaboom");
+
+ await expect(runSync()).resolves.toMatchObject({ success: false });
+ expect(initSpy).not.toHaveBeenCalled();
+ });
+
it("counts already-pulled local changes into the pull total so a resumed initial sync keeps the grand total", async () => {
// On resume the remote only reports the *remaining* changes; the already-pulled ones live in
// the local entity_changes table. The total must be their sum, otherwise the setup progress
diff --git a/packages/trilium-core/src/services/sync.ts b/packages/trilium-core/src/services/sync.ts
index d8fc2ffcbf..b3785eff90 100644
--- a/packages/trilium-core/src/services/sync.ts
+++ b/packages/trilium-core/src/services/sync.ts
@@ -73,6 +73,14 @@ async function sync() {
continueSync = await checkContentHash(syncContext);
} while (continueSync);
+ // A converged sync (everything pushed, everything pulled, content hashes verified)
+ // is what makes an initial sync-from-server database usable. setup.triggerSync()
+ // only marks it initialized when its own sync() call succeeds — an initial sync
+ // interrupted by a crash/restart resumes through sync/now or the sync timer, which
+ // would otherwise leave the flag unset forever and the setup screen stuck on
+ // "finalizing". Idempotent no-op on already-initialized databases.
+ sqlInit.setDbAsInitialized();
+
ws.syncFinished();
if (optionService.getOptionOrNull("syncIncomplete") === "true") {