Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion migration/src/main/resources/migrations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
-->

<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-discontinued-date.xml" relativeToChangelogFile="true" />

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,43 @@
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

Of these, there are still between 100k and 150k on each server with no encounters on the same day
SET @EncTUUID := "a9ef19d1-8875-11ea-8c74-7a7919290ad6";

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;

So we need to figure out a strategy for this.
/* Create EncounterType */
INSERT INTO `encounter_type`
(`name`,`description`,`creator`,`date_created`,`uuid`)
VALUES
(
"Drug Order Encounter","for orders without encounters",1,now(),@EncTUUID);
Copy link
Member

@mseaton mseaton Apr 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure that this is what we want to name this encounter type? And can the description be capitalized?


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.
/* Get encounterType id */
select @encTId:=encounter_type_id from encounter_type where uuid=@EncTUUID;


/* 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,date_created,voided,voided_by,date_voided,void_reason,UUID() FROM orders WHERE encounter_id is null;

/* 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;


/* select the unknown provider */

SELECT @UnknownProvider := provider_id FROM provider WHERE uuid="6a7d7d04-f523-11e5-9ce9-5e5517507c66";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we always want to use the unknown provider? Or are there cases where we'd want to use the creator's provider account?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would work if ticket RWA-805 "Ensure all orders creators are providers" is done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjoshuadevelopment I'm not sure this is true. My understanding of what @jberchmas did in RWA-801 is to retrieve the unknown provider uuid by provider name, and then use this to set the uuid as a global property, as the uuid is different on Rwink and Butaro servers. So as written, I don't think your migration will work on both Rwink and Butaro.

I'd recommend one of two approaches:

a) Ensure your migration runs after the one in RWA-801, and change your code above to get the unknownprovider uuid from the global_property table that RWA-801 is populating.

b) Create a common function that is part of a library of functions we create before any migrations execute, and which can be used by both migration scripts to retrieve the uuid of the unknown provider (we can follow this pattern for other sql functions that might help with our migrations). eg.

create or replace function unknown_provider_uuid() as ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering this ticket to depend on RWA-819 where all creators of encounters will be providers

/* 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 encounter_id,@UnknownProvider,1,creator,date_created,changed_by,date_changed,voided,date_voided,voided_by,void_reason,UUID()
FROM encounter WHERE encounter_type = @encTId;

This could be something like a SQL script that runs the following statements:

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