From 8ce34b3ecc99ff9b33c9421d5197faf0224ccc4b Mon Sep 17 00:00:00 2001 From: Tudor Sitaru Date: Mon, 13 Apr 2026 14:51:14 +0100 Subject: [PATCH] fix(list): read ofsted grade from fact_ofsted_inspection directly, fix dim_school schema lookup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit dim_school.sql was checking for int_ofsted_latest in target.schema (wrong schema) due to the custom generate_schema_name macro using literal schema names. The model lives in 'intermediate', so ofsted_grade/date/framework were always NULL in dim_school, causing all list cards to show 'Not yet inspected'. Fix 1: data_loader.py joins marts.fact_ofsted_inspection with DISTINCT ON to get latest inspection per school — no pipeline re-run needed. Fix 2: dim_school.sql uses schema='intermediate' so future dbt runs correctly denormalise the Ofsted summary into dim_school. --- backend/data_loader.py | 15 ++++++++++++--- pipeline/transform/models/marts/dim_school.sql | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/backend/data_loader.py b/backend/data_loader.py index 7ce4941..efc827e 100644 --- a/backend/data_loader.py +++ b/backend/data_loader.py @@ -130,9 +130,9 @@ _MAIN_QUERY = text(""" s.total_pupils AS gias_total_pupils, s.headteacher_name, s.website, - s.ofsted_grade, - s.ofsted_date, - s.ofsted_framework, + foi.ofsted_grade, + foi.ofsted_date, + foi.ofsted_framework, l.local_authority_name AS local_authority, l.local_authority_code, l.address_line1 AS address1, @@ -201,6 +201,15 @@ _MAIN_QUERY = text(""" FROM marts.dim_school s JOIN marts.dim_location l ON s.urn = l.urn LEFT JOIN marts.fact_performance p ON s.urn = p.urn + LEFT JOIN ( + SELECT DISTINCT ON (urn) + urn, + overall_effectiveness AS ofsted_grade, + inspection_date AS ofsted_date, + framework AS ofsted_framework + FROM marts.fact_ofsted_inspection + ORDER BY urn, inspection_date DESC NULLS LAST + ) foi ON s.urn = foi.urn ORDER BY s.school_name, p.year """) diff --git a/pipeline/transform/models/marts/dim_school.sql b/pipeline/transform/models/marts/dim_school.sql index 20443dc..b423cea 100644 --- a/pipeline/transform/models/marts/dim_school.sql +++ b/pipeline/transform/models/marts/dim_school.sql @@ -6,7 +6,7 @@ with schools as ( {% set ofsted_relation = adapter.get_relation( database=target.database, - schema=target.schema, + schema='intermediate', identifier='int_ofsted_latest' ) %}