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

timgit / pg-boss / 16127615119

07 Jul 2025 08:51PM UTC coverage: 94.475% (-5.5%) from 100.0%
16127615119

Pull #495

github

web-flow
Merge ec219eae0 into 6d4c88633
Pull Request #495: v11

368 of 430 branches covered (85.58%)

304 of 305 new or added lines in 8 files covered. (99.67%)

49 existing lines in 4 files now uncovered.

855 of 905 relevant lines covered (94.48%)

103.5 hits per line

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

56.1
/src/contractor.js
1
const assert = require('node:assert')
4✔
2
const plans = require('./plans')
4✔
3
const { DEFAULT_SCHEMA } = plans
4✔
4
const migrationStore = require('./migrationStore')
4✔
5
const schemaVersion = require('../version.json').schema
4✔
6

7
class Contractor {
8
  static constructionPlans (schema = DEFAULT_SCHEMA) {
×
UNCOV
9
    return plans.create(schema, schemaVersion)
×
10
  }
11

12
  static migrationPlans (schema = DEFAULT_SCHEMA, version = schemaVersion - 1) {
×
UNCOV
13
    return migrationStore.migrate(schema, version)
×
14
  }
15

16
  static rollbackPlans (schema = DEFAULT_SCHEMA, version = schemaVersion) {
×
UNCOV
17
    return migrationStore.rollback(schema, version)
×
18
  }
19

20
  constructor (db, config) {
21
    this.config = config
187✔
22
    this.db = db
187✔
23
    this.migrations = this.config.migrations || migrationStore.getAll(this.config.schema)
187✔
24

25
    // exported api to index
26
    this.functions = [
187✔
27
      this.schemaVersion,
28
      this.isInstalled
29
    ]
30
  }
31

32
  async schemaVersion () {
33
    const result = await this.db.executeSql(plans.getVersion(this.config.schema))
2✔
34
    return result.rows.length ? parseInt(result.rows[0].version) : null
2!
35
  }
36

37
  async isInstalled () {
38
    const result = await this.db.executeSql(plans.versionTableExists(this.config.schema))
186✔
39
    return !!result.rows[0].name
185✔
40
  }
41

42
  async start () {
43
    const installed = await this.isInstalled()
186✔
44

45
    if (installed) {
185✔
46
      const version = await this.schemaVersion()
2✔
47

48
      if (schemaVersion > version) {
2!
UNCOV
49
        await this.migrate(version)
×
50
      }
51
    } else {
52
      await this.create()
183✔
53
    }
54
  }
55

56
  async check () {
UNCOV
57
    const installed = await this.isInstalled()
×
58

UNCOV
59
    if (!installed) {
×
UNCOV
60
      throw new Error('pg-boss is not installed')
×
61
    }
62

UNCOV
63
    const version = await this.schemaVersion()
×
64

UNCOV
65
    if (schemaVersion !== version) {
×
UNCOV
66
      throw new Error('pg-boss database requires migrations')
×
67
    }
68
  }
69

70
  async create () {
71
    try {
183✔
72
      const commands = plans.create(this.config.schema, schemaVersion)
183✔
73
      await this.db.executeSql(commands)
183✔
74
    } catch (err) {
75
      assert(err.message.includes(plans.CREATE_RACE_MESSAGE), err)
19✔
76
    }
77
  }
78

79
  async migrate (version) {
UNCOV
80
    try {
×
UNCOV
81
      const commands = migrationStore.migrate(this.config, version, this.migrations)
×
UNCOV
82
      await this.db.executeSql(commands)
×
83
    } catch (err) {
UNCOV
84
      assert(err.message.includes(plans.MIGRATE_RACE_MESSAGE), err)
×
85
    }
86
  }
87

88
  async next (version) {
UNCOV
89
    const commands = migrationStore.next(this.config.schema, version, this.migrations)
×
UNCOV
90
    await this.db.executeSql(commands)
×
91
  }
92

93
  async rollback (version) {
UNCOV
94
    const commands = migrationStore.rollback(this.config.schema, version, this.migrations)
×
UNCOV
95
    await this.db.executeSql(commands)
×
96
  }
97
}
98

99
module.exports = Contractor
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc