2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 10:58:59 +00:00

fix: use knex for checking values 😔

- open fd from better-sqlite3 causes stalled tests
This commit is contained in:
18alantom 2023-07-13 11:12:43 +05:30
parent 823739f564
commit 4672323dcb
2 changed files with 24 additions and 36 deletions

View File

@ -53,7 +53,7 @@ export class DatabaseManager extends DatabaseDemuxBase {
return; return;
} }
const isFirstRun = this.#getIsFirstRun(); const isFirstRun = await this.#getIsFirstRun();
if (isFirstRun) { if (isFirstRun) {
await this.db!.migrate(); await this.db!.migrate();
} }
@ -80,7 +80,7 @@ export class DatabaseManager extends DatabaseDemuxBase {
} }
async #executeMigration() { async #executeMigration() {
const version = this.#getAppVersion(); const version = await this.#getAppVersion();
const patches = await this.#getPatchesToExecute(version); const patches = await this.#getPatchesToExecute(version);
const hasPatches = !!patches.pre.length || !!patches.post.length; const hasPatches = !!patches.pre.length || !!patches.post.length;
@ -174,43 +174,35 @@ export class DatabaseManager extends DatabaseDemuxBase {
return await queryFunction(this.db!, ...args); return await queryFunction(this.db!, ...args);
} }
#getIsFirstRun(): boolean { async #getIsFirstRun(): Promise<boolean> {
const db = this.getDriver(); const knex = this.db?.knex;
if (!db) { if (!knex) {
return true; return true;
} }
const noPatchRun = const query = await knex('sqlite_master').where({
db type: 'table',
.prepare( name: 'PatchRun',
`select name from sqlite_master });
where return !query.length;
type = 'table' and
name = 'PatchRun'`
)
.all().length === 0;
db.close();
return noPatchRun;
} }
async #createBackup() { async #createBackup() {
const { dbPath } = this.db ?? {}; const { dbPath } = this.db ?? {};
if (!dbPath) { if (!dbPath || process.env.IS_TEST) {
return; return;
} }
const backupPath = this.#getBackupFilePath(); const backupPath = await this.#getBackupFilePath();
if (!backupPath) { if (!backupPath) {
return; return;
} }
const db = this.getDriver(); const db = this.getDriver();
await db?.backup(backupPath); await db?.backup(backupPath).then(() => db.close());
db?.close();
} }
#getBackupFilePath() { async #getBackupFilePath() {
const { dbPath } = this.db ?? {}; const { dbPath } = this.db ?? {};
if (dbPath === ':memory:' || !dbPath) { if (dbPath === ':memory:' || !dbPath) {
return null; return null;
@ -223,28 +215,23 @@ export class DatabaseManager extends DatabaseDemuxBase {
const backupFolder = path.join(path.dirname(dbPath), 'backups'); const backupFolder = path.join(path.dirname(dbPath), 'backups');
const date = new Date().toISOString().split('.')[0]; const date = new Date().toISOString().split('.')[0];
const version = this.#getAppVersion(); const version = await this.#getAppVersion();
const backupFile = `${fileName}-${version}-${date}.books.db`; const backupFile = `${fileName}-${version}-${date}.books.db`;
fs.ensureDirSync(backupFolder); fs.ensureDirSync(backupFolder);
return path.join(backupFolder, backupFile); return path.join(backupFolder, backupFile);
} }
#getAppVersion() { async #getAppVersion(): Promise<string> {
const db = this.getDriver(); const knex = this.db?.knex;
if (!db) { if (!knex) {
return '0.0.0'; return '0.0.0';
} }
const query = db const query = await knex('SingleValue')
.prepare( .select('value')
`select value from SingleValue .where({ fieldname: 'version', parent: 'SystemSettings' });
where const value = (query[0] as undefined | { value: string })?.value;
fieldname = 'version' and return value || '0.0.0';
parent = 'SystemSettings'`
)
.get() as undefined | { value: string };
db.close();
return query?.value || '0.0.0';
} }
getDriver() { getDriver() {

View File

@ -5,4 +5,5 @@ if [ $# -eq 0 ]
TEST_PATH=./**/tests/**/*.spec.ts TEST_PATH=./**/tests/**/*.spec.ts
fi fi
export IS_TEST=true
./scripts/runner.sh ./node_modules/.bin/tape $TEST_PATH | ./node_modules/.bin/tap-spec ./scripts/runner.sh ./node_modules/.bin/tape $TEST_PATH | ./node_modules/.bin/tap-spec