Move PRAGMA utils to a separate file, log out PRAGMA values on start

This commit is contained in:
Alex Gleason 2023-10-11 23:44:28 -05:00
parent f35d38d83b
commit 9686469c28
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 22 additions and 10 deletions

View File

@ -3,6 +3,7 @@ import path from 'node:path';
import { DenoSqlite3, DenoSqliteDialect, FileMigrationProvider, Kysely, Migrator, sql } from '@/deps.ts'; import { DenoSqlite3, DenoSqliteDialect, FileMigrationProvider, Kysely, Migrator, sql } from '@/deps.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { getPragma, setPragma } from '@/pragma.ts';
interface DittoDB { interface DittoDB {
events: EventRow; events: EventRow;
@ -61,22 +62,18 @@ const db = new Kysely<DittoDB>({
}), }),
}); });
// Set PRAGMA values.
await Promise.all([ await Promise.all([
setPragma(db, 'synchronous', 'normal'), setPragma(db, 'synchronous', 'normal'),
setPragma(db, 'temp_store', 'memory'), setPragma(db, 'temp_store', 'memory'),
setPragma(db, 'mmap_size', Conf.sqlite.mmapSize), setPragma(db, 'mmap_size', Conf.sqlite.mmapSize),
]); ]);
/** Set the PRAGMA and then read back its value to confirm. */ // Log out PRAGMA values for debugging.
async function setPragma(db: Kysely<any>, pragma: string, value: string | number) { ['journal_mode', 'synchronous', 'temp_store', 'mmap_size'].forEach(async (pragma) => {
await sql.raw(`PRAGMA ${pragma} = ${value}`).execute(db); const value = await getPragma(db, pragma);
const result = (await sql.raw(`PRAGMA ${pragma}`).execute(db)).rows[0] as object; console.log(`PRAGMA ${pragma} = ${value};`);
});
for (const [key, value] of Object.entries(result)) {
console.log(`PRAGMA ${key} = ${value};`);
break;
}
}
const migrator = new Migrator({ const migrator = new Migrator({
db, db,

15
src/pragma.ts Normal file
View File

@ -0,0 +1,15 @@
import { type Kysely, sql } from '@/deps.ts';
/** Set the PRAGMA and then read back its value to confirm. */
function setPragma(db: Kysely<any>, pragma: string, value: string | number) {
return sql.raw(`PRAGMA ${pragma} = ${value}`).execute(db);
}
/** Get value of PRAGMA from the database. */
async function getPragma(db: Kysely<any>, pragma: string) {
const result = await sql.raw(`PRAGMA ${pragma}`).execute(db);
const row = result.rows[0] as Record<string, unknown>;
return row[pragma];
}
export { getPragma, setPragma };