Skip to content
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
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

zubaira
Copy link
Contributor

@zubaira zubaira commented Dec 5, 2024

No description provided.

@zubaira zubaira changed the title docs: Implement cascading deletion for obsolete TrackedEntities[DHIS2-15066] docs: [WIP] Implement cascading deletion for obsolete TrackedEntities[DHIS2-15066] Dec 5, 2024
@zubaira zubaira changed the title docs: [WIP] Implement cascading deletion for obsolete TrackedEntities[DHIS2-15066] docs: Implement cascading deletion for obsolete TrackedEntities[DHIS2-15066] Dec 6, 2024
##### For 2.41 Instances:

```sql
Copy link
Contributor

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

Copy link
Contributor

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

##### For <= 2.40 Instances:

```sql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

releases/2.42/migration-notes.md Outdated Show resolved Hide resolved
releases/2.42/migration-notes.md Outdated Show resolved Hide resolved


##### Deleting invalid trackedenetities
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
##### Deleting invalid trackedenetities
##### Deleting invalid tracked entities

##### For 2.41 Instances:

```sql
Copy link
Contributor

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

releases/2.42/migration-notes.md Show resolved Hide resolved


##### Deleting invalid tracked enetities
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
##### Deleting invalid tracked enetities
##### Deleting invalid tracked entities

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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Completely remove invalid trackedentity record. ([Delete trackedentities](#deleting-invalid-trackedenetities))
- Completely remove invalid trackedentity record. ([Delete trackedentities](#deleting-invalid-tracked-entities))

Comment on lines +289 to +296
FROM trackedentity te
WHERE te.trackedentitytypeid IS NULL
AND NOT EXISTS (
SELECT 1
FROM enrollment e
WHERE e.trackedentityid = te.trackedentityid
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SELECT COUNT(1)
FROM trackedentity te
WHERE te.trackedentitytypeid IS NULL
AND NOT EXISTS (
SELECT 1
FROM enrollment e
WHERE e.trackedentityid = te.trackedentityid
);
SELECT COUNT(1)
FROM trackedentity te
WHERE te.trackedentitytypeid IS NULL
AND NOT EXISTS (
SELECT 1
FROM enrollment e JOIN program p on e.programid = p.programid
WHERE e.trackedentityid = te.trackedentityid and p.trackedentitytypeid IS NOT NULL
);

Comment on lines +301 to +308
FROM trackedentityinstance te
WHERE te.trackedentitytypeid IS NULL
AND NOT EXISTS (
SELECT 1
FROM programinstance pi
WHERE pi.trackedentityinstanceid = te.trackedentityinstanceid
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SELECT COUNT(1)
FROM trackedentityinstance te
WHERE te.trackedentitytypeid IS NULL
AND NOT EXISTS (
SELECT 1
FROM programinstance pi
WHERE pi.trackedentityinstanceid = te.trackedentityinstanceid
);
SELECT COUNT(1)
FROM trackedentityinstance te
WHERE te.trackedentitytypeid IS NULL
AND NOT EXISTS (
SELECT 1
FROM programinstance pi JOIN program p ON pi.programid = p.programid
WHERE pi.trackedentityinstanceid = te.trackedentityinstanceid and p.trackedentitytypeid IS NOT NULL
);


```sql
UPDATE trackedentity SET trackedentitytypeid=( SELECT trackedentitytypeid FROM trackedentitytype WHERE uid='{REFERENCE_UID}') WHERE trackedentitytypeid IS NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This query is changing all the trackedEntities with null tracked entity type at once.
Can we change the count queries to return the tracked entity uids to be fixed instead of just a count?
And then add them here as a parameter, like AND trackedentityid IN ({uids})?

Comment on lines +382 to +384
FROM enrollment
WHERE enrollment.trackedentityid = trackedentity.trackedentityid
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SELECT 1
FROM enrollment
WHERE enrollment.trackedentityid = trackedentity.trackedentityid
SELECT 1
FROM enrollment e JOIN program p on e.programid = p.programid
WHERE e.trackedentityid = te.trackedentityid and p.trackedentitytypeid IS NOT NULL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants