|
1 | 1 | # dbt-snowflake-streams-tasks |
2 | | -A DBT Snowflake Plugin that enables users to create streams and tasks in DBT to build better versions of incremental models that don't need to be scheduled. |
| 2 | + |
| 3 | +A dbt package that enables creating Snowflake [streams](https://docs.snowflake.com/en/sql-reference/sql/create-stream) and [tasks](https://docs.snowflake.com/en/sql-reference/sql/create-task) through dbt, providing an alternative to incremental models that leverages Snowflake's native change data capture. |
| 4 | + |
| 5 | +## How It Works |
| 6 | + |
| 7 | +Instead of scheduling dbt runs to process incremental data, this package creates: |
| 8 | + |
| 9 | +1. **A target table** — with the schema defined by your SQL |
| 10 | +2. **A Snowflake stream** — to capture changes on your source table/view |
| 11 | +3. **A Snowflake task** — with a MERGE statement that automatically processes stream data into your target table |
| 12 | + |
| 13 | +Once deployed, the stream and task run autonomously in Snowflake — no dbt scheduling required. |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +Add to your `packages.yml`: |
| 18 | + |
| 19 | +```yaml |
| 20 | +packages: |
| 21 | + - git: "https://github.com/<your-org>/dbt-snowflake-streams-tasks.git" |
| 22 | + revision: v0.0.1 |
| 23 | +``` |
| 24 | +
|
| 25 | +## Usage |
| 26 | +
|
| 27 | +### 1. Write Your Model SQL |
| 28 | +
|
| 29 | +Use `stream_ref()` or `stream_source()` to specify the stream source: |
| 30 | + |
| 31 | +```sql |
| 32 | +-- models/my_model.sql |
| 33 | +select |
| 34 | + id, |
| 35 | + name, |
| 36 | + email, |
| 37 | + created_at |
| 38 | +from {{ dbt_streams_tasks.stream_ref('upstream_model') }} |
| 39 | +``` |
| 40 | + |
| 41 | +Or from a source: |
| 42 | + |
| 43 | +```sql |
| 44 | +-- models/my_model.sql |
| 45 | +select * |
| 46 | +from {{ dbt_streams_tasks.stream_source('my_source', 'my_table') }} |
| 47 | +``` |
| 48 | + |
| 49 | +### 2. Configure the Model |
| 50 | + |
| 51 | +In your YAML file: |
| 52 | + |
| 53 | +```yaml |
| 54 | +# models/_models.yml |
| 55 | +version: 2 |
| 56 | +
|
| 57 | +models: |
| 58 | + - name: my_model |
| 59 | + config: |
| 60 | + materialized: stream_task |
| 61 | + unique_key: |
| 62 | + - id |
| 63 | + stream: |
| 64 | + name: "my_stream" |
| 65 | + description: "Captures changes from the source" |
| 66 | + append_only: true |
| 67 | + show_initial_rows: true |
| 68 | + copy_grants: false |
| 69 | + task: |
| 70 | + name: "my_task" |
| 71 | + description: "Merges stream data into target" |
| 72 | + warehouse: "MY_WAREHOUSE" |
| 73 | + schedule: "1 MINUTE" |
| 74 | + when: "SYSTEM$STREAM_HAS_DATA('my_stream')" |
| 75 | +``` |
| 76 | + |
| 77 | +### 3. Run dbt |
| 78 | + |
| 79 | +```bash |
| 80 | +dbt run # Creates table, stream, and task |
| 81 | +dbt run --full-refresh # Recreates everything from scratch |
| 82 | +``` |
| 83 | + |
| 84 | +## Configuration Reference |
| 85 | + |
| 86 | +### Stream Config |
| 87 | + |
| 88 | +| Parameter | Required | Default | Description | |
| 89 | +|-----------|----------|---------|-------------| |
| 90 | +| `name` | Yes | - | Name of the Snowflake stream | |
| 91 | +| `description` | No | `''` | Comment on the stream | |
| 92 | +| `append_only` | No | `true` | Only track inserts (not updates/deletes) | |
| 93 | +| `show_initial_rows` | No | `true` | Include existing rows on stream creation | |
| 94 | +| `copy_grants` | No | `false` | Copy grants when replacing the stream | |
| 95 | + |
| 96 | +### Task Config |
| 97 | + |
| 98 | +| Parameter | Required | Default | Description | |
| 99 | +|-----------|----------|---------|-------------| |
| 100 | +| `name` | Yes | - | Name of the Snowflake task | |
| 101 | +| `description` | No | `''` | Comment on the task | |
| 102 | +| `warehouse` | No* | - | Warehouse to execute the task (*required if not serverless) | |
| 103 | +| `schedule` | No | - | How often to run (e.g., `'1 MINUTE'`) | |
| 104 | +| `when` | No | - | Condition to check before running | |
| 105 | +| `allow_overlapping_execution` | No | - | Allow concurrent executions | |
| 106 | +| `user_task_timeout_ms` | No | - | Task timeout in milliseconds | |
| 107 | +| `suspend_task_after_num_failures` | No | - | Auto-suspend after N failures | |
| 108 | +| `task_auto_retry_attempts` | No | - | Number of auto-retry attempts | |
| 109 | + |
| 110 | +## Running Integration Tests |
| 111 | + |
| 112 | +```bash |
| 113 | +# Set up environment variables |
| 114 | +cp integration_tests/test.env.sample integration_tests/test.env |
| 115 | +cp integration_tests/vars.env.sample integration_tests/vars.env |
| 116 | +# Edit both files with your Snowflake credentials |
| 117 | +
|
| 118 | +# Run tests |
| 119 | +./run_test.sh |
| 120 | +``` |
| 121 | + |
| 122 | +## Requirements |
| 123 | + |
| 124 | +- dbt-core >= 1.9.1 |
| 125 | +- dbt-snowflake >= 1.9.0 |
| 126 | +- Snowflake account with CHANGE_TRACKING enabled on source tables |
0 commit comments