feat: support busy_timeout param parameter for sqlite (#9623)

Co-authored-by: sinkhaha <1468709606@qq.com>
This commit is contained in:
sinkhaha 2022-12-29 21:52:55 +08:00 committed by GitHub
parent 4eda5df869
commit 8668c29d83
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 2 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "typeorm",
"version": "0.3.10",
"version": "0.3.11",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "typeorm",
"version": "0.3.10",
"version": "0.3.11",
"license": "MIT",
"dependencies": {
"@sqltools/formatter": "^1.2.2",

View File

@ -53,4 +53,12 @@ export interface SqliteConnectionOptions extends BaseDataSourceOptions {
readonly flags?: number
readonly poolSize?: never
/**
* Query or change the setting of the busy timeout.
* Time in milliseconds.
*
* @see https://www.sqlite.org/pragma.html#pragma_busy_timeout
*/
readonly busyTimeout?: number
}

View File

@ -173,6 +173,14 @@ export class SqliteDriver extends AbstractSqliteDriver {
await run(`PRAGMA journal_mode = WAL`)
}
if (
this.options.busyTimeout &&
typeof this.options.busyTimeout === "number" &&
this.options.busyTimeout > 0
) {
await run(`PRAGMA busy_timeout = ${this.options.busyTimeout}`)
}
// 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.
await run(`PRAGMA foreign_keys = ON`)

View File

@ -0,0 +1,32 @@
import "reflect-metadata"
import { expect } from "chai"
import { DataSource } from "../../../src/data-source/DataSource"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
describe("sqlite driver > busy-timeout", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
entities: [],
enabledDrivers: ["sqlite"],
driverSpecific: {
busyTimeout: 2000,
},
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))
it("should set the busy_timeout as expected", () =>
Promise.all(
connections.map(async (connection) => {
const result = await connection.query("PRAGMA busy_timeout")
expect(result).to.eql([{ timeout: 2000 }])
}),
))
})