Skip to content

Commit c1b1eb8

Browse files
fix: Add dummy orgUnit to tracker objects with null orgUnit
1 parent 9466146 commit c1b1eb8

File tree

3 files changed

+33
-57
lines changed

3 files changed

+33
-57
lines changed

dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/program/hibernate/Enrollment.hbm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
</list>
7878

7979
<many-to-one name="organisationUnit" class="org.hisp.dhis.organisationunit.OrganisationUnit" column="organisationunitid"
80-
foreign-key="fk_programinstance_organisationunitid" />
80+
foreign-key="fk_programinstance_organisationunitid" not-null="true"/>
8181

8282
</class>
8383

dhis-2/dhis-services/dhis-service-core/src/main/resources/org/hisp/dhis/program/hibernate/Event.hbm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<property name="occurredDate" column="occurreddate" type="timestamp" index="programstageinstance_executiondate" />
5050

5151
<many-to-one name="organisationUnit" class="org.hisp.dhis.organisationunit.OrganisationUnit" column="organisationunitid"
52-
foreign-key="fk_programstageinstance_organisationunitid" index="programstageinstance_organisationunitid" />
52+
foreign-key="fk_programstageinstance_organisationunitid" index="programstageinstance_organisationunitid" not-null="true"/>
5353

5454
<property name="status" column="status" type="org.hisp.dhis.program.EventStatusUserType" not-null="true" />
5555

Original file line numberDiff line numberDiff line change
@@ -1,61 +1,37 @@
1-
-- Delete invalid events linked to invalid enrollments.
2-
delete from event e where e.enrollmentid in (
3-
select en.enrollmentid
4-
from enrollment en join program p on en.programid = p.programid
5-
where p.type = 'WITH_REGISTRATION'
6-
and en.trackedentityid is null
7-
);
1+
DO $$
2+
DECLARE dummyOrgUnitId bigint;
3+
DECLARE dummyOrgUnitUid varchar(11);
4+
BEGIN
5+
select coalesce((select max(organisationunitid) + 1 from organisationunit), 1) into dummyOrgUnitId;
6+
select generate_uid() into dummyOrgUnitUid;
87

9-
-- Delete invalid enrollments that are part of a tracker program and
10-
-- have null tracked entity.
11-
delete from enrollment en where en.programid in (
12-
select p.programid
13-
from program p
14-
where p.type = 'WITH_REGISTRATION'
15-
)
16-
and en.trackedentityid is null;
8+
while (select count(*) from organisationunit where uid = dummyOrgUnitUid) > 0 loop
9+
select generate_uid() into dummyOrgUnitUid;
10+
end loop;
1711

18-
-- Update null organisation unit of enrollments to organisation unit of one of its events
19-
update enrollment en set organisationunitid =
20-
(select distinct ev.organisationunitid
21-
from event ev
22-
where en.enrollmentid = ev.enrollmentid
23-
and ev.organisationunitid is not null
24-
limit 1)
25-
where en.organisationunitid is null;
12+
insert into organisationunit
13+
(organisationunitid, name, code, parentid, shortname, openingdate, created, lastupdated, uid, hierarchylevel)
14+
values
15+
(dummyOrgUnitId,
16+
'DUMMY OU',
17+
'DUMMY_OU_CODE',
18+
null,
19+
'DUMMY OU',
20+
'1970-01-01',
21+
now(),
22+
now(),
23+
dummyOrgUnitUid,
24+
0);
2625

27-
-- If organisationunitid column is still null for any placeholder enrollment,
28-
-- update organisation unit to root organisation unit.
29-
-- Placeholder enrollments do not have a tracked entity, so we need to use one well-know
30-
-- organisation unit.
31-
-- Organisation unit for this enrollments is never used anyway but we still need to fill in a value.
32-
update enrollment en set organisationunitid = (
33-
select distinct organisationunitid
34-
from organisationunit
35-
where parentid is null
36-
limit 1
37-
)
38-
where en.organisationunitid is null
39-
and (select type from program p where en.programid = p.programid) = 'WITHOUT_REGISTRATION';
26+
-- Update null organisation unit of enrollments to dummy organisation unit
27+
update enrollment en set organisationunitid = dummyOrgUnitId
28+
where en.organisationunitid is null;
4029

41-
-- If organisationunitid column is still null for any enrollment that is not a placeholder,
42-
-- use the tracked entity organisation unit.
43-
-- Tracked entity organisation unit is guaranteed to be not null.
44-
update enrollment en set organisationunitid =
45-
(select te.organisationunitid
46-
from trackedentity te
47-
where en.trackedentityid = te.trackedentityid)
48-
where en.organisationunitid is null
49-
and (select type from program p where en.programid = p.programid) = 'WITH_REGISTRATION';
30+
alter table enrollment alter column organisationunitid set not null;
5031

51-
alter table enrollment alter column organisationunitid set not null;
32+
-- Update null organisation unit of event to dummy organisation unit
33+
update event ev set organisationunitid = dummyOrgUnitId
34+
where ev.organisationunitid is null;
5235

53-
-- Update null organisation unit of event to organisation unit the enrollment
54-
-- that at this point is guaranteed to be not null.
55-
update event ev set organisationunitid = (
56-
select organisationunitid
57-
from enrollment en
58-
where en.enrollmentid = ev.enrollmentid)
59-
where ev.organisationunitid is null;
60-
61-
alter table event alter column organisationunitid set not null;
36+
alter table event alter column organisationunitid set not null;
37+
END $$;

0 commit comments

Comments
 (0)