Skip to content

Commit 5722b29

Browse files
committed
feat: optionally disabling schema creation with createSchema option #605
1 parent 5a6313a commit 5722b29

6 files changed

Lines changed: 25 additions & 9 deletions

File tree

docs/api/constructor.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,13 @@ The following options can be set as properties in an object for additional confi
6969

7070
* **migrate**, bool, default true
7171

72-
If this is set to false, this instance will skip attempts to run schema migratations during `start()`. If schema migrations exist, `start()` will throw and error and block usage. This is an advanced use case when the configured user account does not have schema mutation privileges.
72+
If this is set to false, this instance will skip attempts to run schema migrations during `start()`. If schema migrations exist, `start()` will throw and error and block usage. This is an advanced use case when the configured user account does not have schema mutation privileges.
7373

7474
The following configuration options should not normally need to be changed, but are still available for special use cases.
7575

76+
* **createSchema**, bool, default true
77+
If set to false, the migration will not issue a CREATE SCHEMA statement. In some databases (for example, PostgreSQL), executing CREATE SCHEMA can fail even if the schema already exists, when the user lacks the required privileges to create schemas. Disabling this option prevents such errors.
78+
7679
* **superviseIntervalSeconds**, int, default 60 seconds
7780

7881
Entry point for how often queues are monitored and maintained.

src/contractor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const migrationStore = require('./migrationStore')
55
const schemaVersion = require('../version.json').schema
66

77
class Contractor {
8-
static constructionPlans (schema = DEFAULT_SCHEMA) {
9-
return plans.create(schema, schemaVersion)
8+
static constructionPlans (schema = DEFAULT_SCHEMA, options = { createSchema: true }) {
9+
return plans.create(schema, schemaVersion, options)
1010
}
1111

1212
static migrationPlans (schema = DEFAULT_SCHEMA, version = schemaVersion - 1) {
@@ -69,7 +69,7 @@ class Contractor {
6969

7070
async create () {
7171
try {
72-
const commands = plans.create(this.config.schema, schemaVersion)
72+
const commands = plans.create(this.config.schema, schemaVersion, this.config)
7373
await this.db.executeSql(commands)
7474
} catch (err) {
7575
assert(err.message.includes(plans.CREATE_RACE_MESSAGE), err)

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class PgBoss extends EventEmitter {
2424
#manager
2525
#timekeeper
2626

27-
static getConstructionPlans (schema) {
28-
return Contractor.constructionPlans(schema)
27+
static getConstructionPlans (schema, options = { createSchema: true }) {
28+
return Contractor.constructionPlans(schema, options)
2929
}
3030

3131
static getMigrationPlans (schema, version) {

src/plans.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ module.exports = {
8282
DEFAULT_SCHEMA,
8383
}
8484

85-
function create (schema, version) {
85+
function create (schema, version, options = { createSchema: true }) {
8686
const commands = [
87-
createSchema(schema),
87+
...(options.createSchema ? [createSchema(schema)] : []),
8888
createEnumJobState(schema),
8989

9090
createTableVersion(schema),

test/migrationTest.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ describe('migration', function () {
1212
this.currentTest.contractor = new Contractor(db, this.currentTest.bossConfig)
1313
})
1414

15+
it('should include create schema by default ', function () {
16+
const schema = 'custom'
17+
const plans = PgBoss.getConstructionPlans(schema)
18+
assert(plans.includes('CREATE SCHEMA'))
19+
})
20+
21+
it('should not include create schema if createSchema=false', function () {
22+
const schema = 'custom'
23+
const plans = PgBoss.getConstructionPlans(schema, { createSchema: false })
24+
assert(!plans.includes('CREATE SCHEMA'))
25+
})
26+
1527
it('should export commands to manually build schema', function () {
1628
const schema = 'custom'
1729
const plans = PgBoss.getConstructionPlans(schema)

types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ declare namespace PgBoss {
4646
interface MaintenanceOptions {
4747
supervise?: boolean;
4848
migrate?: boolean;
49+
createSchema?: boolean;
4950
warningSlowQuerySeconds?: number;
5051
warningQueueSize?: number;
5152
superviseIntervalSeconds?: number;
@@ -220,7 +221,7 @@ declare class PgBoss extends EventEmitter {
220221
constructor (connectionString: string)
221222
constructor (options: PgBoss.ConstructorOptions)
222223

223-
static getConstructionPlans (schema?: string): string
224+
static getConstructionPlans (schema?: string, options?: { createSchema: boolean }): string
224225
static getMigrationPlans (schema?: string, version?: string): string
225226
static getRollbackPlans (schema?: string, version?: string): string
226227

0 commit comments

Comments
 (0)