Skip to content

Build your personal knowledge base with Trilium Notes

chore(standalone): port most of sql_init

Elian Doran794dab289415
diff --git a/apps/server/src/services/sql_init.ts b/apps/server/src/services/sql_init.ts
index b9319642f8..aa88a26a71 100644
--- a/apps/server/src/services/sql_init.ts
+++ b/apps/server/src/services/sql_init.ts
@@ -1,75 +1,27 @@
-import { deferred, type OptionRow } from "@triliumnext/commons";
-import { events as eventService } from "@triliumnext/core";
+import { type OptionRow } from "@triliumnext/commons";
+import { sql_init as coreSqlInit } from "@triliumnext/core";
import fs from "fs";
-import { t } from "i18next";
import BBranch from "../becca/entities/bbranch.js";
import BNote from "../becca/entities/bnote.js";
import BOption from "../becca/entities/boption.js";
-import backup from "./backup.js";
import cls from "./cls.js";
-import config from "./config.js";
import password from "./encryption/password.js";
import hidden_subtree from "./hidden_subtree.js";
import zipImportService from "./import/zip.js";
import log from "./log.js";
-import migrationService from "./migration.js";
import optionService from "./options.js";
-import port from "./port.js";
import resourceDir from "./resource_dir.js";
import sql from "./sql.js";
import TaskContext from "./task_context.js";
-import { isElectron } from "./utils.js";
-export const dbReady = deferred<void>();
-
-function schemaExists() {
- return !!sql.getValue(/*sql*/`SELECT name FROM sqlite_master
- WHERE type = 'table' AND name = 'options'`);
-}
-
-function isDbInitialized() {
- if (!schemaExists()) {
- return false;
- }
-
- const initialized = sql.getValue("SELECT value FROM options WHERE name = 'initialized'");
-
- return initialized === "true";
-}
-
-async function initDbConnection() {
- if (!isDbInitialized()) {
- if (isElectron) {
- log.info(t("sql_init.db_not_initialized_desktop"));
- } else {
- log.info(t("sql_init.db_not_initialized_server", { port }));
- }
-
- return;
- }
-
- await migrationService.migrateIfNecessary();
-
- sql.execute('CREATE TEMP TABLE IF NOT EXISTS "param_list" (`paramId` TEXT NOT NULL PRIMARY KEY)');
-
- sql.execute(`
- CREATE TABLE IF NOT EXISTS "user_data"
- (
- tmpID INT,
- username TEXT,
- email TEXT,
- userIDEncryptedDataKey TEXT,
- userIDVerificationHash TEXT,
- salt TEXT,
- derivedKey TEXT,
- isSetup TEXT DEFAULT "false",
- UNIQUE (tmpID),
- PRIMARY KEY (tmpID)
- );`);
-
- dbReady.resolve();
-}
+const schemaExists = coreSqlInit.schemaExists;
+const isDbInitialized = coreSqlInit.isDbInitialized;
+const dbReady = coreSqlInit.dbReady;
+const setDbAsInitialized = coreSqlInit.setDbAsInitialized;
+const initDbConnection = coreSqlInit.initDbConnection;
+const initializeDb = coreSqlInit.initializeDb;
+export const getDbSize = coreSqlInit.getDbSize;
/**
* Applies the database schema, creating the necessary tables and importing the demo content.
@@ -186,57 +138,6 @@ async function createDatabaseForSync(options: OptionRow[], syncServerHost = "",
log.info("Schema and not synced options generated.");
}
-function setDbAsInitialized() {
- if (!isDbInitialized()) {
- optionService.setOption("initialized", "true");
-
- initDbConnection();
-
- // Emit an event to notify that the database is now initialized
- eventService.emit(eventService.DB_INITIALIZED);
-
- log.info("Database initialization completed, emitted DB_INITIALIZED event");
- }
-}
-
-function optimize() {
- if (config.General.readOnly) {
- return;
- }
- log.info("Optimizing database");
- const start = Date.now();
-
- sql.execute("PRAGMA optimize");
-
- log.info(`Optimization finished in ${Date.now() - start}ms.`);
-}
-
-export function getDbSize() {
- return sql.getValue<number>("SELECT page_count * page_size / 1000 as size FROM pragma_page_count(), pragma_page_size()");
-}
-
-function initializeDb() {
- cls.init(initDbConnection);
-
- dbReady.then(() => {
- if (config.General && config.General.noBackup === true) {
- log.info("Disabling scheduled backups.");
-
- return;
- }
-
- setInterval(() => backup.regularBackup(), 4 * 60 * 60 * 1000);
-
- // kickoff first backup soon after start up
- setTimeout(() => backup.regularBackup(), 5 * 60 * 1000);
-
- // optimize is usually inexpensive no-op, so running it semi-frequently is not a big deal
- setTimeout(() => optimize(), 60 * 60 * 1000);
-
- setInterval(() => optimize(), 10 * 60 * 60 * 1000);
- });
-}
-
export default {
dbReady,
schemaExists,
diff --git a/packages/trilium-core/src/index.ts b/packages/trilium-core/src/index.ts
index 0d32698581..fe921598b3 100644
--- a/packages/trilium-core/src/index.ts
+++ b/packages/trilium-core/src/index.ts
@@ -10,6 +10,7 @@ import appInfo from "./services/app_info";
export type * from "./services/sql/types";
export * from "./services/sql/index";
+export { default as sql_init } from "./services/sql_init";
export * as protected_session from "./services/protected_session";
export { default as data_encryption } from "./services/encryption/data_encryption"
export * as binary_utils from "./services/utils/binary";
diff --git a/packages/trilium-core/src/services/sql_init.ts b/packages/trilium-core/src/services/sql_init.ts
index 00e804561a..1301a6b56a 100644
--- a/packages/trilium-core/src/services/sql_init.ts
+++ b/packages/trilium-core/src/services/sql_init.ts
@@ -1,14 +1,68 @@
import { deferred } from "@triliumnext/commons";
+import { getSql } from "./sql";
+import { getLog } from "./log";
+import { isElectron } from "./utils";
+import { t } from "i18next";
+import optionService from "./options";
+import eventService from "./events";
+import { getContext } from "./context";
+import config from "./config";
export const dbReady = deferred<void>();
-// TODO: Proper impl.
-setTimeout(() => {
- dbReady.resolve();
-}, 850);
+function schemaExists() {
+ return !!getSql().getValue(/*sql*/`SELECT name FROM sqlite_master
+ WHERE type = 'table' AND name = 'options'`);
+}
function isDbInitialized() {
- return true;
+ try {
+ if (!schemaExists()) {
+ return false;
+ }
+
+ const initialized = getSql().getValue("SELECT value FROM options WHERE name = 'initialized'");
+ return initialized === "true";
+ } catch (e) {
+ return false;
+ }
+}
+
+async function initDbConnection() {
+ if (!isDbInitialized()) {
+ const log = getLog();
+ if (isElectron) {
+ log.info(t("sql_init.db_not_initialized_desktop"));
+ } else {
+ // TODO: Bring back port.
+ log.info(t("sql_init.db_not_initialized_server", { port: 1234 }));
+ }
+
+ return;
+ }
+
+ //TODO: Renable migration
+ //await migrationService.migrateIfNecessary();
+
+ const sql = getSql();
+ sql.execute('CREATE TEMP TABLE IF NOT EXISTS "param_list" (`paramId` TEXT NOT NULL PRIMARY KEY)');
+
+ sql.execute(`
+ CREATE TABLE IF NOT EXISTS "user_data"
+ (
+ tmpID INT,
+ username TEXT,
+ email TEXT,
+ userIDEncryptedDataKey TEXT,
+ userIDVerificationHash TEXT,
+ salt TEXT,
+ derivedKey TEXT,
+ isSetup TEXT DEFAULT "false",
+ UNIQUE (tmpID),
+ PRIMARY KEY (tmpID)
+ );`);
+
+ dbReady.resolve();
}
async function createDatabaseForSync(a: any, b: string, c: any) {
@@ -16,15 +70,56 @@ async function createDatabaseForSync(a: any, b: string, c: any) {
}
function setDbAsInitialized() {
- // Noop.
-}
+ if (!isDbInitialized()) {
+ optionService.setOption("initialized", "true");
-function schemaExists() {
- return true;
+ initDbConnection();
+
+ // Emit an event to notify that the database is now initialized
+ eventService.emit(eventService.DB_INITIALIZED);
+
+ getLog().info("Database initialization completed, emitted DB_INITIALIZED event");
+ }
}
function getDbSize() {
- return 1000;
+ return getSql().getValue<number>("SELECT page_count * page_size / 1000 as size FROM pragma_page_count(), pragma_page_size()");
+}
+
+function optimize() {
+ if (config.General.readOnly) {
+ return;
+ }
+ const log = getLog();
+ log.info("Optimizing database");
+ const start = Date.now();
+
+ getSql().execute("PRAGMA optimize");
+
+ log.info(`Optimization finished in ${Date.now() - start}ms.`);
+}
+
+function initializeDb() {
+ getContext().init(initDbConnection);
+
+ dbReady.then(() => {
+ // TODO: Re-enable backup.
+ // if (config.General && config.General.noBackup === true) {
+ // log.info("Disabling scheduled backups.");
+
+ // return;
+ // }
+
+ // setInterval(() => backup.regularBackup(), 4 * 60 * 60 * 1000);
+
+ // // kickoff first backup soon after start up
+ // setTimeout(() => backup.regularBackup(), 5 * 60 * 1000);
+
+ // // optimize is usually inexpensive no-op, so running it semi-frequently is not a big deal
+ // setTimeout(() => optimize(), 60 * 60 * 1000);
+
+ // setInterval(() => optimize(), 10 * 60 * 60 * 1000);
+ });
}
-export default { isDbInitialized, createDatabaseForSync, setDbAsInitialized, schemaExists, getDbSize, dbReady };
+export default { isDbInitialized, createDatabaseForSync, setDbAsInitialized, schemaExists, getDbSize, initDbConnection, dbReady, initializeDb };