From 4672323dcb150a8ee5dd3f7b06c443406ba04359 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Thu, 13 Jul 2023 11:12:43 +0530 Subject: [PATCH] =?UTF-8?q?fix:=20use=20knex=20for=20checking=20values=20?= =?UTF-8?q?=F0=9F=98=94=20-=20open=20fd=20from=20better-sqlite3=20causes?= =?UTF-8?q?=20stalled=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/database/manager.ts | 59 +++++++++++++++---------------------- scripts/test.sh | 1 + 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/backend/database/manager.ts b/backend/database/manager.ts index 07235c3c..e75034ae 100644 --- a/backend/database/manager.ts +++ b/backend/database/manager.ts @@ -53,7 +53,7 @@ export class DatabaseManager extends DatabaseDemuxBase { return; } - const isFirstRun = this.#getIsFirstRun(); + const isFirstRun = await this.#getIsFirstRun(); if (isFirstRun) { await this.db!.migrate(); } @@ -80,7 +80,7 @@ export class DatabaseManager extends DatabaseDemuxBase { } async #executeMigration() { - const version = this.#getAppVersion(); + const version = await this.#getAppVersion(); const patches = await this.#getPatchesToExecute(version); const hasPatches = !!patches.pre.length || !!patches.post.length; @@ -174,43 +174,35 @@ export class DatabaseManager extends DatabaseDemuxBase { return await queryFunction(this.db!, ...args); } - #getIsFirstRun(): boolean { - const db = this.getDriver(); - if (!db) { + async #getIsFirstRun(): Promise { + const knex = this.db?.knex; + if (!knex) { return true; } - const noPatchRun = - db - .prepare( - `select name from sqlite_master - where - type = 'table' and - name = 'PatchRun'` - ) - .all().length === 0; - - db.close(); - return noPatchRun; + const query = await knex('sqlite_master').where({ + type: 'table', + name: 'PatchRun', + }); + return !query.length; } async #createBackup() { const { dbPath } = this.db ?? {}; - if (!dbPath) { + if (!dbPath || process.env.IS_TEST) { return; } - const backupPath = this.#getBackupFilePath(); + const backupPath = await this.#getBackupFilePath(); if (!backupPath) { return; } const db = this.getDriver(); - await db?.backup(backupPath); - db?.close(); + await db?.backup(backupPath).then(() => db.close()); } - #getBackupFilePath() { + async #getBackupFilePath() { const { dbPath } = this.db ?? {}; if (dbPath === ':memory:' || !dbPath) { return null; @@ -223,28 +215,23 @@ export class DatabaseManager extends DatabaseDemuxBase { const backupFolder = path.join(path.dirname(dbPath), 'backups'); const date = new Date().toISOString().split('.')[0]; - const version = this.#getAppVersion(); + const version = await this.#getAppVersion(); const backupFile = `${fileName}-${version}-${date}.books.db`; fs.ensureDirSync(backupFolder); return path.join(backupFolder, backupFile); } - #getAppVersion() { - const db = this.getDriver(); - if (!db) { + async #getAppVersion(): Promise { + const knex = this.db?.knex; + if (!knex) { return '0.0.0'; } - const query = db - .prepare( - `select value from SingleValue - where - fieldname = 'version' and - parent = 'SystemSettings'` - ) - .get() as undefined | { value: string }; - db.close(); - return query?.value || '0.0.0'; + const query = await knex('SingleValue') + .select('value') + .where({ fieldname: 'version', parent: 'SystemSettings' }); + const value = (query[0] as undefined | { value: string })?.value; + return value || '0.0.0'; } getDriver() { diff --git a/scripts/test.sh b/scripts/test.sh index a0774fda..451d0f05 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -5,4 +5,5 @@ if [ $# -eq 0 ] TEST_PATH=./**/tests/**/*.spec.ts fi +export IS_TEST=true ./scripts/runner.sh ./node_modules/.bin/tape $TEST_PATH | ./node_modules/.bin/tap-spec \ No newline at end of file