Slim CI helps run only the parts of your data project that changed. It saves time and computing power by comparing the current state with the last run.
Slim CI with state comparison in dbt
dbt build --state path/to/previous/run --select state:modified
--state points to the folder with the previous run's manifest and run results.
--select state:modified runs only models that changed since the last run.
./target/previous_run.dbt build --state ./target/previous_run --select state:modified
dbt test --state ./target/previous_run --select state:modified
dbt run --state ./target/previous_run --select state:modified+
This Python script runs a dbt build command that uses slim CI with state comparison. It runs only the models that changed since the last run saved in ./target/previous_run. The output shows which models were run.
import os import subprocess # Assume previous run artifacts are saved in './target/previous_run' previous_state_path = './target/previous_run' # Command to run only modified models compared to previous run command = [ 'dbt', 'build', '--state', previous_state_path, '--select', 'state:modified' ] # Run the command and capture output result = subprocess.run(command, capture_output=True, text=True) print(result.stdout)
Make sure to save the previous run's artifacts (manifest.json, run_results.json) in the folder you point to with --state.
Slim CI only works if you have a previous run to compare with.
You can combine state:modified with other selectors like + to include dependencies.
Slim CI runs only changed models by comparing current and previous states.
Use --state to point to previous run artifacts and --select state:modified to select changed models.
This saves time and resources during development and testing.
Practice
--state and --select state:modified in dbt's Slim CI?Solution
Step 1: Understand the role of
The--state--stateflag points dbt to the previous run's artifacts, allowing comparison between runs.Step 2: Understand
This selects only the models that have changed compared to the previous state, skipping unchanged models.--select state:modifiedFinal Answer:
To run only the models that have changed since the last run -> Option DQuick Check:
Slim CI runs changed models only [OK]
- Thinking it runs all models every time
- Confusing state with backup or deletion
- Assuming it runs unchanged models too
Solution
Step 1: Identify correct flag usage
The correct flags are--stateto specify previous run artifacts and--select state:modifiedto select changed models.Step 2: Match syntax to flags
dbt run --select state:modified --state path/to/previous correctly uses--select state:modifiedand--state path/to/previousin proper order and format.Final Answer:
dbt run --select state:modified --state path/to/previous -> Option BQuick Check:
Correct flags and order [OK]
- Mixing up flag names or order
- Using incorrect flag syntax
- Selecting all models instead of modified
dbt run --state ./target/run1 --select state:modified # After modifying 'sales' model dbt run --state ./target/run1 --select state:modified
What will the second command do?
Solution
Step 1: Understand the first run
The first command runs all models and saves state in./target/run1.Step 2: Analyze the second run with modification
The second command compares current state to./target/run1and runs only models that changed, here 'sales'.Final Answer:
Run only the 'sales' model because it was modified -> Option AQuick Check:
Modified model runs only [OK]
- Assuming all models run again
- Thinking no models run if only one changed
- Confusing state path usage
dbt run --state ./previous_run --select state:modified but get an error saying the state path is invalid. What is the most likely cause?Solution
Step 1: Check the meaning of the error
An invalid state path error means dbt cannot find or read previous run artifacts at the given path.Step 2: Evaluate other options
Flags order or missing '--select all' do not cause this error; '--full-refresh' is unrelated.Final Answer:
The path './previous_run' does not contain valid dbt run artifacts -> Option AQuick Check:
Invalid state path means missing artifacts [OK]
- Assuming flag order causes error
- Thinking '--full-refresh' fixes state errors
- Forgetting to generate previous run artifacts
./target/previous. You have models 'A', 'B', and 'C'. Only 'B' was modified. Which command will run only the modified model and also run its dependent models?Solution
Step 1: Understand the '+' operator in selection
The '+' afterstate:modifiedselects modified models plus their dependents.Step 2: Match command to requirement
dbt run --state ./target/previous --select state:modified+ usesstate:modified+with correct--statepath, running 'B' and its dependents.Step 3: Evaluate other options
dbt run --state ./target/previous --select state:modified runs only modified models without dependents; C adds unnecessary full-refresh; D excludes 'B' incorrectly.Final Answer:
dbt run --state ./target/previous --select state:modified+ -> Option CQuick Check:
Modified plus dependents run [OK]
- Not including dependents with '+'
- Using --full-refresh unnecessarily
- Excluding modified models by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
