Skip to content

Commit 410ab2c

Browse files
authored
Identify workflow setup records with chg versions (#2497)
https://eaflood.atlassian.net/browse/WATER-4437 For context, we found out legacy background jobs scheduled in BullMQ only run intermittently. One we found hadn't run for the last 2 years so we agreed to bin it. Two we need to deal with at some point but intermittent is fine for now. The critical job, which is putting updated licences into workflow, we solved by migrating to [water-abstraction-system](DEFRA/water-abstraction-system#903). As part of solving it we took the opportunity to add data to the workflow record to allow us to identify that the record was added because of an updated licence. We also spotted that our time-limited job was broken 😱 so [fixed that](DEFRA/water-abstraction-system#908) and updated it to _also_ add some info to the workflow record. With these handy bits of info now in the workflow record, it seemed a shame we didn't make the reason why the record was added visible to the user. That led to [Make manage workflow 'to setup' links intelligent](DEFRA/water-abstraction-ui#2549) in the legacy UI code. The last piece of the puzzle is the historic `to_setup` records. The new links would be less confusing if the existing records were updated to also identify if they are for new licences that need charge information or existing ones that need their details checking. This adds a one-time migration to do just that!
1 parent f43439c commit 410ab2c

3 files changed

+88
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict'
2+
3+
const fs = require('fs')
4+
const path = require('path')
5+
let Promise
6+
7+
/**
8+
* We receive the dbmigrate dependency from dbmigrate initially.
9+
* This enables us to not have to rely on NODE_PATH.
10+
*/
11+
exports.setup = function (options, _seedLink) {
12+
Promise = options.Promise
13+
}
14+
15+
exports.up = function (db) {
16+
const filePath = path.join(__dirname, 'sqls', '20240415100933-tag-updated-to-setup-workflows-up.sql')
17+
return new Promise(function (resolve, reject) {
18+
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) {
19+
if (err) return reject(err)
20+
21+
resolve(data)
22+
})
23+
})
24+
.then(function (data) {
25+
return db.runSql(data)
26+
})
27+
}
28+
29+
exports.down = function (db) {
30+
const filePath = path.join(__dirname, 'sqls', '20240415100933-tag-updated-to-setup-workflows-down.sql')
31+
return new Promise(function (resolve, reject) {
32+
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) {
33+
if (err) return reject(err)
34+
35+
resolve(data)
36+
})
37+
})
38+
.then(function (data) {
39+
return db.runSql(data)
40+
})
41+
}
42+
43+
exports._meta = {
44+
version: 1
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* Replace with your SQL commands */
2+
/* No down script due it not being possible to identify the records that were updated */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Identify workflow setup records with chg versions
3+
4+
https://eaflood.atlassian.net/browse/WATER-4437
5+
6+
For context, we found out legacy background jobs scheduled in BullMQ only run intermittently. One we found hadn't run
7+
for the last 2 years so we agreed to bin it. Two we need to deal with at some point but intermittent is fine for now.
8+
The critical job, which is putting updated licences into workflow, we solved by migrating to
9+
[water-abstraction-system](https://github.com/DEFRA/water-abstraction-system/pull/903).
10+
11+
As part of solving it we took the opportunity to add data to the workflow record to allow us to identify that the
12+
record was added because of an updated licence. We also spotted that our time-limited job was broken 😱 so [fixed
13+
that](https://github.com/DEFRA/water-abstraction-system/pull/908) and updated it to _also_ add some info to the
14+
workflow record.
15+
16+
With these handy bits of info now in the workflow record, it seemed a shame we didn't make the reason why the record
17+
was added visible to the user.
18+
19+
That led to [Make manage workflow 'to setup' links
20+
intelligent](https://github.com/DEFRA/water-abstraction-ui/pull/2549) in the legacy UI code. The last piece of the
21+
puzzle is the historic `to_setup` records. The new links would be less confusing if the existing records were updated
22+
to also identify if they are for new licences that need charge information or existing ones that need their details
23+
checking.
24+
25+
This is the migration to do just that!
26+
*/
27+
UPDATE water.charge_version_workflows SET "data" = (
28+
SELECT
29+
(CASE WHEN EXISTS(SELECT 1 FROM water.charge_versions cv WHERE cv.licence_id = cvw_lookup.licence_id)
30+
THEN '{"chargeVersion": null, "chargeVersionExists": true}'::jsonb
31+
ELSE '{"chargeVersion": null, "chargeVersionExists": false}'::jsonb
32+
END) AS charge_version_exists
33+
FROM water.charge_version_workflows cvw_lookup
34+
WHERE cvw_lookup.charge_version_workflow_id = water.charge_version_workflows.charge_version_workflow_id
35+
)
36+
-- Only apply the change to workflow records set as `to_setup` which haven't been touched by the new licence-updates and
37+
-- time-limited jobs
38+
WHERE water.charge_version_workflows.status = 'to_setup'
39+
AND water.charge_version_workflows.date_deleted IS NULL
40+
AND water.charge_version_workflows."data"->>'timeLimitedChargeVersionId' IS NULL
41+
AND water.charge_version_workflows."data"->>'chargeVersionExists' IS NULL;

0 commit comments

Comments
 (0)