Skip to content

Commit

Permalink
Merge branch 'master' of github.com:timgit/pg-boss
Browse files Browse the repository at this point in the history
  • Loading branch information
timgit committed Aug 27, 2024
2 parents 4de92c6 + 877387e commit 25f9c69
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 33 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-boss",
"version": "10.0.5",
"version": "10.0.6",
"description": "Queueing jobs in Postgres from Node.js like a boss",
"main": "./src/index.js",
"engines": {
Expand Down
19 changes: 9 additions & 10 deletions src/contractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ class Contractor {
const version = await this.schemaVersion()

if (schemaVersion > version) {
throw new Error('Migrations are not supported to v10')
// await this.migrate(version)
await this.migrate(version)
}
} else {
await this.create()
Expand Down Expand Up @@ -86,15 +85,15 @@ class Contractor {
}
}

// async next (version) {
// const commands = migrationStore.next(this.config.schema, version, this.migrations)
// await this.db.executeSql(commands)
// }
async next (version) {
const commands = migrationStore.next(this.config.schema, version, this.migrations)
await this.db.executeSql(commands)
}

// async rollback (version) {
// const commands = migrationStore.rollback(this.config.schema, version, this.migrations)
// await this.db.executeSql(commands)
// }
async rollback (version) {
const commands = migrationStore.rollback(this.config.schema, version, this.migrations)
await this.db.executeSql(commands)
}
}

module.exports = Contractor
11 changes: 11 additions & 0 deletions src/migrationStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,16 @@ function migrate (value, version, migrations) {

function getAll (schema) {
return [
{
release: '10.0.6',
version: 22,
previous: 21,
install: [
`ALTER TABLE ${schema}.job ALTER COLUMN retry_limit SET DEFAULT 2`
],
uninstall: [
`ALTER TABLE ${schema}.job ALTER COLUMN retry_limit SET DEFAULT 0`
]
}
]
}
2 changes: 1 addition & 1 deletion src/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function createTableJob (schema) {
priority integer not null default(0),
data jsonb,
state ${schema}.job_state not null default('${JOB_STATES.created}'),
retry_limit integer not null default(0),
retry_limit integer not null default(2),
retry_count integer not null default(0),
retry_delay integer not null default(0),
retry_backoff boolean not null default false,
Expand Down
50 changes: 33 additions & 17 deletions test/migrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,39 @@ describe('migration', function () {
}
})

it.skip('should migrate to previous version and back again', async function () {
it('should migrate to previous version and back again', async function () {
const { contractor } = this.test

await contractor.create()

await contractor.rollback(currentSchemaVersion)
const oldVersion = await contractor.version()
const oldVersion = await contractor.schemaVersion()

assert.notStrictEqual(oldVersion, currentSchemaVersion)

await contractor.migrate(oldVersion)
const newVersion = await contractor.version()
const newVersion = await contractor.schemaVersion()

assert.strictEqual(newVersion, currentSchemaVersion)
})

it.skip('should migrate to latest during start if on previous schema version', async function () {
it('should install next version via contractor', async function () {
const { contractor } = this.test

await contractor.create()

await contractor.rollback(currentSchemaVersion)

const oneVersionAgo = await contractor.schemaVersion()

await contractor.next(oneVersionAgo)

const version = await contractor.schemaVersion()

assert.strictEqual(version, currentSchemaVersion)
})

it('should migrate to latest during start if on previous schema version', async function () {
const { contractor } = this.test

await contractor.create()
Expand All @@ -61,7 +77,7 @@ describe('migration', function () {

await boss.start()

const version = await contractor.version()
const version = await contractor.schemaVersion()

assert.strictEqual(version, currentSchemaVersion)
})
Expand All @@ -88,22 +104,22 @@ describe('migration', function () {
await boss.send(queue)

await contractor.rollback(currentSchemaVersion)
const oneVersionAgo = await contractor.version()
const oneVersionAgo = await contractor.schemaVersion()

assert.notStrictEqual(oneVersionAgo, currentSchemaVersion)

await contractor.rollback(oneVersionAgo)
const twoVersionsAgo = await contractor.version()
const twoVersionsAgo = await contractor.schemaVersion()

assert.notStrictEqual(twoVersionsAgo, oneVersionAgo)

await contractor.next(twoVersionsAgo)
const oneVersionAgoPart2 = await contractor.version()
const oneVersionAgoPart2 = await contractor.schemaVersion()

assert.strictEqual(oneVersionAgo, oneVersionAgoPart2)

await contractor.next(oneVersionAgo)
const version = await contractor.version()
const version = await contractor.schemaVersion()

assert.strictEqual(version, currentSchemaVersion)

Expand All @@ -118,18 +134,18 @@ describe('migration', function () {
await contractor.create()

await contractor.rollback(currentSchemaVersion)
const oneVersionAgo = await contractor.version()
const oneVersionAgo = await contractor.schemaVersion()
assert.strictEqual(oneVersionAgo, currentSchemaVersion - 1)

await contractor.rollback(oneVersionAgo)
const twoVersionsAgo = await contractor.version()
const twoVersionsAgo = await contractor.schemaVersion()
assert.strictEqual(twoVersionsAgo, currentSchemaVersion - 2)

const config = { ...this.test.bossConfig }
const boss = this.test.boss = new PgBoss(config)
await boss.start()

const version = await contractor.version()
const version = await contractor.schemaVersion()

assert.strictEqual(version, currentSchemaVersion)
})
Expand All @@ -146,7 +162,7 @@ describe('migration', function () {
}
})

it.skip('should roll back an error during a migration', async function () {
it('should roll back an error during a migration', async function () {
const { contractor } = this.test

const config = { ...this.test.bossConfig }
Expand All @@ -158,7 +174,7 @@ describe('migration', function () {

await contractor.create()
await contractor.rollback(currentSchemaVersion)
const oneVersionAgo = await contractor.version()
const oneVersionAgo = await contractor.schemaVersion()

const boss1 = new PgBoss(config)

Expand All @@ -170,7 +186,7 @@ describe('migration', function () {
await boss1.stop({ graceful: false, wait: false })
}

const version1 = await contractor.version()
const version1 = await contractor.schemaVersion()

assert.strictEqual(version1, oneVersionAgo)

Expand All @@ -181,7 +197,7 @@ describe('migration', function () {

await boss2.start()

const version2 = await contractor.version()
const version2 = await contractor.schemaVersion()

assert.strictEqual(version2, currentSchemaVersion)

Expand All @@ -199,7 +215,7 @@ describe('migration', function () {
}
})

it.skip('should not migrate if migrate option is false', async function () {
it('should not migrate if migrate option is false', async function () {
const { contractor } = this.test

await contractor.create()
Expand Down
2 changes: 1 addition & 1 deletion test/multiMasterTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('multi-master', function () {

await contractor.rollback(currentSchemaVersion)

const oldVersion = await contractor.version()
const oldVersion = await contractor.schemaVersion()

assert.notStrictEqual(oldVersion, currentSchemaVersion)

Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"schema": 21
"schema": 22
}

0 comments on commit 25f9c69

Please sign in to comment.