Bird
Raised Fist0
dbtdata~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a model contract in dbt?
easy
A. To schedule when models run in dbt Cloud
B. To define rules that ensure data quality in models
C. To control who can access the dbt project files
D. To create visual dashboards from model data

Solution

  1. Step 1: Understand what a model contract does

    A model contract sets rules that data must follow to be considered valid in dbt models.
  2. Step 2: Compare options to the definition

    Only To define rules that ensure data quality in models describes defining rules for data quality, which matches the purpose of model contracts.
  3. Final Answer:

    To define rules that ensure data quality in models -> Option B
  4. Quick Check:

    Model contract = data quality rules [OK]
Hint: Model contracts = rules for data quality [OK]
Common Mistakes:
  • Confusing access control with model contracts
  • Thinking contracts schedule runs
  • Mixing contracts with visualization tools
2. Which of the following is the correct syntax to define a model contract in a dbt model's schema.yml file?
easy
A. models: - name: my_model access_control: users: - admin
B. models: - name: my_model schedule: cron: '0 0 * * *'
C. models: - name: my_model contract: columns: - name: id tests: - not_null
D. models: - name: my_model dashboard: enabled: true

Solution

  1. Step 1: Identify the syntax for model contracts in schema.yml

    Model contracts are defined under the contract key with column tests inside schema.yml.
  2. Step 2: Match options to this syntax

    models: - name: my_model contract: columns: - name: id tests: - not_null correctly shows contract with columns and tests. Others show unrelated keys like access_control, schedule, or dashboard.
  3. Final Answer:

    models: - name: my_model contract: columns: - name: id tests: - not_null -> Option C
  4. Quick Check:

    Contract syntax = models: - name: my_model contract: columns: - name: id tests: - not_null [OK]
Hint: Look for 'contract' key with column tests in schema.yml [OK]
Common Mistakes:
  • Confusing access control syntax with contract syntax
  • Using schedule or dashboard keys incorrectly
  • Missing indentation in YAML
3. Given this dbt model contract snippet in schema.yml:
models:
  - name: sales
    contract:
      columns:
        - name: order_id
          tests:
            - not_null
            - unique
        - name: amount
          tests:
            - not_null
What happens if the order_id column contains duplicate values when the model runs?
medium
A. The model run is skipped automatically
B. The model runs successfully but logs a warning
C. The model ignores the duplicates and completes without error
D. The model run fails because the contract test for uniqueness fails

Solution

  1. Step 1: Understand contract tests behavior

    Contract tests like unique enforce data rules strictly. If violated, the model run fails.
  2. Step 2: Apply to duplicate order_id values

    Since order_id must be unique, duplicates cause the test to fail, stopping the model run.
  3. Final Answer:

    The model run fails because the contract test for uniqueness fails -> Option D
  4. Quick Check:

    Contract test fail = model run fails [OK]
Hint: Contract tests fail stop model runs immediately [OK]
Common Mistakes:
  • Thinking model logs warning but continues
  • Assuming duplicates are ignored
  • Believing model run is skipped automatically
4. You wrote this access control in your dbt_project.yml:
access_control:
  - role: analyst
    models:
      - sales
      - customers
  - role: viewer
    models:
      - sales
But analysts report they cannot see the customers model. What is the likely error?
medium
A. The access_control block is not supported in dbt_project.yml
B. The role name 'analyst' is misspelled
C. The models list must be under each model, not role
D. The customers model does not exist in the project

Solution

  1. Step 1: Check where access controls are defined in dbt

    dbt does not support an access_control block in dbt_project.yml. Access controls are managed outside or via external tools.
  2. Step 2: Analyze other options

    Role spelling or model existence might cause issues, but the main problem is unsupported syntax in the config file.
  3. Final Answer:

    The access_control block is not supported in dbt_project.yml -> Option A
  4. Quick Check:

    Access control block unsupported = The access_control block is not supported in dbt_project.yml [OK]
Hint: Access controls not set in dbt_project.yml [OK]
Common Mistakes:
  • Trying to set access control inside dbt_project.yml
  • Assuming role names cause access issues
  • Not verifying model existence
5. You want to enforce that only users with the 'data_engineer' role can modify the finance model, and that the model must always have a non-null transaction_id. How can you combine model contracts and access controls to achieve this?
hard
A. Define a model contract in schema.yml for transaction_id not null, and manage access control via your data platform's permissions for 'data_engineer' role
B. Add an access control block in schema.yml restricting 'finance' model to 'data_engineer', and add a contract test for transaction_id
C. Use a dbt macro to check user role inside the model SQL and raise error if not 'data_engineer', plus contract test for transaction_id
D. Set transaction_id as primary key in the database and assign 'data_engineer' role ownership on the model

Solution

  1. Step 1: Use model contracts for data quality

    Define a contract test in schema.yml to ensure transaction_id is not null, enforcing data quality.
  2. Step 2: Manage access control outside dbt

    dbt does not handle user permissions directly; access control is managed by your data platform or warehouse permissions for the 'data_engineer' role.
  3. Final Answer:

    Define a model contract in schema.yml for transaction_id not null, and manage access control via your data platform's permissions for 'data_engineer' role -> Option A
  4. Quick Check:

    Contracts for data rules + external access control = Define a model contract in schema.yml for transaction_id not null, and manage access control via your data platform's permissions for 'data_engineer' role [OK]
Hint: Use contracts for data, platform for access control [OK]
Common Mistakes:
  • Trying to set access control inside dbt schema.yml
  • Using macros to check user roles inside SQL
  • Assuming dbt manages user permissions directly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard