• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / storage / 30089661594

24 Jul 2026 11:28AM UTC coverage: 80.756% (+0.4%) from 80.352%
30089661594

Pull #1043

github

web-flow
Merge 583fb7624 into e7b975a46
Pull Request #1043: feat: add queue management handlers

5829 of 7763 branches covered (75.09%)

Branch coverage included in aggregate %.

270 of 296 new or added lines in 6 files covered. (91.22%)

11066 of 13158 relevant lines covered (84.1%)

409.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.24
/src/internal/queue/database.ts
1
import EventEmitter from 'node:events'
2
import type { DatabaseExecutor } from '@internal/database/connection'
3
import { ERRORS } from '@internal/errors'
4
import pg from 'pg'
5
import { Db } from 'pg-boss'
6
import { type PgBeginTransactionOptions, PgPoolExecutor } from '../database/pg-connection'
7

8
export { quoteIdentifier } from '../database/sql'
9

10
export class QueueDB extends EventEmitter implements Db {
11
  opened = false
2✔
12
  isOurs = true
2✔
13
  events = {
2✔
14
    error: 'error',
15
  }
16
  protected config: pg.PoolConfig
17
  protected pool?: pg.Pool
18

19
  constructor(config: pg.PoolConfig) {
20
    super()
2✔
21
    this.config = config
2✔
22
  }
23

24
  async open() {
25
    this.pool = new pg.Pool({ ...this.config, min: 0 })
2✔
26
    this.pool.on('error', (error) => this.emit('error', error))
2✔
27

28
    this.opened = true
2✔
29
  }
30

31
  async close() {
32
    this.opened = false
2✔
33
    await this.pool?.end()
2✔
34
  }
35

36
  beginTransaction(options?: PgBeginTransactionOptions) {
37
    if (!this.opened || !this.pool) {
21!
NEW
38
      throw ERRORS.InternalError(undefined, `QueueDB not opened ${this.opened}`)
×
39
    }
40

41
    return new PgPoolExecutor(this.pool).beginTransaction(options)
21✔
42
  }
43

44
  protected async useTransaction<T>(fn: (client: pg.PoolClient) => Promise<T>): Promise<T> {
45
    if (!this.opened || !this.pool) {
12!
46
      throw ERRORS.InternalError(undefined, `QueueDB not opened ${this.opened}`)
×
47
    }
48

49
    const client = await this.pool.connect()
12✔
50

51
    // Create a promise that rejects if the client emits an error
52
    // (e.g. connection lost, statement_timeout at the backend level)
53
    let clientError: Error | undefined
54
    const onError = (e: Error) => {
12✔
55
      clientError = e
×
56
    }
57
    client.on('error', onError)
12✔
58

59
    try {
12✔
60
      await client.query('BEGIN')
12✔
61

62
      if (this.config.statement_timeout && this.config.statement_timeout > 0) {
12!
63
        await client.query(`SET LOCAL statement_timeout = ${this.config.statement_timeout}`)
12✔
64
      }
65

66
      const result = await fn(client)
12✔
67

68
      if (clientError) {
12!
69
        throw clientError
×
70
      }
71

72
      await client.query('COMMIT')
12✔
73
      return result
12✔
74
    } catch (err) {
75
      const rollbackErr = await client.query('ROLLBACK').catch((e) => e as Error)
×
76

77
      const errors = [err as Error, clientError, rollbackErr].filter(
×
78
        (e): e is Error => e instanceof Error
×
79
      )
80

81
      if (errors.length === 1) throw errors[0]
×
82
      throw new AggregateError(errors, 'Queue transaction failed')
×
83
    } finally {
84
      client.off('error', onError)
12✔
85
      client.release(clientError)
12✔
86
    }
87
  }
88

89
  async executeSql(text: string, values: unknown[]): Promise<{ rows: unknown[] }> {
90
    if (this.opened && this.pool) {
12!
91
      return this.useTransaction((client) => client.query(text, values))
12✔
92
    }
93

94
    throw ERRORS.InternalError(undefined, `QueueDB not opened ${this.opened} ${text}`)
×
95
  }
96
}
97

98
export class PgQueueDB extends EventEmitter implements Db {
99
  events = {
2✔
100
    error: 'error',
101
  }
102

103
  constructor(protected readonly db: DatabaseExecutor) {
2✔
104
    super()
2✔
105
  }
106

107
  async executeSql(text: string, values: unknown[]): Promise<{ rows: unknown[] }> {
108
    const result = await this.db.query({
1✔
109
      text,
110
      values: values.map((value) => (value === undefined ? null : value)),
3✔
111
    })
112

113
    return { rows: result.rows }
1✔
114
  }
115
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc