Skip to content

Commit

Permalink
Replace mod_logs columns with a table - for real! (#2615)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4563
https://eaflood.atlassian.net/browse/WATER-4564
https://eaflood.atlassian.net/browse/WATER-4565

> We foolishly merged [Replace mod_logs columns with a table](#2613) as an empty commit hence doing it for real this time!

> Part of the work to display a licence's history to users (mod log)

**It's a trap!**

We have made the same mistake as the previous team. When we believed that a version (charge, licence, or return) had a one-to-one relationship with NALD mod log records, a JSONB field in each was a suitable solution to capturing just the information we needed. After all, it's only the historical records we have to worry about.

But when we learned that a version record could be linked to multiple mod logs, we should have taken a different approach. Instead, we ploughed on with our JSON column [altering its name and default](#2612) to match. 🤦😬

Once you've started down the JSONB road, it is easy to get trapped!

Thankfully, we've come to our senses before this saw the light of day in production. The mod log data we are importing needs to go into its own table, and we will link our version records accordingly. A 'standard' relationship database solution.

We could just add a migration that drops the columns and adds the new table. But because of the interaction between the views [water-abstraction-system](https://github.com/DEFRA/water-abstraction-system) creates, it's no longer possible.

Because the mod log column migrations have not been run in production yet, we're just going to delete them. This might be a bit messy for our local environments, but it avoids leaving a 'dead' field in the database.

So, in this change, we delete the old migrations and then add our new table migration.
  • Loading branch information
Cruikshanks authored Aug 19, 2024
1 parent 4076c3c commit 62567b3
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 116 deletions.
45 changes: 0 additions & 45 deletions migrations/20240809155344-water-versions-add-mod-log-column.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports.setup = function (options, _seedLink) {
}

exports.up = function (db) {
const filePath = path.join(__dirname, 'sqls', '20240816144249-alter-versions-mod-log-column-up.sql')
const filePath = path.join(__dirname, 'sqls', '20240819083436-water-add-mod-logs-up.sql')
return new Promise(function (resolve, reject) {
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) {
if (err) return reject(err)
Expand All @@ -27,7 +27,7 @@ exports.up = function (db) {
}

exports.down = function (db) {
const filePath = path.join(__dirname, 'sqls', '20240816144249-alter-versions-mod-log-column-down.sql')
const filePath = path.join(__dirname, 'sqls', '20240819083436-water-add-mod-logs-down.sql')
return new Promise(function (resolve, reject) {
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) {
if (err) return reject(err)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

7 changes: 7 additions & 0 deletions migrations/sqls/20240819083436-water-add-mod-logs-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* Revert previous change */

BEGIN;

DROP TABLE IF EXISTS water.mod_logs;

COMMIT;
49 changes: 49 additions & 0 deletions migrations/sqls/20240819083436-water-add-mod-logs-up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Add mod -logs table
> Part of the work to display a licence's history to users (mod log)
Adds a table to hold imported mod logs from NALD.
NALD has a concept called 'mod log'. Essentially, when someone creates a new licence, charge or return version, they
are required to provide a reason and can also add a note.
Rather than this being stored against each record, the 'mod log' is stored in a single place. Users can then view a
licence's 'mod logs' to get a sense of the history of the licence.
When charging switched from NALD to WRLS, we (the previous team) built the ability to record a reason and note against
a new charge version but didn't import any historic data. With return requirements also soon to switch from NALD to
WRLS there is a concern that this view of changes made to a licence will be lost.
Having reviewed how the information is held in NALD we've confirmed that we can extract this information and import it
as part of the NALD import.
*/

BEGIN;

CREATE TABLE water.mod_logs (
id uuid PRIMARY KEY DEFAULT public.gen_random_uuid(),
external_id varchar NOT NULL,
event_code varchar NOT NULL,
event_description varchar NOT NULL,
reason_type varchar NULL,
reason_code varchar NULL,
reason_description varchar NULL,
nald_date date NOT NULL,
user_id varchar NOT NULL,
note text NULL,
licence_ref varchar NOT NULL,
licence_external_id varchar NOT NULL,
licence_id uuid NULL,
licence_version_external_id varchar NULL,
licence_version_id uuid NULL,
charge_version_external_id varchar NULL,
charge_version_id uuid NULL,
return_version_external_id varchar NULL,
return_version_id uuid NULL,
created_at timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL,
CONSTRAINT uniq_mod_log_external_id UNIQUE (external_id)
);

COMMIT;

0 comments on commit 62567b3

Please sign in to comment.