-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Raise exception with details on failing migration [DHIS2-12600]
- Loading branch information
1 parent
1bb7f34
commit 36dd387
Showing
2 changed files
with
13 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 12 additions & 31 deletions
43
...urces/org/hisp/dhis/db/migration/2.42/V2_42_28__Make_ou_event_and_enrollment_not_null.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
$$; |