Set SQLite PRAGMAs on start

This commit is contained in:
Alex Gleason 2023-10-11 23:03:56 -05:00
parent 149f8f6f04
commit d63de0ad0b
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 33 additions and 2 deletions

View File

@ -144,9 +144,25 @@ const Conf = {
local(path: string): string {
return mergePaths(Conf.localDomain, path);
},
/** URL to send Sentry errors to. */
get sentryDsn() {
return Deno.env.get('SENTRY_DSN');
},
/** SQLite settings. */
sqlite: {
/**
* Number of bytes to use for memory-mapped IO.
* https://www.sqlite.org/pragma.html#pragma_mmap_size
*/
get mmapSize(): number {
const value = Deno.env.get('SQLITE_MMAP_SIZE');
if (value) {
return Number(value);
} else {
return 1024 * 1024 * 1024;
}
},
},
};
const optionalBooleanSchema = z

View File

@ -1,7 +1,7 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { DenoSqlite3, DenoSqliteDialect, FileMigrationProvider, Kysely, Migrator } from '@/deps.ts';
import { DenoSqlite3, DenoSqliteDialect, FileMigrationProvider, Kysely, Migrator, sql } from '@/deps.ts';
import { Conf } from '@/config.ts';
interface DittoDB {
@ -55,12 +55,27 @@ interface UnattachedMediaRow {
uploaded_at: Date;
}
const sqlite = new DenoSqlite3(Conf.dbPath);
setPragmas(sqlite, {
synchronous: 'normal',
temp_store: 'memory',
mmap_size: Conf.sqlite.mmapSize,
});
const db = new Kysely<DittoDB>({
dialect: new DenoSqliteDialect({
database: new DenoSqlite3(Conf.dbPath),
database: sqlite,
}),
});
function setPragmas(db: DenoSqlite3, pragmas: Record<string, string | number>) {
for (const [pragma, value] of Object.entries(pragmas)) {
db.prepare(`PRAGMA ${pragma} = ${value}`).run();
console.log(`PRAGMA ${pragma} = ${db.prepare(`PRAGMA ${pragma}`).value()}`);
}
}
const migrator = new Migrator({
db,
provider: new FileMigrationProvider({