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 19 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))

releases/2.42/migration-notes.md Outdated Show resolved Hide resolved
releases/2.42/migration-notes.md Outdated Show resolved Hide resolved
releases/2.42/migration-notes.md Outdated Show resolved Hide resolved
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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

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;
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 should delete only the invalid tracked entities. Without the NOT EXISTS clause, also the fixable ones are going to be deleted

Suggested change
WITH deleted AS (DELETE FROM trackedentity WHERE trackedentitytypeid IS NULL RETURNING *) SELECT COUNT(*) INTO deleted_count FROM deleted;
WITH deleted AS (DELETE FROM trackedentity WHERE 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
)
RETURNING *) SELECT COUNT(*) INTO deleted_count FROM deleted;

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