Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
integration-test: Set up system for resetting in-memory DB
Elian Doran317505484eb9
diff --git a/playwright.config.ts b/playwright.config.tsindex d26fcf86c4..3137351589 100644--- a/playwright.config.ts+++ b/playwright.config.ts@@ -34,7 +34,7 @@ export default defineConfig({webServer: {command: "npm run integration-mem-db",url: "http://127.0.0.1:8082",- reuseExistingServer: false,+ reuseExistingServer: true,stdout: "ignore",stderr: "pipe"},diff --git a/src/routes/api/database.ts b/src/routes/api/database.tsindex 8be084d12f..6b66575c2b 100644--- a/src/routes/api/database.ts+++ b/src/routes/api/database.ts@@ -7,6 +7,8 @@ import anonymizationService from "../../services/anonymization.js";import consistencyChecksService from "../../services/consistency_checks.js";import { Request } from 'express';import ValidationError from "../../errors/validation_error.js";+import sql_init from "../../services/sql_init.js";+import becca_loader from "../../becca/becca_loader.js";function getExistingBackups() {return backupService.getExistingBackups();@@ -28,6 +30,12 @@ function findAndFixConsistencyIssues() {consistencyChecksService.runOnDemandChecks(true);}+async function rebuildIntegrationTestDatabase() {+ sql.rebuildIntegrationTestDatabase();+ sql_init.initializeDb();+ becca_loader.load();+}+function getExistingAnonymizedDatabases() {return anonymizationService.getExistingAnonymizedDatabases();}@@ -54,6 +62,7 @@ export default {backupDatabase,vacuumDatabase,findAndFixConsistencyIssues,+ rebuildIntegrationTestDatabase,getExistingAnonymizedDatabases,anonymize,checkIntegritydiff --git a/src/routes/routes.ts b/src/routes/routes.tsindex e808c1e0d6..fd440804df 100644--- a/src/routes/routes.ts+++ b/src/routes/routes.ts@@ -299,6 +299,10 @@ function register(app: express.Application) {route(PST, '/api/database/anonymize/:type', [auth.checkApiAuthOrElectron, csrfMiddleware], databaseRoute.anonymize, apiResultHandler, false);apiRoute(GET, '/api/database/anonymized-databases', databaseRoute.getExistingAnonymizedDatabases);+ if (process.env.TRILIUM_INTEGRATION_TEST === "memory") {+ route(PST, '/api/database/rebuild/', [auth.checkApiAuthOrElectron], databaseRoute.rebuildIntegrationTestDatabase, apiResultHandler, false);+ }+// backup requires execution outside of transactionroute(PST, '/api/database/backup-database', [auth.checkApiAuthOrElectron, csrfMiddleware], databaseRoute.backupDatabase, apiResultHandler, false);apiRoute(GET, '/api/database/backups', databaseRoute.getExistingBackups);diff --git a/src/services/sql.ts b/src/services/sql.tsindex e352a77020..0f1cf5ea48 100644--- a/src/services/sql.ts+++ b/src/services/sql.ts@@ -14,17 +14,28 @@ import ws from "./ws.js";import becca_loader from "../becca/becca_loader.js";import entity_changes from "./entity_changes.js";-function buildDatabase(path: string) {+let dbConnection: DatabaseType = buildDatabase();+let statementCache: Record<string, Statement> = {};++function buildDatabase() {if (process.env.TRILIUM_INTEGRATION_TEST === "memory") {- // This allows a database that is read normally but is kept in memory and discards all modifications.- const dbBuffer = fs.readFileSync(path);- return new Database(dbBuffer);+ return buildIntegrationTestDatabase();}return new Database(dataDir.DOCUMENT_PATH);}-const dbConnection: DatabaseType = buildDatabase(dataDir.DOCUMENT_PATH);+function buildIntegrationTestDatabase() {+ const dbBuffer = fs.readFileSync(dataDir.DOCUMENT_PATH);+ return new Database(dbBuffer);+}++function rebuildIntegrationTestDatabase() {+ // This allows a database that is read normally but is kept in memory and discards all modifications.+ dbConnection = buildIntegrationTestDatabase();+ statementCache = {};+}+if (!process.env.TRILIUM_INTEGRATION_TEST) {dbConnection.pragma('journal_mode = WAL');@@ -96,8 +107,6 @@ function upsert<T extends {}>(tableName: string, primaryKey: string, rec: T) {execute(query, rec);}-const statementCache: Record<string, Statement> = {};-function stmt(sql: string) {if (!(sql in statementCache)) {statementCache[sql] = dbConnection.prepare(sql);@@ -302,7 +311,7 @@ function fillParamList(paramIds: string[] | Set<string>, truncate = true) {paramIds = paramIds.slice(0, 30000);}- // doing it manually to avoid this showing up on the sloq query list+ // doing it manually to avoid this showing up on the slow query listconst s = stmt(`INSERT INTO param_list VALUES ${paramIds.map(paramId => `(?)`).join(',')}`);s.run(paramIds);@@ -403,5 +412,6 @@ export default {upsert,fillParamList,copyDatabase,- disableSlowQueryLogging+ disableSlowQueryLogging,+ rebuildIntegrationTestDatabase};