Skip to content

Commit

Permalink
fix: update conflicting tests and add condition for deleted docs in c…
Browse files Browse the repository at this point in the history
…ontact and data_record models
  • Loading branch information
njogz committed Aug 1, 2024
1 parent 926d435 commit 6971c52
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 20 deletions.
1 change: 1 addition & 0 deletions models/contacts/contact.sql
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ INNER JOIN
ON source_table._id = document_metadata.uuid
WHERE
document_metadata.doc_type IN ('contact', 'clinic', 'district_hospital', 'health_center', 'person')
AND source_table._deleted = false
{% if is_incremental() %}
AND document_metadata.saved_timestamp >= {{ max_existing_timestamp('saved_timestamp') }}
{% endif %}
6 changes: 6 additions & 0 deletions models/contacts/contacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ models:
data_type: date
- name: sex
data_type: string
- name: deleted
data_type: boolean
- name: place
config:
contract:
Expand All @@ -79,6 +81,8 @@ models:
data_type: timestamp
- name: place_id
data_type: string
- name: deleted
data_type: boolean
- name: patient
config:
contract:
Expand All @@ -98,3 +102,5 @@ models:
data_type: timestamp
- name: patient_id
data_type: string
- name: deleted
data_type: boolean
3 changes: 2 additions & 1 deletion models/contacts/patient.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
SELECT
uuid,
person.saved_timestamp,
couchdb.doc->>'patient_id' as patient_id
couchdb.doc->>'patient_id' AS patient_id,
person.deleted AS deleted
FROM {{ ref('person') }} person
INNER JOIN {{ env_var('POSTGRES_SCHEMA') }}.{{ env_var('POSTGRES_TABLE') }} couchdb ON couchdb._id = uuid
WHERE couchdb.doc->>'patient_id' IS NOT NULL
Expand Down
5 changes: 3 additions & 2 deletions models/contacts/person.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
SELECT
contact.uuid,
contact.saved_timestamp,
(couchdb.doc->>'date_of_birth')::date as date_of_birth,
couchdb.doc->>'sex' as sex
(couchdb.doc->>'date_of_birth')::date AS date_of_birth,
couchdb.doc->>'sex' AS sex,
contact.deleted AS deleted
FROM {{ ref("contact") }} contact
INNER JOIN {{ env_var('POSTGRES_SCHEMA') }}.{{ env_var('POSTGRES_TABLE') }} couchdb ON couchdb._id = uuid
WHERE contact.contact_type = 'person'
Expand Down
3 changes: 2 additions & 1 deletion models/contacts/place.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
SELECT
uuid,
contact.saved_timestamp,
couchdb.doc->>'place_id' as place_id
couchdb.doc->>'place_id' AS place_id,
contact.deleted AS deleted
FROM {{ ref('contact') }} contact
INNER JOIN {{ env_var('POSTGRES_SCHEMA') }}.{{ env_var('POSTGRES_TABLE') }} couchdb ON couchdb._id = uuid
WHERE
Expand Down
1 change: 1 addition & 0 deletions models/forms/data_record.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ INNER JOIN
ON source_table._id = document_metadata.uuid
WHERE
document_metadata.doc_type = 'data_record'
AND source_table._deleted = false
{% if is_incremental() %}
AND document_metadata.saved_timestamp >= {{ max_existing_timestamp('saved_timestamp') }}
{% endif %}
4 changes: 1 addition & 3 deletions test/sqltest/contact.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ WHERE
couchdb.doc->>'type' IN ('contact', 'clinic', 'district_hospital', 'health_center', 'person')
-- TEST CONDITIONS
AND (
-- in couchdb, not in contact table
(contact.uuid IS NULL)
OR -- fields dont match
-- fields dont match
contact.parent_uuid <> couchdb.doc->'parent'->>'_id' OR
contact.contact_type <> COALESCE(couchdb.doc->>'contact_type', couchdb.doc->>'type') OR
contact.phone <> couchdb.doc->>'phone'
Expand Down
4 changes: 1 addition & 3 deletions test/sqltest/data_record.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ WHERE
couchdb.doc->>'type' = 'data_record'
-- TEST CONDITIONS
AND (
-- in couchdb, not in data_record
(data_record.uuid IS NULL)
OR -- fields dont match
-- fields dont match
data_record.from_phone <> couchdb.doc->>'from' OR
data_record.form <> couchdb.doc->>'form' OR
data_record.patient_id <> couchdb.doc->>'patient_id' OR
Expand Down
4 changes: 2 additions & 2 deletions test/sqltest/patient.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ WHERE
couchdb.doc->>'patient_id' IS NOT NULL
-- TEST CONDITIONS
AND (
-- in couchdb, not in patients
patient.uuid IS NULL
-- deleted is true
(patient.deleted = true)
)
6 changes: 2 additions & 4 deletions test/sqltest/person.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ WHERE
)
-- TEST CONDITIONS
AND (
-- in couchdb, not in person
person.uuid IS NULL OR
-- a person, but not a contact?
contact.uuid IS NULL
-- deleted is true
(person.deleted = true)
)
8 changes: 4 additions & 4 deletions test/sqltest/place.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ FROM {{ env_var('POSTGRES_SCHEMA') }}.{{ env_var('POSTGRES_TABLE') }} couchdb
LEFT JOIN {{ ref('place') }} place ON couchdb._id = place.uuid
LEFT JOIN {{ ref('contact') }} contact ON contact.uuid = place.uuid
WHERE
-- person conditions
-- place conditions
(
(couchdb.doc->>'type' <> 'person') AND
(couchdb.doc->>'type' = 'contact' AND couchdb.doc->>'contact_type' <> 'person')
)
-- TEST CONDITIONS
AND (
-- in couchdb, not in place
place.uuid IS NULL OR
-- a person, but not a contact?
-- deleted is true
(place.deleted = true) OR
-- a place, but not a contact?
contact.uuid IS NULL
)

0 comments on commit 6971c52

Please sign in to comment.