Skip to content

Commit

Permalink
feat(#144): align person and place models with datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
witash committed Sep 3, 2024
1 parent 4ba4454 commit a52c859
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 31 deletions.
3 changes: 0 additions & 3 deletions models/contacts/contact.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ SELECT
doc->'parent'->>'_id' AS parent_uuid,
doc->>'name' AS name,
COALESCE(doc->>'contact_type', doc->>'type') as contact_type,
doc->>'phone' AS phone,
doc->>'alternative_phone' AS phone2,
doc->>'is_active' AS active,
doc->>'notes' AS notes,
doc->>'contact_id' AS contact_id,
NULLIF(doc->> 'muted', '') AS muted
FROM {{ ref('document_metadata') }} document_metadata
INNER JOIN
Expand Down
23 changes: 23 additions & 0 deletions models/contacts/contact_type.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{{ config(materialized = 'materialized_view') }}

WITH settings AS (
SELECT
jsonb_array_elements(source_table.doc->'settings'->'contact_types') as element
FROM
{{ source('couchdb', env_var('POSTGRES_TABLE')) }} source_table
WHERE _id = 'settings'
),
existing AS (
SELECT
DISTINCT contact_type as id
FROM {{ ref('contact') }}
)
SELECT
COALESCE(settings.element->>'id', existing.id) as id,
CASE
WHEN id = 'person' THEN TRUE
ELSE COALESCE(settings.element->>'person', 'false')::boolean
END AS person,
(settings.element IS NULL) AS configured
FROM settings
FULL OUTER JOIN existing ON existing.id = settings.element->>'id'
25 changes: 19 additions & 6 deletions models/contacts/contacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,23 @@ models:
data_type: string
data_tests:
- not_null
- name: phone
data_type: string
- name: phone2
data_type: string
- name: active
data_type: string
- name: notes
data_type: string
- name: contact_id
data_type: string
- name: muted
data_type: string
- name: contact_type
config:
contract:
enforced: true
columns:
- name: id
data_type: string
- name: person
data_type: boolean
- name: configured
data_type: boolean
- name: person
config:
contract:
Expand All @@ -70,6 +75,12 @@ models:
data_type: date
- name: sex
data_type: string
- name: patient_id
data_type: string
- name: phone
data_type: string
- name: phone2
data_type: string
- name: place
config:
contract:
Expand All @@ -89,6 +100,8 @@ models:
data_type: timestamp
- name: place_id
data_type: string
- name: contact_id
data_type: string
- name: patient
config:
contract:
Expand Down
8 changes: 6 additions & 2 deletions models/contacts/person.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ SELECT
contact.uuid,
contact.saved_timestamp,
(couchdb.doc->>'date_of_birth')::date as date_of_birth,
couchdb.doc->>'sex' as sex
couchdb.doc->>'sex' as sex,
couchdb.doc->>'phone' AS phone,
couchdb.doc->>'alternative_phone' AS phone2,
couchdb.doc->>'patient_id' as patient_id
FROM {{ ref("contact") }} contact
INNER JOIN {{ ref('contact_type') }} contact_type ON contact_type.id = contact.contact_type
INNER JOIN {{ source('couchdb', env_var('POSTGRES_TABLE')) }} couchdb ON couchdb._id = uuid
WHERE contact.contact_type = 'person'
WHERE contact_type.person = true
{% if is_incremental() %}
AND contact.saved_timestamp >= {{ max_existing_timestamp('saved_timestamp') }}
{% endif %}
8 changes: 3 additions & 5 deletions models/contacts/place.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
SELECT
uuid,
contact.saved_timestamp,
couchdb.doc->>'contact_id' as contact_id,
couchdb.doc->>'place_id' as place_id
FROM {{ ref('contact') }} contact
INNER JOIN {{ ref('contact_type') }} contact_type ON contact_type.id = contact.contact_type
INNER JOIN {{ source('couchdb', env_var('POSTGRES_TABLE')) }} couchdb ON couchdb._id = uuid
WHERE
(
(couchdb.doc->>'place_id' IS NOT NULL) OR
(contact.contact_type <> 'person')
)
WHERE contact_type.person = false
{% if is_incremental() %}
AND contact.saved_timestamp >= {{ max_existing_timestamp('saved_timestamp') }}
{% endif %}
10 changes: 5 additions & 5 deletions test/fixtures/contact/contact_initial_expected.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
uuid,saved_timestamp,reported,parent_uuid,name,contact_type,phone,phone2,active,notes,contact_id,muted
c1,2024-08-01 00:00:00,2024-07-31 08:00:00+00,p1,John Doe,person,12345,54321,true,Note 1,C-123,false
c2,2024-08-01 00:00:00,2024-07-31 08:00:00+00,p2,Jane Doe,clinic,67890,09876,true,Note 2,C-456,true
c3,2024-08-02 00:00:00,2024-07-31 08:00:00+00,p3,Mike Smith,person,11223,33211,false,Note 3,C-789,false
c4,2024-08-02 00:00:00,2024-07-31 08:00:00+00,p4,Sara Smith,district_hospital,44556,65544,true,Note 4,C-101,true
uuid,saved_timestamp,reported,parent_uuid,name,contact_type,active,notes,muted
c1,2024-08-01 00:00:00,2024-07-31 08:00:00+00,p1,John Doe,person,true,Note 1,false
c2,2024-08-01 00:00:00,2024-07-31 08:00:00+00,p2,Jane Doe,clinic,true,Note 2,true
c3,2024-08-02 00:00:00,2024-07-31 08:00:00+00,p3,Mike Smith,person,false,Note 3,false
c4,2024-08-02 00:00:00,2024-07-31 08:00:00+00,p4,Sara Smith,district_hospital,true,Note 4,true
7 changes: 3 additions & 4 deletions test/fixtures/person/person_initial_expected.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
uuid,saved_timestamp,date_of_birth,sex
p1,2024-08-01 00:00:00,1980-01-01,M
p2,2024-08-01 00:00:00,1990-05-15,F

uuid,saved_timestamp,date_of_birth,sex,phone,phone2
p1,2024-08-01 00:00:00,1980-01-01,M,12345,54321
p2,2024-08-01 00:00:00,1990-05-15,F,67890,9876
10 changes: 5 additions & 5 deletions test/fixtures/place/place_initial_expected.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
uuid,saved_timestamp,place_id
p1,2024-08-01 00:00:00,P-001
p2,2024-08-01 00:00:00,P-002
p3,2024-08-02 00:00:00,P-003
p5,2024-08-02 00:00:00,P-004
uuid,saved_timestamp,place_id,contact_id
p1,2024-08-01 00:00:00,P-001,C-123
p2,2024-08-01 00:00:00,P-002,C-456
p3,2024-08-02 00:00:00,P-003,C-789
p5,2024-08-02 00:00:00,P-004,C-101
2 changes: 1 addition & 1 deletion test/sqltest/contact.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ WHERE
OR -- 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'
contact.name <> couchdb.doc->>'name'
)

0 comments on commit a52c859

Please sign in to comment.