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- );
8-
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 ;
17-
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 ;
26-
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' ;
40-
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' ;
50-
51- alter table enrollment alter column organisationunitid set not null ;
52-
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 ;
1+ -- Function that randomly creates an alphanumeric char.
2+ -- Passing a value of 0 or 1 will create a random alphabetic char.
3+ -- Passing a value between 0 and 2 will create a random alphanumeric char.
4+ CREATE OR REPLACE FUNCTION new_char (checks int ) RETURNS text
5+ AS $$
6+ select
7+ CASE
8+ WHEN checks = 0 THEN (SELECT chr((65 + round(random() * 25 )) :: integer ))
9+ WHEN checks = 1 THEN (SELECT chr((97 + round(random() * 25 )) :: integer ))
10+ WHEN checks >= 2 THEN (SELECT chr((48 + round(random() * 9 )) :: integer ))
11+ END $$
12+ LANGUAGE SQL;
13+
14+ CREATE OR REPLACE FUNCTION new_uid () RETURNS text
15+ AS $$
16+ select new_char((round(random()))::int )
17+ || new_char(round((random() * 2 ))::int )
18+ || new_char(round((random() * 2 ))::int )
19+ || new_char(round((random() * 2 ))::int )
20+ || new_char(round((random() * 2 ))::int )
21+ || new_char(round((random() * 2 ))::int )
22+ || new_char(round((random() * 2 ))::int )
23+ || new_char(round((random() * 2 ))::int )
24+ || new_char(round((random() * 2 ))::int )
25+ || new_char(round((random() * 2 ))::int )
26+ || new_char(round((random() * 2 ))::int ); $$
27+ LANGUAGE SQL;
28+
29+ DO $$
30+ DECLARE dummyOrgUnitId bigint ;
31+ DECLARE dummyOrgUnitUid varchar (11 );
32+ BEGIN
33+ select coalesce((select max (organisationunitid) + 1 from organisationunit), 1 ) into dummyOrgUnitId;
34+ select new_uid() into dummyOrgUnitUid;
35+
36+ while (select count (* ) from organisationunit where uid = dummyOrgUnitUid) > 0 loop
37+ select new_uid() into dummyOrgUnitUid;
38+ end loop;
39+
40+ insert into organisationunit
41+ (organisationunitid, name, code, parentid, shortname, openingdate, created, lastupdated, uid, hierarchylevel)
42+ values
43+ (dummyOrgUnitId,
44+ ' DUMMY OU' ,
45+ ' DUMMY_OU_CODE' ,
46+ null ,
47+ ' DUMMY OU' ,
48+ ' 1970-01-01' ,
49+ now(),
50+ now(),
51+ dummyOrgUnitUid,
52+ 99 );
53+
54+ -- Update null organisation unit of enrollments to dummy organisation unit
55+ update enrollment en set organisationunitid = dummyOrgUnitId
56+ where en .organisationunitid is null ;
57+
58+ alter table enrollment alter column organisationunitid set not null ;
59+
60+ -- Update null organisation unit of event to dummy organisation unit
61+ update event ev set organisationunitid = dummyOrgUnitId
62+ where ev .organisationunitid is null ;
63+
64+ alter table event alter column organisationunitid set not null ;
65+ END $$;
0 commit comments