Skip to content

Commit 9f1892a

Browse files
committed
...
1 parent 3412aab commit 9f1892a

2 files changed

Lines changed: 11 additions & 15 deletions

File tree

src/dialect/postgres/postgres-driver.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import { Driver, TransactionSettings } from '../../driver/driver.js'
88
import { parseSavepointCommand } from '../../parser/savepoint-parser.js'
99
import { CompiledQuery } from '../../query-compiler/compiled-query.js'
1010
import { QueryCompiler } from '../../query-compiler/query-compiler.js'
11-
import { ExecuteQueryOptions } from '../../query-executor/query-executor.js'
12-
import { AbortError, assertNotAborted } from '../../util/abort.js'
13-
import { Deferred } from '../../util/deferred.js'
1411
import { logOnce } from '../../util/log-once.js'
1512
import { isFunction, freeze } from '../../util/object-utils.js'
1613
import { createQueryId } from '../../util/query-id.js'
@@ -22,7 +19,6 @@ import {
2219
PostgresPoolClient,
2320
} from './postgres-dialect-config.js'
2421

25-
const PRIVATE_MAKE_CANCELABLE_METHOD = Symbol()
2622
const PRIVATE_RELEASE_METHOD = Symbol()
2723

2824
export class PostgresDriver implements Driver {
@@ -182,7 +178,7 @@ class PostgresConnection implements DatabaseConnection {
182178
): Promise<QueryResult<O>> {
183179
try {
184180
if (options?.cancelable) {
185-
await this[PRIVATE_MAKE_CANCELABLE_METHOD]()
181+
await this.#setupCancelability()
186182
}
187183

188184
const result = await this.#client.query<O>(compiledQuery.sql, [
@@ -222,7 +218,7 @@ class PostgresConnection implements DatabaseConnection {
222218
}
223219

224220
if (options?.cancelable) {
225-
await this[PRIVATE_MAKE_CANCELABLE_METHOD]()
221+
await this.#setupCancelability()
226222
}
227223

228224
const cursor = this.#client.query(
@@ -249,7 +245,11 @@ class PostgresConnection implements DatabaseConnection {
249245
}
250246
}
251247

252-
async [PRIVATE_MAKE_CANCELABLE_METHOD](): Promise<void> {
248+
[PRIVATE_RELEASE_METHOD](): void {
249+
this.#client.release()
250+
}
251+
252+
async #setupCancelability(): Promise<void> {
253253
if (this.#pid) {
254254
return
255255
}
@@ -263,8 +263,4 @@ class PostgresConnection implements DatabaseConnection {
263263

264264
this.#pid = row.pid
265265
}
266-
267-
[PRIVATE_RELEASE_METHOD](): void {
268-
this.#client.release()
269-
}
270266
}

test/node/src/cancellation.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ for (const dialect of DIALECTS) {
6464

6565
const delayedQuery = (
6666
{
67-
postgres: sql`select pg_sleep(1); ${writeQuery};`,
68-
mysql: sql`select sleep(1); ${writeQuery};`,
69-
mssql: sql`waitfor delay '00:00:01.000'; ${writeQuery};`,
67+
postgres: sql`select pg_sleep(0.1); ${writeQuery};`,
68+
mysql: sql`select sleep(0.1); ${writeQuery};`,
69+
mssql: sql`waitfor delay '00:00:00.100'; ${writeQuery};`,
7070
sqlite: sql`WITH RECURSIVE timer(i) AS (
7171
SELECT 1
7272
UNION ALL
@@ -88,7 +88,7 @@ for (const dialect of DIALECTS) {
8888
error.reason === 'aborted during query execution',
8989
)
9090

91-
// this checks the write after the sleep query was not executed due to the abort.
91+
await setTimeout(250) // long enough to ensure that if the query was not aborted, the write would have registered.
9292
await expect(
9393
ctx.db
9494
.selectFrom('person')

0 commit comments

Comments
 (0)