integration-test: Set up system for resetting in-memory DB

This commit is contained in:
Elian Doran 2024-08-15 00:06:37 +03:00
parent d1f4d99c19
commit 317505484e
No known key found for this signature in database
4 changed files with 33 additions and 10 deletions

View File

@ -34,7 +34,7 @@ export default defineConfig({
webServer: { webServer: {
command: "npm run integration-mem-db", command: "npm run integration-mem-db",
url: "http://127.0.0.1:8082", url: "http://127.0.0.1:8082",
reuseExistingServer: false, reuseExistingServer: true,
stdout: "ignore", stdout: "ignore",
stderr: "pipe" stderr: "pipe"
}, },

View File

@ -7,6 +7,8 @@ import anonymizationService from "../../services/anonymization.js";
import consistencyChecksService from "../../services/consistency_checks.js"; import consistencyChecksService from "../../services/consistency_checks.js";
import { Request } from 'express'; import { Request } from 'express';
import ValidationError from "../../errors/validation_error.js"; 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() { function getExistingBackups() {
return backupService.getExistingBackups(); return backupService.getExistingBackups();
@ -28,6 +30,12 @@ function findAndFixConsistencyIssues() {
consistencyChecksService.runOnDemandChecks(true); consistencyChecksService.runOnDemandChecks(true);
} }
async function rebuildIntegrationTestDatabase() {
sql.rebuildIntegrationTestDatabase();
sql_init.initializeDb();
becca_loader.load();
}
function getExistingAnonymizedDatabases() { function getExistingAnonymizedDatabases() {
return anonymizationService.getExistingAnonymizedDatabases(); return anonymizationService.getExistingAnonymizedDatabases();
} }
@ -54,6 +62,7 @@ export default {
backupDatabase, backupDatabase,
vacuumDatabase, vacuumDatabase,
findAndFixConsistencyIssues, findAndFixConsistencyIssues,
rebuildIntegrationTestDatabase,
getExistingAnonymizedDatabases, getExistingAnonymizedDatabases,
anonymize, anonymize,
checkIntegrity checkIntegrity

View File

@ -299,6 +299,10 @@ function register(app: express.Application) {
route(PST, '/api/database/anonymize/:type', [auth.checkApiAuthOrElectron, csrfMiddleware], databaseRoute.anonymize, apiResultHandler, false); route(PST, '/api/database/anonymize/:type', [auth.checkApiAuthOrElectron, csrfMiddleware], databaseRoute.anonymize, apiResultHandler, false);
apiRoute(GET, '/api/database/anonymized-databases', databaseRoute.getExistingAnonymizedDatabases); 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 // backup requires execution outside of transaction
route(PST, '/api/database/backup-database', [auth.checkApiAuthOrElectron, csrfMiddleware], databaseRoute.backupDatabase, apiResultHandler, false); route(PST, '/api/database/backup-database', [auth.checkApiAuthOrElectron, csrfMiddleware], databaseRoute.backupDatabase, apiResultHandler, false);
apiRoute(GET, '/api/database/backups', databaseRoute.getExistingBackups); apiRoute(GET, '/api/database/backups', databaseRoute.getExistingBackups);

View File

@ -14,17 +14,28 @@ import ws from "./ws.js";
import becca_loader from "../becca/becca_loader.js"; import becca_loader from "../becca/becca_loader.js";
import entity_changes from "./entity_changes.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") { if (process.env.TRILIUM_INTEGRATION_TEST === "memory") {
// This allows a database that is read normally but is kept in memory and discards all modifications. return buildIntegrationTestDatabase();
const dbBuffer = fs.readFileSync(path);
return new Database(dbBuffer);
} }
return new Database(dataDir.DOCUMENT_PATH); 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) { if (!process.env.TRILIUM_INTEGRATION_TEST) {
dbConnection.pragma('journal_mode = WAL'); dbConnection.pragma('journal_mode = WAL');
@ -96,8 +107,6 @@ function upsert<T extends {}>(tableName: string, primaryKey: string, rec: T) {
execute(query, rec); execute(query, rec);
} }
const statementCache: Record<string, Statement> = {};
function stmt(sql: string) { function stmt(sql: string) {
if (!(sql in statementCache)) { if (!(sql in statementCache)) {
statementCache[sql] = dbConnection.prepare(sql); statementCache[sql] = dbConnection.prepare(sql);
@ -302,7 +311,7 @@ function fillParamList(paramIds: string[] | Set<string>, truncate = true) {
paramIds = paramIds.slice(0, 30000); 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(',')}`); const s = stmt(`INSERT INTO param_list VALUES ${paramIds.map(paramId => `(?)`).join(',')}`);
s.run(paramIds); s.run(paramIds);
@ -403,5 +412,6 @@ export default {
upsert, upsert,
fillParamList, fillParamList,
copyDatabase, copyDatabase,
disableSlowQueryLogging disableSlowQueryLogging,
rebuildIntegrationTestDatabase
}; };