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

Rwa 805 ensure orders have orderers. #8

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
5 changes: 3 additions & 2 deletions migration/src/main/resources/migrations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
https://wiki.openmrs.org/display/docs/Prepare+for+Upgrading+From+a+Pre-1.10+to+1.10+or+Later+Version
https://github.com/rwanda-rbc-emr/openmrs-module-mohbeforeoneelevenupgrade
-->

<include file="migrations/ensure-users-are-providers.xml" relativeToChangelogFile="true" />
<include file="migrations/ensure-orders-have-start-dates.xml" relativeToChangelogFile="true" />
<!-- <include file="migrations/ensure-orders-have-encounter-ids.xml" relativeToChangelogFile="true" />-->
<include file="migrations/ensure-unknown-provider-configured.xml" relativeToChangelogFile="true" />
<include file="migrations/ensure-orders-have-encounters.xml" relativeToChangelogFile="true" />
<include file="migrations/ensure-orders-have-orderers.xml" relativeToChangelogFile="true" />
<include file="migrations/ensure-orders-have-discontinued-date.xml" relativeToChangelogFile="true" />

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,48 @@
then create the encounters for orders that don’t have encounters
-->
<sql>
There are over 300,000 orders on both Rwink and Butaro with no linked encounter
select count(*) from orders where encounter_id is null
SET @EncTUUID := "a9ef19d1-8875-11ea-8c74-7a7919290ad6";

Of these, there are still between 100k and 150k on each server with no encounters on the same day

select count(*)
from orders o
left join encounter e on o.patient_id = e.patient_id and date(o.start_date) = date(e.encounter_datetime)
where e.encounter_id is null;
/* Create EncounterType */
INSERT INTO `encounter_type`
(`name`,`description`,`creator`,`date_created`,`uuid`)
VALUES
("Drug Order Encounter","Encounter type used for recording drug orders in an encounter when no existing encounter is available",1,now(),@EncTUUID);

So we need to figure out a strategy for this.
/* Get encounterType id */
select @encTId:=encounter_type_id from encounter_type where uuid=@EncTUUID;

Safest thing for now might be to create new encounters for all of these. Later on, if we want to merge
some of these into other existing enocunters for the same patient on the same day, we can do so,
but that isn't needed for migration and might not be 100% accurate.

This could be something like a SQL script that runs the following statements:
/* Create encounters */
INSERT INTO `encounter`
(`encounter_type`,`patient_id`,`encounter_datetime`,`creator`,`date_created`,`voided`,`voided_by`,`date_voided`,`void_reason`,`uuid`)
SELECT
@encTId,patient_id,start_date,creator,now(),voided,voided_by,date_voided,void_reason,UUID()
FROM orders
WHERE encounter_id is null group by patient_id,creator,date_created;

/* Update orders*/
UPDATE orders O
LEFT JOIN encounter ENC
ON O.patient_id = ENC.patient_id
AND O.start_date=ENC.encounter_datetime
AND O.creator=ENC.creator
AND O.date_created=ENC.date_created
SET O.encounter_id=ENC.encounter_id
WHERE O.encounter_id is null AND ENC.encounter_type=@encTId;


/* Create encounter Providers */


INSERT INTO `encounter_provider`(`encounter_id`,`provider_id`,`encounter_role_id`,`creator`,`date_created`,`changed_by`,`date_changed`,`voided`,`date_voided`,`voided_by`,`void_reason`,`uuid`)
SELECT enc.encounter_id,pr.provider_id,1,enc.creator,enc.date_created,enc.changed_by,enc.date_changed,enc.voided,enc.date_voided,enc.voided_by,enc.void_reason,UUID()
FROM encounter enc
INNER JOIN users US ON enc.creator = US.user_id
INNER JOIN provider pr ON US.person_id=pr.person_id
WHERE encounter_type = @encTId;

1. Create new encounter type if not exists with known, fixed uuid (eg. with name "Drug Order Encounter")
2. Get this encounter_type_id as @encId
3. Add new encounter for each patient/order.start_date combination
4. Update all orders with null encounter by setting them to the encounter for matching patient/date with the created encounter_type

</sql>
</changeSet>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

<!-- In 1.10+, All users that created orders(orders.orderer) have provider accounts -->

<changeSet id="ensure-orders-have-orderers" author="IMB">
<sql>

update orders set orderer = creator where orderer is null;

</sql>

</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@

<!-- In 1.10+, All users that created orders(orders.orderer) have provider accounts -->

<changeSet id="ensure-orders-creators-are-providers" author="IMB">
<changeSet id="ensure-users-are-providers" author="IMB">
<sql>

<!-- TODO: All users that created orders(orders.orderer) have provider accounts -->
check for

select distinct u.person_id
from orders o,users u
where o.orderer=u.user_id and u.person_id not in (select person_id from provider);

if return something then run this

insert into provider(person_id, identifier, creator, date_created, retired, retired_by, date_retired, retire_reason, uuid)
select distinct u.person_id, u.system_id, 1, CURRENT_TIMESTAMP, u.retired, u.retired_by, u.date_retired, u.retire_reason, UUID()
from orders o,users u where o.orderer=u.user_id and u.person_id not in (select person_id from provider);
insert into provider (`person_id`,`identifier`,`creator`,`date_created`,
`retired`,`retired_by`,`date_retired`,`retire_reason`,`uuid`)
select u.person_id,u.system_id,1,now(),
u.retired,u.retired_by,u.date_retired,u.retire_reason,UUID()
from users u
left join provider p on u.person_id = p.person_id
where p.provider_id is null ;

</sql>
<!--
Expand Down