-
Notifications
You must be signed in to change notification settings - Fork 45
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
docs: Implement cascading deletion for obsolete TrackedEntities[DHIS2-15066] #196
Open
zubaira
wants to merge
15
commits into
master
Choose a base branch
from
notes-DHIS2-15066
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+209
−1
Open
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f367564
Implement cascading deletion for obsolete TrackedEntities
zubaira c3d4d1a
markdown correction
zubaira 0996c92
markdown correction
zubaira dddc7bc
show count of trackedentities being affected
zubaira 8f0a0af
use tables instead of CTE
zubaira 8b80619
updated
zubaira 83a7e3c
Update releases/2.42/migration-notes.md
zubaira db0a42c
updated
zubaira 3b4a028
change migration notes
zubaira 72b8848
Update migration-notes.md
zubaira 29d5fcc
resolve conflict
zubaira dbf5a7c
Merge branch 'master' into notes-DHIS2-15066
zubaira 8622bc2
fix invalid before deletion
zubaira 5e4be1a
fix procedure and then deletion
zubaira 1f4f571
fix reviews
zubaira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,6 +11,7 @@ To help you navigate the document, here's a detailed table of contents. | |||||
- [Inconsistent data](#inconsistent-data) | ||||||
- [Tracker](#tracker) | ||||||
- [Null Organisation Unit](#null-organisation-unit) | ||||||
- [Null Tracked Entity Type](#null-tracked-entity-type) | ||||||
--- | ||||||
## Inconsistent-data | ||||||
|
||||||
|
@@ -272,4 +273,195 @@ BEGIN | |||||
|
||||||
END; | ||||||
$$; | ||||||
``` | ||||||
``` | ||||||
|
||||||
### Null Tracked Entity Type | ||||||
|
||||||
The `TrackedEntity`, previously known as `TrackedEntityInstance`, is required to have a specific type, such as person, place, equipment, or area. However, this constraint was not enforced at the database level, leading to inconsistent data. To ensure data integrity moving forward, we need to enforce this requirement by making the `trackedentitytypeid` column in the `trackedentity` table non-nullable. | ||||||
|
||||||
#### Checking for Null Values | ||||||
|
||||||
To check for any NULL values in this column, you can use the following SQL script. If it returns a value greater than 0, it indicates the presence of inconsistent data in the system. | ||||||
|
||||||
##### For 2.41 Instances: | ||||||
|
||||||
```sql | ||||||
SELECT COUNT(1) | ||||||
FROM trackedentity | ||||||
WHERE trackedentitytypeid IS NULL; | ||||||
``` | ||||||
##### For <= 2.40 Instances: | ||||||
|
||||||
```sql | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||||||
SELECT COUNT(1) | ||||||
FROM trackedentityinstance | ||||||
WHERE trackedentitytypeid IS NULL; | ||||||
``` | ||||||
|
||||||
#### Fixing Null Values | ||||||
zubaira marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Starting from version v42, NULL values are no longer allowed in the trackedentitytypeid column. The migration attempted to address the invalid data, but it was unsuccessful. There are two options going forward. | ||||||
- Change the `NULL` value to a valid trackedentitytypeid. ([Assign trackedentitytyeid to tracked entity](#assign-tracked-entity-type)) | ||||||
- Completely remove invalid trackedentity record. ([Delete trackedentities](#deleting-invalid-trackedenetities)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
##### Assign tracked entity type | ||||||
To assign valid trackedentitytypeid. It can be fetched from program via enrollment. | ||||||
zubaira marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
```sql | ||||||
UPDATE trackedentity SET trackedentitytypeid = ( | ||||||
SELECT program.trackedentitytypeid FROM program | ||||||
JOIN enrollment ON program.programid = enrollment.programid | ||||||
WHERE enrollment.trackedentityid = trackedentity.trackedentityid) | ||||||
WHERE trackedentitytypeid IS NULL AND EXISTS ( | ||||||
SELECT 1 FROM enrollment JOIN program ON enrollment.programid = program.programid | ||||||
WHERE enrollment.trackedentityid = trackedentity.trackedentityid | ||||||
); | ||||||
``` | ||||||
|
||||||
|
||||||
##### Deleting invalid trackedenetities | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
The following script can be used to remove all trackedentities with null in `trackedentitytypeid` column. | ||||||
zubaira marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
```plsql | ||||||
zubaira marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
DO $$ | ||||||
DECLARE | ||||||
invalid_count INT; -- Variable to store the count of invalid TrackedEntities | ||||||
deleted_count INT := 0; -- Variable to keep track of deleted TrackedEntity count | ||||||
BEGIN | ||||||
CREATE TEMP TABLE te AS ( | ||||||
SELECT trackedentityid | ||||||
FROM trackedentity | ||||||
WHERE trackedentitytypeid IS NULL | ||||||
); | ||||||
|
||||||
CREATE TEMP TABLE enrollment_ids AS ( | ||||||
SELECT enrollmentid | ||||||
FROM enrollment | ||||||
WHERE trackedentityid IN (SELECT trackedentityid FROM te) | ||||||
); | ||||||
|
||||||
CREATE TEMP TABLE event_ids AS ( | ||||||
SELECT eventid | ||||||
FROM event | ||||||
WHERE enrollmentid IN (SELECT enrollmentid FROM enrollment_ids) | ||||||
); | ||||||
|
||||||
CREATE TEMP TABLE te_pm AS ( | ||||||
SELECT id | ||||||
FROM programmessage | ||||||
WHERE trackedentityid IN (SELECT trackedentityid FROM te) | ||||||
); | ||||||
|
||||||
CREATE TEMP TABLE pi_pm AS ( | ||||||
SELECT id | ||||||
FROM programmessage | ||||||
WHERE enrollmentid IN (SELECT enrollmentid FROM enrollment_ids) | ||||||
); | ||||||
|
||||||
CREATE TEMP TABLE event_pm AS ( | ||||||
SELECT id | ||||||
FROM programmessage | ||||||
WHERE eventid IN (SELECT eventid FROM event_ids) | ||||||
); | ||||||
|
||||||
SELECT COUNT(trackedentityid) | ||||||
INTO invalid_count | ||||||
FROM trackedentity | ||||||
WHERE trackedentitytypeid IS NULL; | ||||||
|
||||||
RAISE NOTICE 'Number of invalid TrackedEntities (trackedentitytypeid IS NULL): %', invalid_count; | ||||||
|
||||||
-- If there are any invalid TrackedEntities, proceed with deletion | ||||||
IF invalid_count > 0 THEN | ||||||
|
||||||
DELETE FROM programmessage_deliverychannels | ||||||
WHERE programmessagedeliverychannelsid IN (SELECT id FROM te_pm); | ||||||
|
||||||
DELETE FROM programmessage_emailaddresses | ||||||
WHERE programmessageemailaddressid IN (SELECT id FROM te_pm); | ||||||
|
||||||
DELETE FROM programmessage_phonenumbers | ||||||
WHERE programmessagephonenumberid IN (SELECT id FROM te_pm); | ||||||
|
||||||
DELETE FROM programmessage_deliverychannels | ||||||
WHERE programmessagedeliverychannelsid IN (SELECT id FROM pi_pm); | ||||||
|
||||||
DELETE FROM programmessage_emailaddresses | ||||||
WHERE programmessageemailaddressid IN (SELECT id FROM pi_pm); | ||||||
|
||||||
DELETE FROM programmessage_phonenumbers | ||||||
WHERE programmessagephonenumberid IN (SELECT id FROM pi_pm); | ||||||
|
||||||
DELETE FROM programmessage_deliverychannels | ||||||
WHERE programmessagedeliverychannelsid IN (SELECT id FROM event_pm); | ||||||
|
||||||
DELETE FROM programmessage_emailaddresses | ||||||
WHERE programmessageemailaddressid IN (SELECT id FROM event_pm); | ||||||
|
||||||
DELETE FROM programmessage_phonenumbers | ||||||
WHERE programmessagephonenumberid IN (SELECT id FROM event_pm); | ||||||
|
||||||
DELETE FROM event_notes | ||||||
WHERE eventid IN (SELECT eventid FROM event_ids); | ||||||
|
||||||
DELETE FROM enrollment_notes | ||||||
WHERE enrollmentid IN (SELECT enrollmentid FROM enrollment_ids); | ||||||
|
||||||
DELETE FROM note | ||||||
WHERE noteid NOT IN ( | ||||||
SELECT noteid FROM event_notes | ||||||
UNION ALL | ||||||
SELECT noteid FROM enrollment_notes | ||||||
); | ||||||
|
||||||
DELETE FROM trackedentitydatavalueaudit | ||||||
WHERE eventid IN (SELECT eventid FROM event_ids); | ||||||
|
||||||
DELETE FROM programmessage | ||||||
WHERE eventid IN (SELECT eventid FROM event_ids); | ||||||
|
||||||
DELETE FROM programmessage | ||||||
WHERE enrollmentid IN (SELECT enrollmentid FROM enrollment_ids); | ||||||
|
||||||
DELETE FROM event | ||||||
WHERE enrollmentid IN (SELECT enrollmentid FROM enrollment_ids); | ||||||
|
||||||
DELETE FROM programmessage | ||||||
WHERE trackedentityid IN (SELECT trackedentityid FROM te); | ||||||
|
||||||
DELETE FROM relationshipitem | ||||||
WHERE trackedentityid IN (SELECT trackedentityid FROM te); | ||||||
|
||||||
DELETE FROM trackedentityattributevalue | ||||||
WHERE trackedentityid IN (SELECT trackedentityid FROM te); | ||||||
|
||||||
DELETE FROM trackedentityattributevalueaudit | ||||||
WHERE trackedentityid IN (SELECT trackedentityid FROM te); | ||||||
|
||||||
DELETE FROM trackedentityprogramowner | ||||||
WHERE trackedentityid IN (SELECT trackedentityid FROM te); | ||||||
|
||||||
DELETE FROM programtempowner | ||||||
WHERE trackedentityid IN (SELECT trackedentityid FROM te); | ||||||
|
||||||
DELETE FROM programtempownershipaudit | ||||||
WHERE trackedentityid IN (SELECT trackedentityid FROM te); | ||||||
|
||||||
DELETE FROM programownershiphistory | ||||||
WHERE trackedentityid IN (SELECT trackedentityid FROM te); | ||||||
|
||||||
DELETE FROM enrollment | ||||||
WHERE trackedentityid IN (SELECT trackedentityid FROM te); | ||||||
|
||||||
WITH deleted AS (DELETE FROM trackedentity WHERE trackedentitytypeid IS NULL RETURNING *) SELECT COUNT(*) INTO deleted_count FROM deleted; | ||||||
|
||||||
RAISE NOTICE 'Total number of TrackedEntities deleted: %', deleted_count; | ||||||
|
||||||
ELSE | ||||||
RAISE NOTICE 'No invalid TrackedEntities found for deletion.'; | ||||||
END IF; | ||||||
|
||||||
DROP TABLE IF EXISTS te, enrollment_ids, event_ids, te_pm, pi_pm, event_pm; | ||||||
|
||||||
END; | ||||||
$$; | ||||||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we shouldn't show entities that we can fix in the migration, so we should filter out any tracked entity that has an enrollment with a program that has a defined trackedEntityType
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think we should change this query to not show the entities that will be fixed by the migration