From 317505484eb9aa4beb5194aef29fb28c8391c0db Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 15 Aug 2024 00:06:37 +0300 Subject: [PATCH] integration-test: Set up system for resetting in-memory DB --- playwright.config.ts | 2 +- src/routes/api/database.ts | 9 +++++++++ src/routes/routes.ts | 4 ++++ src/services/sql.ts | 28 +++++++++++++++++++--------- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/playwright.config.ts b/playwright.config.ts index d26fcf86c..313735158 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.ts index 8be084d12..6b66575c2 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, checkIntegrity diff --git a/src/routes/routes.ts b/src/routes/routes.ts index e808c1e0d..fd440804d 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 transaction route(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.ts index e352a7702..0f1cf5ea4 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 = {}; + +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(tableName: string, primaryKey: string, rec: T) { execute(query, rec); } -const statementCache: Record = {}; - function stmt(sql: string) { if (!(sql in statementCache)) { statementCache[sql] = dbConnection.prepare(sql); @@ -302,7 +311,7 @@ function fillParamList(paramIds: string[] | Set, 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 list const 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 };