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

timgit / pg-boss / 9949043480

16 Jul 2024 01:09AM UTC coverage: 94.41% (-5.6%) from 100.0%
9949043480

Pull #425

github

web-flow
Merge 6b98fbe61 into f1c1636ca
Pull Request #425: v10

466 of 546 branches covered (85.35%)

351 of 365 new or added lines in 10 files covered. (96.16%)

40 existing lines in 5 files now uncovered.

912 of 966 relevant lines covered (94.41%)

803.11 hits per line

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

58.54
/src/contractor.js
1
const assert = require('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) {
×
9
    return plans.create(schema, schemaVersion)
1✔
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
182✔
22
    this.db = db
182✔
23
    this.migrations = this.config.migrations || migrationStore.getAll(this.config.schema)
182✔
24

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

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

37
  async isInstalled () {
38
    const result = await this.db.executeSql(plans.versionTableExists(this.config.schema))
182✔
39
    return result.rows.length ? result.rows[0].name : null
181!
40
  }
41

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

45
    if (installed) {
181✔
46
      const version = await this.version()
4✔
47

48
      if (schemaVersion > version) {
4!
NEW
49
        throw new Error('Migrations are not supported to v10')
×
50
        // await this.migrate(version)
51
      }
52
    } else {
53
      await this.create()
177✔
54
    }
55
  }
56

57
  async check () {
NEW
58
    const installed = await this.isInstalled()
×
59

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

NEW
64
    const version = await this.version()
×
65

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

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

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

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

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

100
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