@contember/database 2.2.0-alpha.12 → 2.2.0-alpha.14

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.
Files changed (32) hide show
  1. package/CLAUDE.md +3 -0
  2. package/dist/development/src/client/AcquiredConnection.cjs +2 -0
  3. package/dist/development/src/client/AcquiredConnection.cjs.map +1 -1
  4. package/dist/development/src/client/AcquiredConnection.js +2 -0
  5. package/dist/development/src/client/AcquiredConnection.js.map +1 -1
  6. package/dist/development/src/client/Connection.cjs +40 -4
  7. package/dist/development/src/client/Connection.cjs.map +1 -1
  8. package/dist/development/src/client/Connection.js +40 -4
  9. package/dist/development/src/client/Connection.js.map +1 -1
  10. package/dist/production/src/client/AcquiredConnection.cjs +2 -0
  11. package/dist/production/src/client/AcquiredConnection.cjs.map +1 -1
  12. package/dist/production/src/client/AcquiredConnection.js +2 -0
  13. package/dist/production/src/client/AcquiredConnection.js.map +1 -1
  14. package/dist/production/src/client/Connection.cjs +40 -4
  15. package/dist/production/src/client/Connection.cjs.map +1 -1
  16. package/dist/production/src/client/Connection.js +40 -4
  17. package/dist/production/src/client/Connection.js.map +1 -1
  18. package/dist/tests/cases/unit/createConnectionMockAlt.d.ts +6 -0
  19. package/dist/tests/cases/unit/createConnectionMockAlt.d.ts.map +1 -1
  20. package/dist/tests/cases/unit/staleConnectionRetry.test.d.ts +2 -0
  21. package/dist/tests/cases/unit/staleConnectionRetry.test.d.ts.map +1 -0
  22. package/dist/tests/tsconfig.tsbuildinfo +1 -1
  23. package/dist/types/client/AcquiredConnection.d.ts.map +1 -1
  24. package/dist/types/client/Connection.d.ts +11 -2
  25. package/dist/types/client/Connection.d.ts.map +1 -1
  26. package/dist/types/tsconfig.tsbuildinfo +1 -1
  27. package/package.json +3 -3
  28. package/src/client/AcquiredConnection.ts +2 -0
  29. package/src/client/Connection.ts +57 -2
  30. package/tests/cases/unit/connection.test.ts +45 -1
  31. package/tests/cases/unit/createConnectionMockAlt.ts +19 -2
  32. 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/index.js'
2
+ import { MutexDeadlockError, retryTransaction } from '../../../src/utils/index.js'
3
+ import { SerializationFailureError } from '../../../src/client/index.js'
4
+ import { ClientErrorCodes } from '../../../src/client/errorCodes.js'
3
5
  import { createConnectionMockAlt } from './createConnectionMockAlt.js'
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' }],
@@ -179,3 +187,39 @@ it('rejects closing a savepoint twice', async () => {
179
187
  })
180
188
  end()
181
189
  })
190
+
191
+ it('maps a PostgreSQL deadlock to a serialization failure', async () => {
192
+ const [connection, end] = createConnectionMockAlt([{ sql: 'SELECT 1', error: new PgError(ClientErrorCodes.T_R_DEADLOCK_DETECTED) }])
193
+
194
+ await expect(connection.query('SELECT 1')).rejects.toBeInstanceOf(SerializationFailureError)
195
+ end()
196
+ })
197
+
198
+ it('retries a transaction aborted by a PostgreSQL deadlock', async () => {
199
+ // a scope that throws disposes its connection, so the retry runs on a new one
200
+ const [connection, end] = createConnectionMockAlt(
201
+ [
202
+ { sql: 'BEGIN' },
203
+ { sql: 'UPDATE t SET v = 1', error: new PgError(ClientErrorCodes.T_R_DEADLOCK_DETECTED) },
204
+ { sql: 'ROLLBACK' },
205
+ ],
206
+ [
207
+ { sql: 'BEGIN' },
208
+ { sql: 'UPDATE t SET v = 1', result: { rowCount: 1 } },
209
+ { sql: 'COMMIT', result: { command: 'COMMIT' } },
210
+ ],
211
+ )
212
+
213
+ let attempts = 0
214
+ await retryTransaction(
215
+ () =>
216
+ connection.transaction(async trx => {
217
+ attempts++
218
+ await trx.query('UPDATE t SET v = 1')
219
+ }),
220
+ () => {},
221
+ { minTimeout: 0, maxTimeout: 0 },
222
+ )
223
+ expect(attempts).toBe(2)
224
+ end()
225
+ })
@@ -3,7 +3,8 @@ import { PgClient } from '../../../src/client/PgClient.js'
3
3
  import EventEmitter from 'node:events'
4
4
  import { expect } from 'bun:test'
5
5
 
6
- type MockQuery = { sql: string; timeout?: number; result?: any }
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 }
7
8
 
8
9
  export const createConnectionMockAlt = (...queries: MockQuery[][]): [Connection, () => void] => {
9
10
  return createConnectionMockAltWithPool({}, ...queries)
@@ -12,6 +13,16 @@ export const createConnectionMockAlt = (...queries: MockQuery[][]): [Connection,
12
13
  export const createConnectionMockAltWithPool = (
13
14
  poolConfig: Partial<Omit<PoolConfig, 'logError'>>,
14
15
  ...queries: MockQuery[][]
16
+ ): [Connection, () => void] => {
17
+ return createConnectionMockAltWithOptions({ pool: poolConfig }, ...queries)
18
+ }
19
+
20
+ export const createConnectionMockAltWithOptions = (
21
+ { pool: poolConfig = {}, connection: connectionOptions = {} }: {
22
+ pool?: Partial<Omit<PoolConfig, 'logError'>>
23
+ connection?: Connection.Options
24
+ },
25
+ ...queries: MockQuery[][]
15
26
  ): [Connection, () => void] => {
16
27
  const connectionMocks: (PgClient & { assertEmpty: () => void })[] = []
17
28
  for (const queriesSet of queries) {
@@ -35,6 +46,12 @@ export const createConnectionMockAltWithPool = (
35
46
  expect(query).toBeDefined()
36
47
  expect(sql).toEqual(query?.sql as string)
37
48
  await new Promise<void>(resolve => setTimeout(resolve, query?.timeout ?? 1))
49
+ if (query?.lost) {
50
+ this.emit('error', query.error)
51
+ }
52
+ if (query?.error) {
53
+ throw query.error
54
+ }
38
55
 
39
56
  return query?.result
40
57
  }
@@ -52,7 +69,7 @@ export const createConnectionMockAltWithPool = (
52
69
  })()
53
70
  }, { ...poolConfig, logError: () => null })
54
71
  return [
55
- new Connection(pool),
72
+ new Connection(pool, undefined, connectionOptions),
56
73
  () => {
57
74
  allMocks.forEach(it => it.assertEmpty())
58
75
  },
@@ -0,0 +1,86 @@
1
+ import { expect, it } from 'bun:test'
2
+ import { createConnectionMockAltWithOptions } from './createConnectionMockAlt.js'
3
+ import { Connection } from '../../../src/index.js'
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 = { connection: { 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
+ })