Skip to content

Commit 463f2fc

Browse files
authored
511 - add create_queue db migration (#513)
* add missing migration for create_queue function * versioning
1 parent 625779c commit 463f2fc

File tree

5 files changed

+73
-6
lines changed

5 files changed

+73
-6
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pg-boss",
3-
"version": "10.1.4",
3+
"version": "10.1.5",
44
"description": "Queueing jobs in Postgres from Node.js like a boss",
55
"main": "./src/index.js",
66
"engines": {
@@ -23,7 +23,8 @@
2323
"cover": "nyc npm test",
2424
"tsc": "tsc --noEmit types.d.ts",
2525
"readme": "node ./test/readme.js",
26-
"migrate": "node -e 'console.log(require(\"./src\").getMigrationPlans())'"
26+
"db:migrate": "node -e 'console.log(require(\"./src\").getMigrationPlans())'",
27+
"db:construct": "node -e 'console.log(require(\"./src\").getConstructionPlans())'"
2728
},
2829
"mocha": {
2930
"timeout": 10000,

src/migrationStore.js

+66
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,72 @@ function migrate (value, version, migrations) {
6464

6565
function getAll (schema) {
6666
return [
67+
{
68+
release: '10.1.5',
69+
version: 24,
70+
previous: 23,
71+
install: [
72+
`
73+
CREATE OR REPLACE FUNCTION ${schema}.create_queue(queue_name text, options json)
74+
RETURNS VOID AS
75+
$$
76+
DECLARE
77+
table_name varchar := 'j' || encode(sha224(queue_name::bytea), 'hex');
78+
queue_created_on timestamptz;
79+
BEGIN
80+
81+
WITH q as (
82+
INSERT INTO ${schema}.queue (
83+
name,
84+
policy,
85+
retry_limit,
86+
retry_delay,
87+
retry_backoff,
88+
expire_seconds,
89+
retention_minutes,
90+
dead_letter,
91+
partition_name
92+
)
93+
VALUES (
94+
queue_name,
95+
options->>'policy',
96+
(options->>'retryLimit')::int,
97+
(options->>'retryDelay')::int,
98+
(options->>'retryBackoff')::bool,
99+
(options->>'expireInSeconds')::int,
100+
(options->>'retentionMinutes')::int,
101+
options->>'deadLetter',
102+
table_name
103+
)
104+
ON CONFLICT DO NOTHING
105+
RETURNING created_on
106+
)
107+
SELECT created_on into queue_created_on from q;
108+
109+
IF queue_created_on IS NULL THEN
110+
RETURN;
111+
END IF;
112+
113+
EXECUTE format('CREATE TABLE ${schema}.%I (LIKE ${schema}.job INCLUDING DEFAULTS)', table_name);
114+
115+
EXECUTE format('ALTER TABLE ${schema}.%1$I ADD PRIMARY KEY (name, id)', table_name);
116+
EXECUTE format('ALTER TABLE ${schema}.%1$I ADD CONSTRAINT q_fkey FOREIGN KEY (name) REFERENCES ${schema}.queue (name) ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED', table_name);
117+
EXECUTE format('ALTER TABLE ${schema}.%1$I ADD CONSTRAINT dlq_fkey FOREIGN KEY (dead_letter) REFERENCES ${schema}.queue (name) ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED', table_name);
118+
EXECUTE format('CREATE UNIQUE INDEX %1$s_i1 ON ${schema}.%1$I (name, COALESCE(singleton_key, '''')) WHERE state = ''created'' AND policy = ''short''', table_name);
119+
EXECUTE format('CREATE UNIQUE INDEX %1$s_i2 ON ${schema}.%1$I (name, COALESCE(singleton_key, '''')) WHERE state = ''active'' AND policy = ''singleton''', table_name);
120+
EXECUTE format('CREATE UNIQUE INDEX %1$s_i3 ON ${schema}.%1$I (name, state, COALESCE(singleton_key, '''')) WHERE state <= ''active'' AND policy = ''stately''', table_name);
121+
EXECUTE format('CREATE UNIQUE INDEX %1$s_i4 ON ${schema}.%1$I (name, singleton_on, COALESCE(singleton_key, '''')) WHERE state <> ''cancelled'' AND singleton_on IS NOT NULL', table_name);
122+
EXECUTE format('CREATE INDEX %1$s_i5 ON ${schema}.%1$I (name, start_after) INCLUDE (priority, created_on, id) WHERE state < ''active''', table_name);
123+
124+
EXECUTE format('ALTER TABLE ${schema}.%I ADD CONSTRAINT cjc CHECK (name=%L)', table_name, queue_name);
125+
EXECUTE format('ALTER TABLE ${schema}.job ATTACH PARTITION ${schema}.%I FOR VALUES IN (%L)', table_name, queue_name);
126+
END;
127+
$$
128+
LANGUAGE plpgsql
129+
`
130+
],
131+
uninstall: []
132+
},
67133
{
68134
release: '10.1.1',
69135
version: 23,

src/plans.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ function createPrimaryKeyArchive (schema) {
332332
}
333333

334334
function createIndexJobPolicyShort (schema) {
335-
return `CREATE UNIQUE INDEX job_i1 ON ${schema}.job (name, COALESCE(singleton_key, '')) WHERE state = '${JOB_STATES.created}' AND policy = '${QUEUE_POLICIES.short}';`
335+
return `CREATE UNIQUE INDEX job_i1 ON ${schema}.job (name, COALESCE(singleton_key, '')) WHERE state = '${JOB_STATES.created}' AND policy = '${QUEUE_POLICIES.short}'`
336336
}
337337

338338
function createIndexJobPolicySingleton (schema) {

version.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"schema": 23
2+
"schema": 24
33
}

0 commit comments

Comments
 (0)