Skip to content

Fix - Delete licence with invalid leading space #2417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions migrations/20240207123613-delete-broken-licence.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', '20240207123613-delete-broken-licence-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', '20240207123613-delete-broken-licence-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
}
2 changes: 2 additions & 0 deletions migrations/sqls/20240207123613-delete-broken-licence-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* Replace with your SQL commands */
/* No down script due to migration being used to remove bad data from the db. We don't want that bad data put back in!!!! */
122 changes: 122 additions & 0 deletions migrations/sqls/20240207123613-delete-broken-licence-up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
Fix duplicate licence record caused by leading space in 2nd NALD

https://eaflood.atlassian.net/browse/WATER-4357

Searching for NW/075/0012/011 is crashing the service. It is because there are 2 of them in the DB. One of the records
has a leading space in front of the licence reference.

Clearly, water-abstraction-import is not stripping whitespace. So, the computer sees these as completely different
values, meaning it has imported both.

This migration removes the problem record.
*/
DO $$
BEGIN
IF EXISTS
( SELECT 1
FROM information_schema.tables
WHERE table_schema = 'crm_v2'
AND table_name = 'document_roles'
)
THEN
DELETE FROM "crm_v2"."document_roles" WHERE document_id IN (
SELECT document_id FROM "crm_v2"."documents" WHERE document_ref = ' NW/075/0012/011'
);
END IF ;
END
$$ ;

DO $$
BEGIN
IF EXISTS
( SELECT 1
FROM information_schema.tables
WHERE table_schema = 'crm_v2'
AND table_name = 'documents'
)
THEN
DELETE FROM "crm_v2"."documents" WHERE document_ref = ' NW/075/0012/011';
END IF ;
END
$$ ;

DO $$
BEGIN
IF EXISTS
( SELECT 1
FROM information_schema.tables
WHERE table_schema = 'crm'
AND table_name = 'document_header'
)
THEN
DELETE FROM crm.document_header WHERE system_external_id = ' NW/075/0012/011';
END IF ;
END
$$ ;

DO $$
BEGIN
IF EXISTS
( SELECT 1
FROM information_schema.tables
WHERE table_schema = 'permit'
AND table_name = 'licence'
)
THEN
DELETE FROM permit.licence WHERE licence_ref = ' NW/075/0012/011';
END IF ;
END
$$ ;

DO $$
BEGIN
IF EXISTS
( SELECT 1
FROM information_schema.tables
WHERE table_schema = 'returns'
AND table_name = 'returns'
)
THEN
DELETE FROM "returns"."returns" WHERE licence_ref = ' NW/075/0012/011';
END IF ;
END
$$ ;

DELETE FROM water.licence_version_purpose_conditions WHERE licence_version_purpose_id IN (
SELECT licence_version_purpose_id FROM water.licence_version_purposes WHERE licence_version_id IN (
SELECT licence_version_id FROM water.licence_versions WHERE licence_id IN (
SELECT licence_id FROM water.licences WHERE licence_ref = ' NW/075/0012/011'
)
)
);

DELETE FROM water.licence_version_purposes WHERE licence_version_id IN (
SELECT licence_version_id FROM water.licence_versions WHERE licence_id IN (
SELECT licence_id FROM water.licences WHERE licence_ref = ' NW/075/0012/011'
)
);

DELETE FROM water.licence_versions WHERE licence_id IN (
SELECT licence_id FROM water.licences WHERE licence_ref = ' NW/075/0012/011'
);

DELETE FROM water.return_requirement_purposes WHERE return_requirement_id IN (
SELECT return_requirement_id FROM water.return_requirements WHERE return_version_id IN (
SELECT return_version_id FROM water.return_versions WHERE licence_id IN (
SELECT licence_id FROM water.licences WHERE licence_ref = ' NW/075/0012/011'
)
)
);

DELETE FROM water.return_requirements WHERE return_version_id IN (
SELECT return_version_id FROM water.return_versions WHERE licence_id IN(
SELECT licence_id FROM water.licences WHERE licence_ref = ' NW/075/0012/011'
)
);

DELETE FROM water.return_versions WHERE licence_id IN (
SELECT licence_id FROM water.licences WHERE licence_ref = ' NW/075/0012/011'
);

DELETE FROM water.licences WHERE licence_ref = ' NW/075/0012/011';