-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
78 - Fix checksum migration issue (#79)
- Rollback changes in immutable migration file and applied intended changes in new SQL script. Migrations files are signed with a md5 sum in the DB to avoid modifications, so the SQL file was restored to its original state. - The changes were moved to a new a script, but because is not possible to add a new column to a materialized view, the change was applied recreating the view, and all its dependents indexes and views. - The changes to the view `raw_contacts` that was rolled back was redundant, because the same migration to add 'contact' to the list of values for `doc->>'type'` was added in an earlier migration script. - Add README notes and .gitignore rule. Co-authored-by: Diana Barsan <35681649+dianabarsan@users.noreply.github.com>
- Loading branch information
1 parent
5fd77a8
commit baaaf21
Showing
4 changed files
with
109 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
.idea/ |
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
4 changes: 2 additions & 2 deletions
4
libs/analytics/migrations/201606200952.do.2318-prepareContacts.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
61 changes: 61 additions & 0 deletions
61
libs/analytics/migrations/202104281912.do.0078-contactType.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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
-- Recreates the view only to add the new column contact_type | ||
-- Replaces -> https://github.com/medic/medic-couch2pg/blob/53c8b27aa6e33c8e2a62dc7d8f697cad20b891c7/libs/analytics/migrations/201606200952.do.2318-prepareContacts.sql#L5-L25 | ||
DROP MATERIALIZED VIEW IF EXISTS contactview_metadata CASCADE; | ||
CREATE MATERIALIZED VIEW contactview_metadata AS | ||
SELECT doc->>'_id' AS uuid, | ||
doc->>'name' AS name, | ||
doc->>'type' AS type, | ||
doc->>'contact_type' AS contact_type, --> only this is new | ||
doc#>>'{contact,_id}' AS contact_uuid, | ||
doc#>>'{parent,_id}' AS parent_uuid, | ||
doc->>'notes' AS notes, | ||
TIMESTAMP WITH TIME ZONE 'epoch' + (doc->>'reported_date')::numeric / 1000 * interval '1 second' AS reported | ||
FROM raw_contacts; | ||
|
||
CREATE UNIQUE INDEX contactview_metadata_uuid ON contactview_metadata (uuid); | ||
CREATE INDEX contactview_metadata_contact_uuid ON contactview_metadata (contact_uuid); | ||
CREATE INDEX contactview_metadata_parent_uuid ON contactview_metadata (parent_uuid); | ||
CREATE INDEX contactview_metadata_type ON contactview_metadata (type); | ||
|
||
-- NOTE: The recreation of the view above caused 4 other views to be dropped in cascade, | ||
-- here are the scripts to recreate them: | ||
|
||
-- Replace -> https://github.com/medic/medic-couch2pg/blob/53c8b27aa6e33c8e2a62dc7d8f697cad20b891c7/libs/analytics/migrations/201606200952.do.2318-prepareContacts.sql#L31 | ||
CREATE VIEW contactview_hospital AS | ||
SELECT cmd.uuid, cmd.name | ||
FROM contactview_metadata AS cmd | ||
WHERE cmd.type = 'district_hospital'; | ||
|
||
-- Replace -> https://github.com/medic/medic-couch2pg/blob/53c8b27aa6e33c8e2a62dc7d8f697cad20b891c7/libs/analytics/migrations/201606200952.do.2318-prepareContacts.sql#L48 | ||
CREATE VIEW contactview_chw AS | ||
SELECT chw.name, pplfields.*, chwarea.uuid AS area_uuid, | ||
chwarea.parent_uuid AS branch_uuid | ||
FROM contactview_person_fields AS pplfields | ||
INNER JOIN contactview_metadata AS chw ON (chw.uuid = pplfields.uuid) | ||
INNER JOIN contactview_metadata AS chwarea ON (chw.parent_uuid = chwarea.uuid) | ||
WHERE pplfields.parent_type = 'health_center'; | ||
|
||
-- Replace -> https://github.com/medic/medic-couch2pg/blob/53c8b27aa6e33c8e2a62dc7d8f697cad20b891c7/libs/analytics/migrations/201606200952.do.2318-prepareContacts.sql#L57 | ||
CREATE VIEW contactview_clinic AS | ||
SELECT cmd.uuid, cmd.name, chw.uuid AS chw_uuid, cmd.reported AS created | ||
FROM contactview_metadata AS cmd | ||
INNER JOIN contactview_chw AS chw ON (cmd.parent_uuid = chw.area_uuid) | ||
WHERE type = 'clinic'; | ||
|
||
-- Replace -> https://github.com/medic/medic-couch2pg/blob/53c8b27aa6e33c8e2a62dc7d8f697cad20b891c7/libs/analytics/migrations/201711071603.do.2635-removeNestedContactReferences.sql#L4 | ||
CREATE VIEW contactview_clinic_person AS | ||
SELECT | ||
raw_contacts.doc ->> '_id' AS uuid, | ||
raw_contacts.doc ->> 'name' AS name, | ||
raw_contacts.doc ->> 'type' AS type, | ||
raw_contacts.doc #>> '{parent,_id}' AS family_uuid, | ||
raw_contacts.doc ->> 'phone' AS phone, | ||
raw_contacts.doc ->> 'alternative_phone' AS phone2, | ||
raw_contacts.doc ->> 'date_of_birth' AS date_of_birth, | ||
cmeta.type AS parent_type | ||
FROM | ||
raw_contacts | ||
LEFT JOIN contactview_metadata AS cmeta ON (doc #>> '{parent,_id}' = cmeta.uuid) | ||
WHERE | ||
raw_contacts.doc->>'type' = 'person' AND | ||
raw_contacts.doc->>'_id' IN (SELECT contact_uuid FROM contactview_metadata WHERE type = 'clinic'); |