@contember/database 2.1.0-rc.4 → 2.1.0-rc.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +2 -0
- package/dist/development/src/client/AcquiredConnection.cjs +2 -0
- package/dist/development/src/client/AcquiredConnection.cjs.map +1 -1
- package/dist/development/src/client/AcquiredConnection.js +2 -0
- package/dist/development/src/client/AcquiredConnection.js.map +1 -1
- package/dist/development/src/client/Connection.cjs +41 -5
- package/dist/development/src/client/Connection.cjs.map +1 -1
- package/dist/development/src/client/Connection.js +41 -5
- package/dist/development/src/client/Connection.js.map +1 -1
- package/dist/production/src/client/AcquiredConnection.cjs +2 -0
- package/dist/production/src/client/AcquiredConnection.cjs.map +1 -1
- package/dist/production/src/client/AcquiredConnection.js +2 -0
- package/dist/production/src/client/AcquiredConnection.js.map +1 -1
- package/dist/production/src/client/Connection.cjs +41 -5
- package/dist/production/src/client/Connection.cjs.map +1 -1
- package/dist/production/src/client/Connection.js +41 -5
- package/dist/production/src/client/Connection.js.map +1 -1
- package/dist/tests/cases/unit/createConnectionMockAlt.d.ts +7 -2
- package/dist/tests/cases/unit/createConnectionMockAlt.d.ts.map +1 -1
- package/dist/tests/cases/unit/staleConnectionRetry.test.d.ts +2 -0
- package/dist/tests/cases/unit/staleConnectionRetry.test.d.ts.map +1 -0
- package/dist/tests/tsconfig.tsbuildinfo +1 -1
- package/dist/types/client/AcquiredConnection.d.ts.map +1 -1
- package/dist/types/client/Connection.d.ts +11 -2
- package/dist/types/client/Connection.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/client/AcquiredConnection.ts +2 -0
- package/src/client/Connection.ts +58 -3
- package/tests/cases/unit/connection.test.ts +45 -1
- package/tests/cases/unit/createConnectionMockAlt.ts +18 -2
- package/tests/cases/unit/staleConnectionRetry.test.ts +86 -0
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import { expect, it } from 'bun:test'
|
|
2
|
-
import { MutexDeadlockError } from '../../../src/utils'
|
|
2
|
+
import { MutexDeadlockError, retryTransaction } from '../../../src/utils'
|
|
3
|
+
import { SerializationFailureError } from '../../../src/client'
|
|
4
|
+
import { ClientErrorCodes } from '../../../src/client/errorCodes'
|
|
3
5
|
import { createConnectionMockAlt } from './createConnectionMockAlt'
|
|
4
6
|
|
|
7
|
+
class PgError extends Error {
|
|
8
|
+
constructor(public readonly code: string) {
|
|
9
|
+
super(`PostgreSQL error ${code}`)
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
5
13
|
it('support nested scope', async () => {
|
|
6
14
|
const [connection, end] = createConnectionMockAlt(
|
|
7
15
|
[{ sql: 'SELECT 1' }],
|
|
@@ -147,3 +155,39 @@ it('detects deadlock', async () => {
|
|
|
147
155
|
await expect(connection.scope(async c1 => c1.scope(async () => await c1.query('SELECT 1')))).rejects.toThrow(MutexDeadlockError)
|
|
148
156
|
end()
|
|
149
157
|
})
|
|
158
|
+
|
|
159
|
+
it('maps a PostgreSQL deadlock to a serialization failure', async () => {
|
|
160
|
+
const [connection, end] = createConnectionMockAlt([{ sql: 'SELECT 1', error: new PgError(ClientErrorCodes.T_R_DEADLOCK_DETECTED) }])
|
|
161
|
+
|
|
162
|
+
await expect(connection.query('SELECT 1')).rejects.toBeInstanceOf(SerializationFailureError)
|
|
163
|
+
end()
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('retries a transaction aborted by a PostgreSQL deadlock', async () => {
|
|
167
|
+
// a scope that throws disposes its connection, so the retry runs on a new one
|
|
168
|
+
const [connection, end] = createConnectionMockAlt(
|
|
169
|
+
[
|
|
170
|
+
{ sql: 'BEGIN' },
|
|
171
|
+
{ sql: 'UPDATE t SET v = 1', error: new PgError(ClientErrorCodes.T_R_DEADLOCK_DETECTED) },
|
|
172
|
+
{ sql: 'ROLLBACK' },
|
|
173
|
+
],
|
|
174
|
+
[
|
|
175
|
+
{ sql: 'BEGIN' },
|
|
176
|
+
{ sql: 'UPDATE t SET v = 1', result: { rowCount: 1 } },
|
|
177
|
+
{ sql: 'COMMIT', result: { command: 'COMMIT' } },
|
|
178
|
+
],
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
let attempts = 0
|
|
182
|
+
await retryTransaction(
|
|
183
|
+
() =>
|
|
184
|
+
connection.transaction(async trx => {
|
|
185
|
+
attempts++
|
|
186
|
+
await trx.query('UPDATE t SET v = 1')
|
|
187
|
+
}),
|
|
188
|
+
() => {},
|
|
189
|
+
{ minTimeout: 0, maxTimeout: 0 },
|
|
190
|
+
)
|
|
191
|
+
expect(attempts).toBe(2)
|
|
192
|
+
end()
|
|
193
|
+
})
|
|
@@ -3,7 +3,17 @@ import { PgClient } from '../../../src/client/PgClient'
|
|
|
3
3
|
import EventEmitter from 'node:events'
|
|
4
4
|
import { expect } from 'bun:test'
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
// `error` rejects the query; with `lost`, the client first emits `error` as pg does when the server closes the connection.
|
|
7
|
+
type MockQuery = { sql: string; timeout?: number; result?: any; error?: Error; lost?: boolean }
|
|
8
|
+
|
|
9
|
+
export const createConnectionMockAlt = (...queries: MockQuery[][]): [Connection, () => void] => {
|
|
10
|
+
return createConnectionMockAltWithOptions({}, ...queries)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const createConnectionMockAltWithOptions = (
|
|
14
|
+
connectionOptions: Connection.Options,
|
|
15
|
+
...queries: MockQuery[][]
|
|
16
|
+
): [Connection, () => void] => {
|
|
7
17
|
const connectionMocks: (PgClient & { assertEmpty: () => void })[] = []
|
|
8
18
|
for (const queriesSet of queries) {
|
|
9
19
|
connectionMocks.push(
|
|
@@ -21,6 +31,12 @@ export const createConnectionMockAlt = (...queries: { sql: string; timeout?: num
|
|
|
21
31
|
expect(query).toBeDefined()
|
|
22
32
|
expect(sql).toEqual(query?.sql as string)
|
|
23
33
|
await new Promise<void>(resolve => setTimeout(resolve, query?.timeout ?? 1))
|
|
34
|
+
if (query?.lost) {
|
|
35
|
+
this.emit('error', query.error)
|
|
36
|
+
}
|
|
37
|
+
if (query?.error) {
|
|
38
|
+
throw query.error
|
|
39
|
+
}
|
|
24
40
|
|
|
25
41
|
return query?.result
|
|
26
42
|
}
|
|
@@ -38,7 +54,7 @@ export const createConnectionMockAlt = (...queries: { sql: string; timeout?: num
|
|
|
38
54
|
})()
|
|
39
55
|
}, { logError: () => null })
|
|
40
56
|
return [
|
|
41
|
-
new Connection(pool),
|
|
57
|
+
new Connection(pool, undefined, connectionOptions),
|
|
42
58
|
() => {
|
|
43
59
|
allMocks.forEach(it => it.assertEmpty())
|
|
44
60
|
},
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { expect, it } from 'bun:test'
|
|
2
|
+
import { createConnectionMockAltWithOptions } from './createConnectionMockAlt'
|
|
3
|
+
import { Connection } from '../../../src'
|
|
4
|
+
|
|
5
|
+
const terminated = () => new Error('Connection terminated unexpectedly')
|
|
6
|
+
const adminShutdown = () => Object.assign(new Error('terminating connection due to administrator command'), { code: '57P01' })
|
|
7
|
+
const retrying = { retryStaleConnection: true }
|
|
8
|
+
|
|
9
|
+
const warmUp = async (connection: Connection) => {
|
|
10
|
+
await connection.query('SELECT 1')
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
it('runs the scope again when a pooled connection was lost before answering', async () => {
|
|
14
|
+
const [connection, end] = createConnectionMockAltWithOptions(
|
|
15
|
+
retrying,
|
|
16
|
+
[{ sql: 'SELECT 1' }, { sql: 'BEGIN', error: terminated(), lost: true }],
|
|
17
|
+
[{ sql: 'BEGIN' }, { sql: 'SELECT 2', result: { rows: [{ ok: true }] } }, { sql: 'COMMIT', result: { command: 'COMMIT' } }],
|
|
18
|
+
)
|
|
19
|
+
await warmUp(connection)
|
|
20
|
+
const rows = await connection.transaction(async trx => (await trx.query('SELECT 2')).rows)
|
|
21
|
+
expect(rows).toStrictEqual([{ ok: true }])
|
|
22
|
+
end()
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('runs the scope again when the server shut a pooled connection down', async () => {
|
|
26
|
+
const [connection, end] = createConnectionMockAltWithOptions(
|
|
27
|
+
retrying,
|
|
28
|
+
[{ sql: 'SELECT 1' }, { sql: 'SELECT 2', error: adminShutdown() }],
|
|
29
|
+
[{ sql: 'SELECT 2', result: { rows: [] } }],
|
|
30
|
+
)
|
|
31
|
+
await warmUp(connection)
|
|
32
|
+
await connection.query('SELECT 2')
|
|
33
|
+
end()
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('keeps going through stale idle connections until a working one', async () => {
|
|
37
|
+
const [connection, end] = createConnectionMockAltWithOptions(
|
|
38
|
+
retrying,
|
|
39
|
+
[{ sql: 'SELECT 1', timeout: 5 }, { sql: 'SELECT 2', error: terminated(), lost: true }],
|
|
40
|
+
[{ sql: 'SELECT 1', timeout: 5 }, { sql: 'SELECT 2', error: adminShutdown() }],
|
|
41
|
+
[{ sql: 'SELECT 2', result: { rows: [] } }],
|
|
42
|
+
)
|
|
43
|
+
await Promise.all([warmUp(connection), warmUp(connection)])
|
|
44
|
+
await connection.query('SELECT 2')
|
|
45
|
+
end()
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('does not retry without the option', async () => {
|
|
49
|
+
const [connection, end] = createConnectionMockAltWithOptions(
|
|
50
|
+
{},
|
|
51
|
+
[{ sql: 'SELECT 1' }, { sql: 'SELECT 2', error: terminated(), lost: true }],
|
|
52
|
+
)
|
|
53
|
+
await warmUp(connection)
|
|
54
|
+
await expect(connection.query('SELECT 2')).rejects.toThrow('Connection terminated unexpectedly')
|
|
55
|
+
end()
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('does not retry a new connection', async () => {
|
|
59
|
+
const [connection, end] = createConnectionMockAltWithOptions(
|
|
60
|
+
retrying,
|
|
61
|
+
[{ sql: 'SELECT 1', error: terminated(), lost: true }],
|
|
62
|
+
)
|
|
63
|
+
await expect(connection.query('SELECT 1')).rejects.toThrow('Connection terminated unexpectedly')
|
|
64
|
+
end()
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('does not retry once the connection answered a query', async () => {
|
|
68
|
+
const [connection, end] = createConnectionMockAltWithOptions(
|
|
69
|
+
retrying,
|
|
70
|
+
[{ sql: 'SELECT 1' }, { sql: 'BEGIN' }, { sql: 'SELECT 2', error: terminated(), lost: true }, { sql: 'ROLLBACK' }],
|
|
71
|
+
)
|
|
72
|
+
await warmUp(connection)
|
|
73
|
+
await expect(connection.transaction(trx => trx.query('SELECT 2'))).rejects.toThrow('Connection terminated unexpectedly')
|
|
74
|
+
end()
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
it('does not retry a query error on a live connection', async () => {
|
|
78
|
+
const syntaxError = Object.assign(new Error('syntax error'), { code: '42601' })
|
|
79
|
+
const [connection, end] = createConnectionMockAltWithOptions(
|
|
80
|
+
retrying,
|
|
81
|
+
[{ sql: 'SELECT 1' }, { sql: 'SELECT 2', error: syntaxError }],
|
|
82
|
+
)
|
|
83
|
+
await warmUp(connection)
|
|
84
|
+
await expect(connection.query('SELECT 2')).rejects.toThrow('syntax error')
|
|
85
|
+
end()
|
|
86
|
+
})
|