Skip to content

Commit

Permalink
fix: must invoke key pragma before any other interaction if SEE setted (
Browse files Browse the repository at this point in the history
#8478)

see documentation: https://www.sqlite.org/see/doc/release/www/readme.wiki

* fix: sqlite driver
* fix: better-sqlite3 driver

Closed: #8475
  • Loading branch information
yolopunk committed Jan 15, 2022
1 parent 26202b5 commit 546b3ed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
17 changes: 9 additions & 8 deletions src/driver/better-sqlite3/BetterSqlite3Driver.ts
Expand Up @@ -100,23 +100,24 @@ export class BetterSqlite3Driver extends AbstractSqliteDriver {
prepareDatabase
} = this.options;
const databaseConnection = this.sqlite(database, { readonly, fileMustExist, timeout, verbose });

// 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`);

// turn on WAL mode to enhance performance
databaseConnection.exec(`PRAGMA journal_mode = WAL`);

// 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)}`);
}

// function to run before a database is used in typeorm.
if (typeof prepareDatabase === "function") {
prepareDatabase(databaseConnection);
}

// 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`);

// turn on WAL mode to enhance performance
databaseConnection.exec(`PRAGMA journal_mode = WAL`);

return databaseConnection;
}

Expand Down
10 changes: 5 additions & 5 deletions src/driver/sqlite/SqliteDriver.ts
Expand Up @@ -107,6 +107,11 @@ export class SqliteDriver 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) {
await run(`PRAGMA key = ${JSON.stringify(this.options.key)};`);
}

if (this.options.enableWAL) {
await run(`PRAGMA journal_mode = WAL;`);
Expand All @@ -116,11 +121,6 @@ export class SqliteDriver extends AbstractSqliteDriver {
// working properly. this also makes onDelete to work with sqlite.
await run(`PRAGMA foreign_keys = ON;`);

// in the options, if encryption key for SQLCipher is setted.
if (this.options.key) {
await run(`PRAGMA key = ${JSON.stringify(this.options.key)};`);
}

return databaseConnection;
}

Expand Down

0 comments on commit 546b3ed

Please sign in to comment.