Skip to content

Build your personal knowledge base with Trilium Notes

feat(setup): resume interrupted sync on restart

Elian Doran0f6a40fb041c
diff --git a/apps/client/src/setup.tsx b/apps/client/src/setup.tsx
index aa0f294a0f..5b4398e939 100644
--- a/apps/client/src/setup.tsx
+++ b/apps/client/src/setup.tsx
@@ -52,11 +52,26 @@ function renderState(state: State, setState: (state: State) => void) {
}
function App() {
- const [state, setState] = useState<State>("selectLanguage");
+ // A sync that already created the schema but was interrupted before finishing
+ // resumes straight on the progress screen instead of restarting the wizard.
+ const resuming = window.glob.syncInProgress === true;
+ const [state, setState] = useState<State>(resuming ? "syncFromServerInProgress" : "selectLanguage");
const [prevState, setPrevState] = useState<State | null>(null);
const [transitioning, setTransitioning] = useState(false);
const prevStateRef = useRef<State>(state);
+ useEffect(() => {
+ if (!resuming) {
+ return;
+ }
+ // The background sync timer stays gated behind DB initialization, so nothing
+ // restarts the interrupted sync on its own — kick it off like the launch-bar
+ // button does. sync/now is a no-op if a sync is somehow already running.
+ server.post("sync/now").catch(() => {
+ // Ignore — the progress screen keeps polling sync/stats regardless.
+ });
+ }, [resuming]);
+
function handleSetState(newState: State) {
setPrevState(prevStateRef.current);
prevStateRef.current = newState;
diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.ts
index 169a0c8d9f..9883b97caa 100644
--- a/packages/commons/src/lib/server_api.ts
+++ b/packages/commons/src/lib/server_api.ts
@@ -386,6 +386,13 @@ export interface DefinitionObject {
*/
export type BootstrapDefinition = {
dbInitialized: boolean;
+ /**
+ * Whether a sync that has already created the database schema was interrupted
+ * before finishing (schema exists but the `initialized` flag is not yet set).
+ * Only meaningful while `dbInitialized` is `false`; the setup screen uses it to
+ * jump straight back to the sync-in-progress step and resume the sync on restart.
+ */
+ syncInProgress?: boolean;
/**
* Whether a password has been set yet. `false` only in the pre-auth window
* after the database is initialized but before the user has set a password,
diff --git a/packages/trilium-core/src/services/bootstrap_utils.spec.ts b/packages/trilium-core/src/services/bootstrap_utils.spec.ts
index 6431635cdd..cff47d56f9 100644
--- a/packages/trilium-core/src/services/bootstrap_utils.spec.ts
+++ b/packages/trilium-core/src/services/bootstrap_utils.spec.ts
@@ -1,10 +1,11 @@
-import { afterEach, describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import attributes from "./attributes.js";
import getSharedBootstrapItems, { getIconConfig } from "./bootstrap_utils.js";
import * as cls from "./context.js";
import notes from "./notes.js";
import options from "./options.js";
+import sqlInit from "./sql_init.js";
const ASSET_PATH = "assets-x";
@@ -43,6 +44,21 @@ describe("bootstrap_utils (real DB)", () => {
expect(typeof items.iconPackCss).toBe("string");
});
+ it("flags syncInProgress for a not-yet-initialized DB only when the schema already exists", () => {
+ const schemaSpy = vi.spyOn(sqlInit, "schemaExists");
+ try {
+ // Schema present but DB not initialized = a sync that was interrupted → resume it.
+ schemaSpy.mockReturnValue(true);
+ expect(getSharedBootstrapItems(ASSET_PATH, false).syncInProgress).toBe(true);
+
+ // No schema yet = a fresh install → start the setup wizard from the top.
+ schemaSpy.mockReturnValue(false);
+ expect(getSharedBootstrapItems(ASSET_PATH, false).syncInProgress).toBe(false);
+ } finally {
+ schemaSpy.mockRestore();
+ }
+ });
+
it("returns the full payload once the DB is initialized, including app CSS note ids", () => {
const appCssNoteId = cls.init(() => {
const { note } = notes.createNewNote({
diff --git a/packages/trilium-core/src/services/bootstrap_utils.ts b/packages/trilium-core/src/services/bootstrap_utils.ts
index 88b44f38c6..0aab17aafe 100644
--- a/packages/trilium-core/src/services/bootstrap_utils.ts
+++ b/packages/trilium-core/src/services/bootstrap_utils.ts
@@ -8,6 +8,7 @@ import { getCurrentLocale } from "./i18n";
import attributes from "./attributes";
import BNote from "../becca/entities/bnote";
import { getPlatform } from "./platform";
+import sqlInit from "./sql_init";
export default function getSharedBootstrapItems(assetPath: string, dbInitialized: boolean) {
const sql = getSql();
@@ -29,6 +30,9 @@ export default function getSharedBootstrapItems(assetPath: string, dbInitialized
if (!dbInitialized) {
return {
...commonItems,
+ // Schema already present but not yet initialized means a sync was started
+ // and interrupted, so the setup screen resumes it instead of starting over.
+ syncInProgress: sqlInit.schemaExists(),
theme: "next",
themeCssUrl: false as const,
themeUseNextAsBase: "next" as const,