Skip to content

Commit

Permalink
Alter name & default for versions mod_log field (#2612)
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

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

In [Add mod_log fields 2 return, licence & charge ver.](#2598), we added a data migration to add a new `mod_log` field to the charge, licence, and return version tables in the `water` schema. This field was intended to hold details from the linked NALD mod log record, such as who, when, and why the version was created.

However, we've since learned that a version in NALD can have multiple mod log records. Therefore, we want to change the name to make that clear. We also change the default to avoid complexity in the code. Now, it will default to an empty JSONB array rather than an empty object.

This change is the data migration for that.
  • Loading branch information
Cruikshanks authored Aug 16, 2024
1 parent 7e68be1 commit 9f1e3fa
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
45 changes: 45 additions & 0 deletions migrations/20240816144249-alter-versions-mod-log-column.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict'

const fs = require('fs')
const path = require('path')
let Promise

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function (options, _seedLink) {
Promise = options.Promise
}

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

resolve(data)
})
})
.then(function (data) {
return db.runSql(data)
})
}

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

resolve(data)
})
})
.then(function (data) {
return db.runSql(data)
})
}

exports._meta = {
version: 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Revert the changes */

BEGIN;

ALTER TABLE water.charge_versions ALTER COLUMN mod_logs SET DEFAULT '{}'::jsonb;
ALTER TABLE water.charge_versions RENAME COLUMN mod_logs TO mod_log;
UPDATE water.charge_versions SET mod_log = '{}'::jsonb WHERE mod_log = '[]'::jsonb;

ALTER TABLE water.licence_versions ALTER COLUMN mod_logs SET DEFAULT '{}'::jsonb;
ALTER TABLE water.licence_versions RENAME COLUMN mod_logs TO mod_log;
UPDATE water.licence_versions SET mod_log = '{}'::jsonb WHERE mod_log = '[]'::jsonb;

ALTER TABLE water.return_versions ALTER COLUMN mod_logs SET DEFAULT '{}'::jsonb;
ALTER TABLE water.return_versions RENAME COLUMN mod_logs TO mod_log;
UPDATE water.return_versions SET mod_log = '{}'::jsonb WHERE mod_log = '[]'::jsonb;

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Renames the mod_log column and changes its default in each version table
In [Add mod_log fields 2 return, licence & charge ver.](https://github.com/DEFRA/water-abstraction-service/pull/2598),
we added a data migration to add a new `mod_log` field to the charge, licence, and return version tables in the
`water` schema. This field was intended to hold details from the linked NALD mod log record, such as who, when, and
why the version was created.
However, we've since learned that a version in NALD can have multiple mod log records. Therefore, we want to change
the name to make that clear. We also change the default to avoid complexity in the code. Now, it will default to an
empty JSONB array rather than an empty object.
*/

BEGIN;

ALTER TABLE water.charge_versions RENAME COLUMN mod_log TO mod_logs;
ALTER TABLE water.charge_versions ALTER COLUMN mod_logs SET DEFAULT '[]'::jsonb;
UPDATE water.charge_versions SET mod_logs = '[]'::jsonb WHERE mod_logs = '{}'::jsonb;

ALTER TABLE water.licence_versions RENAME COLUMN mod_log TO mod_logs;
ALTER TABLE water.licence_versions ALTER COLUMN mod_logs SET DEFAULT '[]'::jsonb;
UPDATE water.licence_versions SET mod_logs = '[]'::jsonb WHERE mod_logs = '{}'::jsonb;

ALTER TABLE water.return_versions RENAME COLUMN mod_log TO mod_logs;
ALTER TABLE water.return_versions ALTER COLUMN mod_logs SET DEFAULT '[]'::jsonb;
UPDATE water.return_versions SET mod_logs = '[]'::jsonb WHERE mod_logs = '{}'::jsonb;

COMMIT;

0 comments on commit 9f1e3fa

Please sign in to comment.