r/DataBuildTool • • 25d ago

Show and tell Medallion Layer Enforcement

As dbt projects grow and more people contribute, keeping standards consistent gets harder. Someone could make a silver model (cleansed data) read from another silver model. Someone could make a gold model (business-ready data) read straight from bronze (raw data).

 

That’s why I think enforcing medallion layer dependencies matters: silver should read from bronze, and gold should read from silver.

 

This isn’t just tidiness. If a gold table skips silver and reads raw data directly, a report on billable surgical procedures could be built on data that was never cleaned. Duplicates, bad values and missing fields flow straight into the numbers people make decisions on.

 

There’s an engineering cost too. When models read from each other within a layer, two models can end up depending on each other. dbt can’t build either one due to cycle errors, and fixing it means time lost re-engineering models.

 

dbt checks (new in dbt 2.0) can enforce this. You apply it by writing a SQL query in a checks folder, plus a small YAML file for settings, that looks at every model and what it depends on and returns anything that breaks the rule. Per the dbt docs, checks are enforced “before any warehouse work runs.” If the project violates a rule, the build stops before a single model is compiled or built.

 

I tested it by pointing a gold model straight at a raw table, and a silver model at another silver model. The build failed and showed exactly which connections broke the rule (see the last image).

Rules people must remember get broken. Rules the build enforces don’t.

#dbt #AnalyticsEngineering #DataEngineering

1 Upvotes

9 comments sorted by

4

u/meatmick 25d ago

What does the code for these rules look like?

1

u/TranslatorComplex517 18d ago edited 18d ago

apols for the late response. it looks something like the below. ensure you place it inside a folder called 'checks' (same level as 'models'):

WITH nodes AS (
    SELECT 
        unique_id,
        CASE
            WHEN schema_name = '1_bronze' THEN 'bronze'
        END AS layer 
    FROM {{ info_schema('sources') }}
    
    UNION ALL


    SELECT
        unique_id,
        CASE
            WHEN schema_name = '2_silver' THEN 'silver'
            WHEN schema_name = '3_gold' THEN 'gold'
        END AS layer
    FROM {{ info_schema('models') }}
),


allowed (parent_layer, child_layer) AS (
    VALUES
        ('bronze', 'silver'),
        ('silver', 'gold')
)


SELECT
    e.parent_unique_id,
    e.child_unique_id,
    p.layer AS parent_layer,
    c.layer AS child_layer
FROM {{ info_schema('edges') }} AS e
INNER JOIN nodes AS p ON p.unique_id = e.parent_unique_id
INNER JOIN nodes AS c ON c.unique_id = e.child_unique_id
LEFT JOIN allowed AS a
    ON a.parent_layer = p.layer
    AND a.child_layer = c.layer
WHERE a.parent_layer IS NULL

4

u/mow12 25d ago

Dbt project evaluator already does that

1

u/BaxTheDestroyer 25d ago

Is the check doing anything in this instance that can’t be done with a test?

edit: Is the only difference that it runs before the warehouse work?

1

u/TranslatorComplex517 18d ago

I don't think a test can enforce medallion layer dependency rules like a check would. Also, a check can enforce other standards you set for your project e.g. ensuring model names align with your naming conventions.

1

u/pl3xi0n 14d ago

Enforcing this means you might have to repeat the same, possibly expensive, transformations many times. Or you might need maintain logic that depend on each other in two places, because you couldnt have an intermediate table for it. Or you need to add more layers than just silver and gold.

1

u/TranslatorComplex517 11d ago

Fair concern. To avoid repeating a complex/expensive transformation downstream, you can materialise it once as a view in the bronze layer. The view can then be reused downstream. The sql file in checks folder will need a small change to accommodate this.

1

u/pl3xi0n 10d ago

I agree, the only difference is that i put that intermediate view in silver. It’s just a matter of preference imo.

1

u/bigcizzle 25d ago

To deal with scale, I've found separating these into individual dbt projects and connect them via loom. That way you force a hard separation, clear contracts, and easily identify breaks in the structure (the silver project does not "import" or rely on the gold for example)