Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle amended licence refs in rtn versions import (#1046)
https://eaflood.atlassian.net/browse/WATER-4794 While working on WATER-4734, we pulled together a list of all licences deleted from NALD that still exist in WRLS. Because we have the latest version of the WATER-4654 (return version data clean-up) changes in our non-prod environments (excluding pre), we were expecting no deleted licences to be flagged as having return logs. However, six came back in the results. We know our previous changes to clean the return versions handle return versions linked to 'completed' return logs. If this scenario occurs, we won't delete it until it can be looked at. So, when it comes to cleaning the licences, we can do a bit of a shortcut: any WRLS licence due to be deleted which still has a `water.return_version` record must do so because it is linked to a 'completed' return log. Taking **MD/0280003/012** as an example, we confirmed there are no return logs but it was one of the six returned in the results. Huh? If this licence is deleted in NALD, then so must be the return versions and all child records, so why have we not deleted it in our clean job? What we found is the NALD return version has not been deleted. It and all the child records remain in NALD. However, when we look at the NALD licence record it is linked to, it has a `LIC_NO` of **MD/028/0003/012**. Can you spot the difference?! 😂 We've deduced that when first creating the licence in NALD, a typo was made with the licence reference. But WRLS would have imported the record and created a **MD/0280003/012** `water.licences` record. When the typo was spotted, rather than creating a new NALD licence record, the typo was corrected. If the previous team had used the NALD table ID, as they do with all other tables, then the import would have just corrected the WRLS licence record. However, they use the licence reference as the unique identifier for reasons known only to them! This means the import thinks **MD/028/0003/012** is an entirely new licence, so it creates a second WRLS record. During the nightly import, the NALD return version records update the existing information, but they point to the wrong WRLS licence record! Initially, we thought this would require manual intervention, so we added [a fix migration to water-abstraction-service](DEFRA/water-abstraction-service#2660). However, after double-checking the import query for the return versions, we realised if we included `licence_id` in the `ON CONFLICT DO UPDATE` statement, this would also fix the issue. Plus, this would fix any future licence reference amendments.
- Loading branch information