Skip to content

Commit

Permalink
Alter CRM document unique constraints (#1053)
Browse files Browse the repository at this point in the history
* Alter CRM document unique constraints

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

As part of the ongoing work to move the import code into WRLS. We need to use constraints to enforce data consistency and uniqueness.

Previously the document type and regime where defaulted and used to form a unique key. We no longer need to do this.

This chance simplifies the unique constraint to just the document ref (licence ref).

---------

Co-authored-by: Alan Cruikshanks <[email protected]>
  • Loading branch information
jonathangoulding and Cruikshanks authored Oct 15, 2024
1 parent cd7e26c commit 752cf29
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
47 changes: 47 additions & 0 deletions migrations/20241014133216-alter-crm-v2-documents-defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'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', '20241014133216-alter-crm-v2-documents-defaults.js-up.sql')
return new Promise(function (resolve, reject) {
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) {
if (err) return reject(err)
console.log('received data: ' + data)

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

exports.down = function (db) {
const filePath = path.join(__dirname, 'sqls', '20241014133216-alter-crm-v2-documents-defaults.js-down.sql')
return new Promise(function (resolve, reject) {
fs.readFile(filePath, { encoding: 'utf-8' }, function (err, data) {
if (err) return reject(err)
console.log('received data: ' + data)

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,9 @@
BEGIN;

-- Drop unique index on the licence number
DROP INDEX crm_v2.documents_only_one_document_ref;

-- Create unique index on licence number
CREATE UNIQUE INDEX documents_document_ref ON crm_v2.documents(regime, document_type, document_ref);

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
BEGIN;

-- Drop unique index on licence number, document type and regime
DROP INDEX crm_v2.documents_document_ref;

-- -- Create unique index on licence number
CREATE UNIQUE INDEX documents_only_one_document_ref ON crm_v2.documents(document_ref);

COMMIT;

0 comments on commit 752cf29

Please sign in to comment.