|
| 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