Skip to content

Commit

Permalink
fix: Raise exception with details on failing migration [DHIS2-12600]
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante committed Dec 3, 2024
1 parent 1bb7f34 commit 36dd387
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private QueryWithOrderBy buildEnrollmentHql(EnrollmentQueryParams params) {
hql += hlp.whereAnd() + "en.program.uid = '" + params.getProgram().getUid() + "'";
}

// TODO(DHIS2-17961) This will be removed when placeholder enrollments will not exist anymore
// TODO(DHIS2-17961) This will be removed when dummy enrollments will not exist anymore
hql += hlp.whereAnd() + "en.program.programType = '" + ProgramType.WITH_REGISTRATION + "'";

if (params.hasEnrollmentStatus()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,18 @@
DO $$
DECLARE dummyOrgUnitId bigint;
DECLARE dummyOrgUnitUid varchar(11);
BEGIN
select coalesce((select max(organisationunitid) + 1 from organisationunit), 1) into dummyOrgUnitId;
select generate_uid() into dummyOrgUnitUid;
-- Set null organisation unit of dummy enrollments to root organisation unit
update enrollment en
set organisationunitid = (select organisationunitid from organisationunit order by hierarchylevel limit 1)
where en.organisationunitid is null
and en.programid in (select programid from program where type = 'WITHOUT_REGISTRATION');

while (select count(*) from organisationunit where uid = dummyOrgUnitUid) > 0 loop
select generate_uid() into dummyOrgUnitUid;
end loop;
alter table if exists enrollment alter column organisationunitid set not null;

insert into organisationunit
(organisationunitid, name, code, parentid, shortname, openingdate, created, lastupdated, uid, hierarchylevel)
values
(dummyOrgUnitId,
'DUMMY OU',
'DUMMY_OU_CODE',
null,
'DUMMY OU',
'1970-01-01',
now(),
now(),
dummyOrgUnitUid,
0);
alter table if exists event alter column organisationunitid set not null;

-- Update null organisation unit of enrollments to dummy organisation unit
update enrollment en set organisationunitid = dummyOrgUnitId
where en.organisationunitid is null;
EXCEPTION
WHEN not_null_violation THEN
RAISE EXCEPTION 'There are inconsistent data in your DB. Please check https://github.com/dhis2/dhis2-releases/blob/master/releases/2.42/migration-notes.md to have more information on the issue and to find ways to fix it. Detailed error message: %', SQLERRM;

alter table enrollment alter column organisationunitid set not null;

-- Update null organisation unit of event to dummy organisation unit
update event ev set organisationunitid = dummyOrgUnitId
where ev.organisationunitid is null;

alter table event alter column organisationunitid set not null;
END $$;
END;
$$;

0 comments on commit 36dd387

Please sign in to comment.