refactor: use pragma method in better-sqlite3 (#10684)

This commit is contained in:
Mohamed Akram 2025-12-01 23:43:06 +02:00 committed by GitHub
parent c4f5d12f3f
commit ec3ea10b44
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 11 deletions

View File

@ -138,7 +138,7 @@ export class BetterSqlite3Driver extends AbstractSqliteDriver {
nativeBinding = null,
prepareDatabase,
} = this.options
const databaseConnection = this.sqlite(database, {
const databaseConnection = new this.sqlite(database, {
readonly,
fileMustExist,
timeout,
@ -148,8 +148,8 @@ export class BetterSqlite3Driver extends AbstractSqliteDriver {
// in the options, if encryption key for SQLCipher is setted.
// Must invoke key pragma before trying to do any other interaction with the database.
if (this.options.key) {
databaseConnection.exec(
`PRAGMA key = ${JSON.stringify(this.options.key)}`,
databaseConnection.pragma(
`key = ${JSON.stringify(this.options.key)}`,
)
}
@ -160,11 +160,11 @@ export class BetterSqlite3Driver extends AbstractSqliteDriver {
// we need to enable foreign keys in sqlite to make sure all foreign key related features
// working properly. this also makes onDelete to work with sqlite.
databaseConnection.exec(`PRAGMA foreign_keys = ON`)
databaseConnection.pragma("foreign_keys = ON")
// turn on WAL mode to enhance performance
if (this.options.enableWAL) {
databaseConnection.exec(`PRAGMA journal_mode = WAL`)
databaseConnection.pragma("journal_mode = WAL")
}
return databaseConnection

View File

@ -62,14 +62,16 @@ export class BetterSqlite3QueryRunner extends AbstractSqliteQueryRunner {
* Called before migrations are run.
*/
async beforeMigration(): Promise<void> {
await this.query(`PRAGMA foreign_keys = OFF`)
const databaseConnection = await this.connect()
databaseConnection.pragma("foreign_keys = OFF")
}
/**
* Called after migrations are run.
*/
async afterMigration(): Promise<void> {
await this.query(`PRAGMA foreign_keys = ON`)
const databaseConnection = await this.connect()
databaseConnection.pragma("foreign_keys = ON")
}
/**
@ -172,10 +174,9 @@ export class BetterSqlite3QueryRunner extends AbstractSqliteQueryRunner {
}
protected async loadPragmaRecords(tablePath: string, pragma: string) {
const [database, tableName] = this.splitTablePath(tablePath)
const res = await this.query(
`PRAGMA ${
database ? `"${database}".` : ""
}${pragma}("${tableName}")`,
const databaseConnection = await this.connect()
const res = databaseConnection.pragma(
`${database ? `"${database}".` : ""}${pragma}("${tableName}")`,
)
return res
}