Skip to content

Commit

Permalink
fix: use knex for checking values 😔
Browse files Browse the repository at this point in the history
- open fd from better-sqlite3 causes stalled tests
  • Loading branch information
18alantom committed Jul 13, 2023
1 parent 823739f commit 4672323
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
59 changes: 23 additions & 36 deletions backend/database/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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;
Expand Down Expand Up @@ -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<boolean> {
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;
Expand All @@ -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<string> {
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() {
Expand Down
1 change: 1 addition & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4672323

Please sign in to comment.