From 4515d7d7ce260062598ba8ba17f8438af023d5d4 Mon Sep 17 00:00:00 2001 From: Njuguna Ndung'u Date: Thu, 12 Sep 2024 09:59:46 +0300 Subject: [PATCH] fix: handle empty strings, bull values, and incorrect dates for date_of_birth column --- models/contacts/person.sql | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/models/contacts/person.sql b/models/contacts/person.sql index 88315eda..40b1449c 100644 --- a/models/contacts/person.sql +++ b/models/contacts/person.sql @@ -13,7 +13,12 @@ SELECT contact.uuid, contact.saved_timestamp, - (couchdb.doc->>'date_of_birth')::date as date_of_birth, + CASE + WHEN couchdb.doc->>'date_of_birth' IS NULL OR couchdb.doc->>'date_of_birth' = '' THEN NULL + WHEN trim(couchdb.doc->>'date_of_birth') = '' THEN NULL + WHEN (couchdb.doc->>'date_of_birth') ~ '^\d{4}-\d{2}-\d{2}$' THEN (couchdb.doc->>'date_of_birth')::date + ELSE NULL + END as date_of_birth, couchdb.doc->>'sex' as sex FROM {{ ref("contact") }} contact INNER JOIN {{ source('couchdb', env_var('POSTGRES_TABLE')) }} couchdb ON couchdb._id = uuid