Skip to content

Build your personal knowledge base with Trilium Notes

fix(options): serve /api/options on an uninitialized database

Elian Doranb4dd91e76330
diff --git a/packages/trilium-core/src/routes/api/options.spec.ts b/packages/trilium-core/src/routes/api/options.spec.ts
index f84aef489a..e5e43acc02 100644
--- a/packages/trilium-core/src/routes/api/options.spec.ts
+++ b/packages/trilium-core/src/routes/api/options.spec.ts
@@ -20,6 +20,10 @@ function getOptionValue(name: string): string | null {
/** Forces the stored `syncServerHost` option, leaving every other option reading from the fixture. */
function mockSyncServerHost(syncServerHost: string) {
const originalGetOption = optionService.getOption.bind(optionService);
+ const originalGetOptionOrNull = optionService.getOptionOrNull.bind(optionService);
+ vi.spyOn(optionService, "getOptionOrNull").mockImplementation((name) =>
+ name === "syncServerHost" ? syncServerHost : originalGetOptionOrNull(name)
+ );
return vi.spyOn(optionService, "getOption").mockImplementation((name) =>
name === "syncServerHost" ? syncServerHost : originalGetOption(name)
);
diff --git a/packages/trilium-core/src/services/options.spec.ts b/packages/trilium-core/src/services/options.spec.ts
index bd94e8be3b..87d9ef9080 100644
--- a/packages/trilium-core/src/services/options.spec.ts
+++ b/packages/trilium-core/src/services/options.spec.ts
@@ -51,6 +51,28 @@ describe("options service (real DB)", () => {
}
});
+ it("does not query the options table while becca is not loaded and the table does not exist", () => {
+ // A query against a missing table fails and sql.ts logs it, even though the caller expects it.
+ const sql = getSql();
+ const originalGetValue = sql.getValue.bind(sql);
+ const getValueSpy = vi.spyOn(sql, "getValue").mockImplementation((query, params) =>
+ query.includes("sqlite_master") ? null : originalGetValue(query, params)
+ );
+ const getRowSpy = vi.spyOn(sql, "getRow");
+ const wasLoaded = becca.loaded;
+ becca.loaded = false;
+
+ try {
+ expect(optionService.getOptionOrNull("mainFontSize" as any)).toBeNull();
+ expect(getValueSpy).toHaveBeenCalledWith(expect.stringContaining("sqlite_master"));
+ expect(getRowSpy).not.toHaveBeenCalled();
+ } finally {
+ becca.loaded = wasLoaded;
+ getValueSpy.mockRestore();
+ getRowSpy.mockRestore();
+ }
+ });
+
it("throws from getOption for a non-existent option", () => {
expect(() => optionService.getOption("doesNotExistOption" as any)).toThrow(
/doesn't exist/
diff --git a/packages/trilium-core/src/services/options.ts b/packages/trilium-core/src/services/options.ts
index 3e36a26a7c..02f054b1ea 100644
--- a/packages/trilium-core/src/services/options.ts
+++ b/packages/trilium-core/src/services/options.ts
@@ -27,7 +27,13 @@ function getOptionOrNull(name: OptionNames): string | null {
} else {
// e.g. in initial sync becca is not loaded because DB is not initialized
try {
- option = getSql().getRow<OptionRow>("SELECT * FROM options WHERE name = ?", [name]);
+ const sql = getSql();
+ // Checked first because `sql.getRow()` logs a query against a missing table before it throws.
+ if (!sql.getValue("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'options'")) {
+ return null;
+ }
+
+ option = sql.getRow<OptionRow>("SELECT * FROM options WHERE name = ?", [name]);
} catch (e: unknown) {
// DB is not initialized.
return null;
diff --git a/packages/trilium-core/src/services/sync_options.spec.ts b/packages/trilium-core/src/services/sync_options.spec.ts
index ca61914d88..1992d32f3c 100644
--- a/packages/trilium-core/src/services/sync_options.spec.ts
+++ b/packages/trilium-core/src/services/sync_options.spec.ts
@@ -52,3 +52,47 @@ describe("syncOptions.getSyncTimeout", () => {
expect(getSyncTimeout()).toBe(120000); // fallback for invalid config
});
});
+
+describe("syncOptions.isSyncSetup", () => {
+ let isSyncSetup: () => boolean;
+ let getOptionOrNullMock: ReturnType<typeof vi.fn>;
+ let mockSyncConfig: Record<string, string | undefined>;
+
+ beforeEach(async () => {
+ vi.resetModules();
+ mockSyncConfig = {};
+ getOptionOrNullMock = vi.fn();
+
+ vi.doMock("./config.js", () => ({ default: { Sync: mockSyncConfig } }));
+ vi.doMock("./options.js", () => ({
+ default: {
+ getOption: () => { throw new Error("Option 'syncServerHost' doesn't exist"); },
+ getOptionOrNull: getOptionOrNullMock
+ }
+ }));
+
+ const mod = await import("./sync_options.js");
+ isSyncSetup = mod.default.isSyncSetup;
+ });
+
+ afterEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it("reports sync as not set up when the option does not exist yet", () => {
+ getOptionOrNullMock.mockReturnValue(null);
+ expect(isSyncSetup()).toBe(false);
+ });
+
+ it("follows the stored host, and the config override ahead of it", () => {
+ getOptionOrNullMock.mockReturnValue("https://sync.example.com");
+ expect(isSyncSetup()).toBe(true);
+
+ getOptionOrNullMock.mockReturnValue("");
+ expect(isSyncSetup()).toBe(false);
+
+ mockSyncConfig.syncServerHost = "disabled";
+ getOptionOrNullMock.mockReturnValue("https://sync.example.com");
+ expect(isSyncSetup()).toBe(false);
+ });
+});
diff --git a/packages/trilium-core/src/services/sync_options.ts b/packages/trilium-core/src/services/sync_options.ts
index 48fac8cc1e..281da2886a 100644
--- a/packages/trilium-core/src/services/sync_options.ts
+++ b/packages/trilium-core/src/services/sync_options.ts
@@ -23,7 +23,8 @@ export default {
return host ? normalizeUrl(host) : host;
},
isSyncSetup: () => {
- const syncServerHost = get("syncServerHost");
+ // The option does not exist before setup creates the database, which means sync is not set up.
+ const syncServerHost = config["Sync"]?.syncServerHost || optionService.getOptionOrNull("syncServerHost");
// special value "disabled" is here to support a use case where the document is configured with sync server,
// and we need to override it with config from config.ini