Merge branch 'wal' into 'main'

Enable WAL, set some PRAGMA values at start

See merge request soapbox-pub/ditto!56
This commit is contained in:
Alex Gleason 2023-10-12 05:07:48 +00:00
commit 7ed34a0906
4 changed files with 54 additions and 0 deletions

View File

@ -144,9 +144,25 @@ const Conf = {
local(path: string): string { local(path: string): string {
return mergePaths(Conf.localDomain, path); return mergePaths(Conf.localDomain, path);
}, },
/** URL to send Sentry errors to. */
get sentryDsn() { get sentryDsn() {
return Deno.env.get('SENTRY_DSN'); 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 const optionalBooleanSchema = z

View File

@ -3,6 +3,7 @@ import path from 'node:path';
import { DenoSqlite3, DenoSqliteDialect, FileMigrationProvider, Kysely, Migrator } from '@/deps.ts'; import { DenoSqlite3, DenoSqliteDialect, FileMigrationProvider, Kysely, Migrator } 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,6 +62,19 @@ const db = new Kysely<DittoDB>({
}), }),
}); });
// Set PRAGMA values.
await Promise.all([
setPragma(db, 'synchronous', 'normal'),
setPragma(db, 'temp_store', 'memory'),
setPragma(db, 'mmap_size', Conf.sqlite.mmapSize),
]);
// Log out PRAGMA values for debugging.
['journal_mode', 'synchronous', 'temp_store', 'mmap_size'].forEach(async (pragma) => {
const value = await getPragma(db, pragma);
console.log(`PRAGMA ${pragma} = ${value};`);
});
const migrator = new Migrator({ const migrator = new Migrator({
db, db,
provider: new FileMigrationProvider({ provider: new FileMigrationProvider({

View File

@ -0,0 +1,9 @@
import { Kysely, sql } from '@/deps.ts';
export async function up(db: Kysely<any>): Promise<void> {
await sql`PRAGMA journal_mode = WAL`.execute(db);
}
export async function down(db: Kysely<any>): Promise<void> {
await sql`PRAGMA journal_mode = DELETE`.execute(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> | undefined;
return row?.[pragma];
}
export { getPragma, setPragma };