You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/concepts/models/overview.md
+5-2Lines changed: 5 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -513,9 +513,12 @@ Some properties are only available in specific model kinds - see the [model conf
513
513
514
514
Must be one of the following values: `allow`, `warn`, `error` (default), or `ignore`.
515
515
516
-
!!! warning "Ignore is Dangerous"
516
+
### on_additive_change
517
+
: What should happen when a change to a [forward-only model](../../guides/incremental_time.md#forward-only-models) or incremental model in a [forward-only plan](../plans.md#forward-only-plans) causes an additive modification to the table schema (i.e., adding new columns, modifying column data types in compatible ways, ect.).
517
518
518
-
`ignore` is dangerous since it can result in error or data loss. It likely should never be used but could be useful as an "escape-hatch" or a way to workaround unexpected behavior.
519
+
SQLMesh checks for additive changes at plan time based on the model definition and run time based on the model's underlying physical tables.
520
+
521
+
Must be one of the following values: `allow` (default), `warn`, `error`, or `ignore`.
519
522
520
523
### disable_restatement
521
524
: Set this to true to indicate that [data restatement](../plans.md#restatement-plans) is disabled for this model.
Copy file name to clipboardExpand all lines: docs/concepts/plans.md
+18-2Lines changed: 18 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -355,9 +355,25 @@ Some model changes destroy existing data in a table. SQLMesh automatically detec
355
355
356
356
Forward-only plans treats all of the plan's model changes as forward-only. In these plans, SQLMesh will check all modified incremental models for destructive schema changes, not just forward-only models.
357
357
358
-
SQLMesh determines what to dofor each model based on this setting hierarchy: the [model's `on_destructive_change` value](../guides/incremental_time.md#destructive-changes) (if present), the `on_destructive_change` [model defaults](../reference/model_configuration.md#model-defaults) value (if present), and the SQLMesh global default of `error`.
358
+
SQLMesh determines what to dofor each model based on this setting hierarchy:
359
359
360
-
If you want to temporarily allow destructive changes to models that don't allow them, use the `plan` command's `--allow-destructive-model` selector to specify which models. Learn more about model selectors [here](../guides/model_selection.md).
360
+
- **For destructive changes**: the [model's `on_destructive_change` value](../guides/incremental_time.md#schema-changes) (if present), the `on_destructive_change` [model defaults](../reference/model_configuration.md#model-defaults) value (if present), and the SQLMesh global default of `error`
361
+
- **For additive changes**: the [model's `on_additive_change` value](../guides/incremental_time.md#schema-changes) (if present), the `on_additive_change` [model defaults](../reference/model_configuration.md#model-defaults) value (if present), and the SQLMesh global default of `allow`
362
+
363
+
If you want to temporarily allow destructive changes to models that don't allow them, use the `plan` command's `--allow-destructive-model` selector to specify which models.
364
+
Similarly, if you want to temporarily allow additive changes to models configured with `on_additive_change=error`, use the `--allow-additive-model` selector.
365
+
366
+
For example, to allow destructive changes to all models in the `analytics` schema:
367
+
```bash
368
+
sqlmesh plan --forward-only --allow-destructive-model "analytics.*"
369
+
```
370
+
371
+
Or to allow destructive changes to multiple specific models:
372
+
```bash
373
+
sqlmesh plan --forward-only --allow-destructive-model "sales.revenue_model" --allow-destructive-model "marketing.campaign_model"
374
+
```
375
+
376
+
Learn more about model selectors [here](../guides/model_selection.md).
361
377
362
378
### Effective date
363
379
Changes that are part of the forward-only plan can also be applied retroactively to the production environment by specifying the effective date:
Alternatively, all the changes contained in a *specific plan* can be classified as forward-only with a flag: `sqlmesh plan --forward-only`. A subsequent plan that did not include the forward-only flag would fully refresh the model's physical table. Learn more about forward-only plans [here](../concepts/plans.md#forward-only-plans).
161
161
162
-
### Destructive changes
162
+
### Schema changes
163
163
164
-
Some model changes destroy existing data in a table. Dropping a column from the model is the most direct cause, but changing a column's data type (such as casting a column from a `STRING`to `INTEGER`) can also require a drop. (Whether or not a specific change requires dropping a column may differ across SQL engines.)
164
+
When SQLMesh processes forward-only changes to incremental models, it compares the model's new schema with the existing physical table schema to detect potential data loss or compatibility issues. SQLMesh categorizes schema changes into two types:
165
165
166
-
Forward-only models are used to retain existing data. Before executing forward-only changes to incremental models, SQLMesh performs a check to determine if existing data will be destroyed.
166
+
#### Destructive changes
167
167
168
-
The check is performed at plan time based on the model definition. SQLMesh may not be able to resolve all of a model's column data types and complete the check, so the check is performed again at run time based on the physical tables underlying the model.
168
+
Some model changes destroy existing data in a table. Examples include:
169
+
170
+
-**Dropping a column** from the model
171
+
-**Renaming a column**
172
+
-**Modifying a column data type** in a ways that could cause data loss
173
+
174
+
Whether a specific change is destructive may differ across SQL engines based on their schema evolution capabilities.
175
+
176
+
#### Additive changes
177
+
178
+
Additive changes are any changes to the table's columns that aren't categorized as destructive. A simple example would be adding a column to a table but another would be changing a column data type to a type that is compatible (ex: INT -> STRING).
179
+
180
+
SQLMesh performs schema change detection at plan time based on the model definition. If SQLMesh cannot resolve all of a model's column data types at plan time, the check is performed again at run time based on the physical tables underlying the model.
169
181
170
182
#### Changes to forward-only models
171
183
172
-
A model's `on_destructive_change`[configuration setting](../reference/model_configuration.md#incremental-models) determines what happens when SQLMesh detects a destructive change.
184
+
SQLMesh provides two configuration settings to control how schema changes are handled:
185
+
186
+
-**`on_destructive_change`** - Controls behavior for destructive schema changes
187
+
-**`on_additive_change`** - Controls behavior for additive schema changes
188
+
189
+
##### Configuration options
190
+
191
+
Both properties support four values:
173
192
174
-
By default, SQLMesh will error so no data is lost. You can set `on_destructive_change` to `warn` or `allow` in the model's `MODEL` block to allow destructive changes.
175
-
`ignore` can be used to not perform the schema change and allow the table's definition to diverge from the model definition.
193
+
-**`error`** (default for `on_destructive_change`): Stop execution and raise an error
194
+
-**`warn`**: Log a warning but proceed with the change
195
+
-**`allow`** (default for `on_additive_change`): Silently proceed with the change
196
+
-**`ignore`**: Skip the schema change check entirely for this change type
176
197
177
198
!!! warning "Ignore is Dangerous"
178
199
179
-
`ignore` is dangerous since it can result in error or data loss. It likely should never be used but could be useful as an "escape-hatch" or a way to workaround unexpected behavior.
200
+
`ignore` is dangerous since it can result in error or data loss. It likely should never be used but could be useful as an "escape-hatch" or a way to workaround unexpected behavior.
201
+
202
+
##### Destructive change handling
203
+
204
+
The `on_destructive_change`[configuration setting](../reference/model_configuration.md#incremental-models) determines what happens when SQLMesh detects a destructive change. By default, SQLMesh will error so no data is lost.
180
205
181
206
This example configures a model to silently `allow` destructive changes:
182
207
@@ -191,12 +216,93 @@ MODEL (
191
216
);
192
217
```
193
218
194
-
A default `on_destructive_change` value can be set for all incremental models that do not specify it themselves in the [model defaults configuration](../reference/model_configuration.md#model-defaults).
219
+
##### Additive change handling
220
+
221
+
The `on_additive_change` configuration setting determines what happens when SQLMesh detects an additive change like adding new columns. By default, SQLMesh allows these changes since they don't destroy existing data.
222
+
223
+
This example configures a model to raise an error for additive changes (useful for strict schema control):
224
+
225
+
```sql linenums="1"
226
+
MODEL (
227
+
name sqlmesh_example.new_model,
228
+
kind INCREMENTAL_BY_TIME_RANGE (
229
+
time_column model_time_column,
230
+
forward_only true,
231
+
on_additive_change error
232
+
),
233
+
);
234
+
```
235
+
236
+
##### Combining both settings
237
+
238
+
You can configure both settings together to have fine-grained control over schema evolution:
239
+
240
+
```sql linenums="1"
241
+
MODEL (
242
+
name sqlmesh_example.new_model,
243
+
kind INCREMENTAL_BY_TIME_RANGE (
244
+
time_column model_time_column,
245
+
forward_only true,
246
+
on_destructive_change warn, -- Warn but allow destructive changes
247
+
on_additive_change allow -- Silently allow new columns
248
+
),
249
+
);
250
+
```
251
+
252
+
##### Model defaults
253
+
254
+
Default values for both `on_destructive_change` and `on_additive_change` can be set for all incremental models in the [model defaults configuration](../reference/model_configuration.md#model-defaults).
255
+
256
+
##### Common use cases
257
+
258
+
Here are some common patterns for configuring schema change handling:
259
+
260
+
**Strict schema control** - Prevent any schema changes:
on_additive_change allow -- Allow new columns (`allow` is the default value for this setting, so it can be omitted here)
282
+
),
283
+
);
284
+
```
285
+
286
+
**Production safety** - Allow safe changes, warn about risky ones:
287
+
```sql linenums="1"
288
+
MODEL (
289
+
name sqlmesh_example.production_model,
290
+
kind INCREMENTAL_BY_TIME_RANGE (
291
+
time_column event_date,
292
+
forward_only true,
293
+
on_destructive_change warn, -- Warn about destructive changes
294
+
on_additive_change allow -- Allow new columns (`allow` is the default value for this setting, so it can be omitted here)
295
+
),
296
+
);
297
+
```
195
298
196
299
#### Changes in forward-only plans
197
300
198
-
The SQLMesh `plan`[`--forward-only` option](../concepts/plans.md#forward-only-plans) treats all the plan's model changes as forward-only. When this option is specified, SQLMesh will check all modified incremental models for destructive schema changes, not just models configured with `forward_only true`.
301
+
The SQLMesh `plan`[`--forward-only` option](../concepts/plans.md#forward-only-plans) treats all the plan's model changes as forward-only. When this option is specified, SQLMesh will check all modified incremental models for both destructive and additive schema changes, not just models configured with `forward_only true`.
302
+
303
+
SQLMesh determines what to do for each model based on this setting hierarchy:
199
304
200
-
SQLMesh determines what to do for each model based on this setting hierarchy: the model's `on_destructive_change` value (if present), the `on_destructive_change`[model defaults](../reference/model_configuration.md#model-defaults) value (if present), and the SQLMesh global default of `error`.
305
+
-**For destructive changes**: the model's `on_destructive_change` value (if present), the `on_destructive_change`[model defaults](../reference/model_configuration.md#model-defaults) value (if present), and the SQLMesh global default of `error`
306
+
-**For additive changes**: the model's `on_additive_change` value (if present), the `on_additive_change`[model defaults](../reference/model_configuration.md#model-defaults) value (if present), and the SQLMesh global default of `allow`
201
307
202
-
If you want to temporarily allow destructive changes to models that don't allow them, use the `plan` command's [`--allow-destructive-model` selector](../concepts/plans.md#destructive-changes) to specify which models. Learn more about model selectors [here](../guides/model_selection.md).
308
+
If you want to temporarily allow destructive changes to models that don't allow them, use the `plan` command's [`--allow-destructive-model` selector](../concepts/plans.md#destructive-changes) to specify which models. Similarly, if you want to temporarily allow additive changes to models configured with `on_additive_change=error`, use the [`--allow-additive-model` selector](../concepts/plans.md#destructive-changes). Learn more about model selectors [here](../guides/model_selection.md).
Copy file name to clipboardExpand all lines: docs/guides/model_selection.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
This guide describes how to select specific models to include in a SQLMesh plan, which can be useful when modifying a subset of the models in a SQLMesh project.
4
4
5
-
Note: the selector syntax described below is also used for the SQLMesh `plan`[`--allow-destructive-model`selector](../concepts/plans.md#destructive-changes) and for the `table_diff` command to [diff a selection of models](./tablediff.md#diffing-multiple-models-across-environments).
5
+
Note: the selector syntax described below is also used for the SQLMesh `plan`[`--allow-destructive-model`and `--allow-additive-model` selectors](../concepts/plans.md#destructive-changes) and for the `table_diff` command to [diff a selection of models](./tablediff.md#diffing-multiple-models-across-environments).
Copy file name to clipboardExpand all lines: docs/integrations/dbt.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -273,18 +273,18 @@ Similarly, the [allow_partials](../concepts/models/overview.md#allow_partials) p
273
273
274
274
#### on_schema_change
275
275
276
-
SQLMesh automatically detects destructive schema changes to [forward-only incremental models](../guides/incremental_time.md#forward-only-models) and to all incremental models in [forward-only plans](../concepts/plans.md#destructive-changes).
276
+
SQLMesh automatically detects both destructive and additive schema changes to [forward-only incremental models](../guides/incremental_time.md#forward-only-models) and to all incremental models in [forward-only plans](../concepts/plans.md#destructive-changes).
277
277
278
-
A model's [`on_destructive_change` setting](../guides/incremental_time.md#destructive-changes) determines whether it errors (default), warns, or silently allowsthe changes. SQLMesh always allows non-destructive forward-only schema changes, such as adding or casting a column in place.
278
+
A model's [`on_destructive_change` and `on_additive_change` settings](../guides/incremental_time.md#schema-changes) determine whether it errors, warns, silently allows, or ignores the changes. SQLMesh provides fine-grained control over both destructive changes (like dropping columns) and additive changes (like adding new columns).
279
279
280
-
`on_schema_change` configuration values are mapped to these SQLMesh `on_destructive_change` values:
280
+
`on_schema_change` configuration values are mapped to these SQLMesh settings:
0 commit comments