0
0
dbtdata~10 mins

Model contracts and access controls in dbt - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Model contracts and access controls
Define Model Contract
Specify Input Schema
Specify Output Schema
Implement Model Logic
Apply Access Controls
Run dbt Model
Validate Contract Compliance
Grant or Deny Data Access
This flow shows how a dbt model contract is defined with input/output schemas, then access controls are applied before running and validating the model.
Execution Sample
dbt
version: 2
models:
  - name: sales_summary
    columns:
      - name: total_sales
        tests:
          - not_null
          - accepted_values:
              values: [0, 1000000]
This dbt schema.yml snippet defines a model contract for 'sales_summary' with column tests to enforce data quality.
Execution Table
StepActionInput Schema CheckOutput Schema CheckAccess Control CheckResult
1Start dbt runN/AN/AN/ARunning model
2Check input schema matches contractPassN/AN/AInput schema valid
3Run model SQL logicN/AN/AN/AModel executed
4Check output schema matches contractN/APassN/AOutput schema valid
5Apply access controlsN/AN/AUser has permissionAccess granted
6Complete runN/AN/AN/AModel run successful
7If access deniedN/AN/AUser lacks permissionAccess denied - stop
💡 Execution stops if access control check fails or after successful model run and validation
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
input_schema_validundefinedTrueTrueTrueTrue
output_schema_validundefinedundefinedTrueTrueTrue
user_accessundefinedundefinedundefinedTrue or FalseTrue or False
model_run_statusNot startedNot startedRunningRunningSuccess or Denied
Key Moments - 3 Insights
Why does the model run stop if access control fails?
Because the execution_table row 7 shows that if the user lacks permission, access is denied and the process stops to protect data.
What happens if the output schema does not match the contract?
The output schema check in row 4 would fail, causing the model run to fail or raise an error to ensure data quality.
How do input and output schema checks help in model contracts?
They ensure the data going in and coming out matches expected formats, preventing errors and maintaining trust in data pipelines as shown in rows 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result after step 4?
AOutput schema valid
BAccess denied
CInput schema invalid
DModel not started
💡 Hint
Check the 'Result' column in row 4 of the execution_table
At which step does the access control check happen?
AStep 2
BStep 5
CStep 4
DStep 7
💡 Hint
Look at the 'Action' column for access control in the execution_table
If the user lacks permission, what happens to model_run_status in variable_tracker?
AChanges to 'Success'
BRemains 'Running'
CChanges to 'Denied'
DChanges to 'Not started'
💡 Hint
Refer to 'model_run_status' variable in variable_tracker after step 5
Concept Snapshot
Model contracts in dbt define expected input/output schemas.
Access controls restrict who can run or see model data.
Schema checks validate data shape before and after model runs.
Access denied stops model execution to protect data.
Contracts ensure data quality and trust in pipelines.
Full Transcript
In dbt, model contracts define the expected input and output data shapes using schema files. When running a model, dbt first checks if the input data matches the contract. Then it runs the model's SQL logic. After that, it checks if the output data matches the expected schema. Access controls are applied to ensure only authorized users can run or see the model data. If access is denied, the model run stops immediately to protect data. This process helps maintain data quality and security in data pipelines.