forked from timgit/pg-boss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrationTest.js
159 lines (107 loc) · 4.43 KB
/
migrationTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const assert = require('assert')
const PgBoss = require('../')
const helper = require('./testHelper')
const Contractor = require('../src/contractor')
const migrationStore = require('../src/migrationStore')
const currentSchemaVersion = require('../version.json').schema
describe('migration', function () {
let contractor
beforeEach(async function () {
const db = await helper.getDb()
contractor = new Contractor(db, this.currentTest.bossConfig)
})
it('should migrate to previous version and back again', async function () {
await contractor.create()
await contractor.rollback(currentSchemaVersion)
const oldVersion = await contractor.version()
assert.notStrictEqual(oldVersion, currentSchemaVersion)
await contractor.migrate(oldVersion)
const newVersion = await contractor.version()
assert.strictEqual(newVersion, currentSchemaVersion)
})
it('should migrate to latest during start if on previous schema version', async function () {
await contractor.create()
await contractor.rollback(currentSchemaVersion)
const config = { ...this.test.bossConfig, noSupervisor: true }
const boss = this.test.boss = new PgBoss(config)
await boss.start()
const version = await contractor.version()
assert.strictEqual(version, currentSchemaVersion)
})
it('should migrate through 2 versions back and forth', async function () {
const queue = 'migrate-back-2-and-forward'
const config = { ...this.test.bossConfig, noSupervisor: true }
const boss = this.test.boss = new PgBoss(config)
await boss.start()
// creating jobs in 3 states to have data to migrate back and forth
// completed job
await boss.send(queue)
const job = await boss.fetch(queue)
await boss.complete(job.id)
// active job
await boss.send(queue)
await boss.fetch(queue)
// created job
await boss.send(queue)
await contractor.rollback(currentSchemaVersion)
const oneVersionAgo = await contractor.version()
assert.notStrictEqual(oneVersionAgo, currentSchemaVersion)
await contractor.rollback(oneVersionAgo)
const twoVersionsAgo = await contractor.version()
assert.notStrictEqual(twoVersionsAgo, oneVersionAgo)
await contractor.next(twoVersionsAgo)
const oneVersionAgoPart2 = await contractor.version()
assert.strictEqual(oneVersionAgo, oneVersionAgoPart2)
await contractor.next(oneVersionAgo)
const version = await contractor.version()
assert.strictEqual(version, currentSchemaVersion)
})
it('should migrate to latest during start if on previous 2 schema versions', async function () {
await contractor.create()
await contractor.rollback(currentSchemaVersion)
const oneVersionAgo = await contractor.version()
assert.strictEqual(oneVersionAgo, currentSchemaVersion - 1)
await contractor.rollback(oneVersionAgo)
const twoVersionsAgo = await contractor.version()
assert.strictEqual(twoVersionsAgo, currentSchemaVersion - 2)
const config = { ...this.test.bossConfig, noSupervisor: true }
const boss = this.test.boss = new PgBoss(config)
await boss.start()
const version = await contractor.version()
assert.strictEqual(version, currentSchemaVersion)
})
it('migrating to non-existent version fails gracefully', async function () {
await contractor.create()
try {
await contractor.migrate('¯\\_(ツ)_//¯')
} catch (error) {
assert(error.message.includes('not found'))
}
})
it('should roll back an error during a migration', async function () {
const config = { ...this.test.bossConfig, noSupervisor: true }
config.migrations = migrationStore.getAll(config.schema)
// add invalid sql statement
config.migrations[0].install.push('wat')
await contractor.create()
await contractor.rollback(currentSchemaVersion)
const oneVersionAgo = await contractor.version()
const boss1 = new PgBoss(config)
try {
await boss1.start()
} catch (error) {
assert(error.message.includes('wat'))
} finally {
await boss1.stop({ graceful: false })
}
const version1 = await contractor.version()
assert.strictEqual(version1, oneVersionAgo)
// remove bad sql statement
config.migrations[0].install.pop()
const boss2 = new PgBoss(config)
await boss2.start()
const version2 = await contractor.version()
assert.strictEqual(version2, currentSchemaVersion)
await boss2.stop({ graceful: false })
})
})