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/model_kinds.md
+40-8Lines changed: 40 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -935,7 +935,13 @@ SQLMesh achieves this by adding a `valid_from` and `valid_to` column to your mod
935
935
936
936
Therefore, you can use these models to not only tell you what the latest value is for a given record but also what the values were anytime in the past. Note that maintaining this history does come at a cost of increased storage and compute and this may not be a good fit for sources that change frequently since the history could get very large.
937
937
938
-
**Note**: Partial data [restatement](../plans.md#restatement-plans) is not supported for this model kind, which means that the entire table will be recreated from scratch if restated. This may lead to data loss, so data restatement is disabled for models of this kind by default.
938
+
**Note**: SCD Type 2 models support [restatements](../plans.md#restatement-plans) with specific limitations:
939
+
940
+
- **Full restatements**: The entire table will be recreated from scratch when no start date is specified
941
+
- **Partial restatements**: You can specify a start date to restate data from a certain point onwards to the latest interval. The end date will always be set to the latest interval's end date, regardless of what end date you specify
942
+
- **Partial sections**: Restatements of specific sections (discontinued ranges) of the table are not supported
943
+
944
+
Data restatement is disabled for models of this kind by default (`disable_restatement true`). To enable restatements, set `disable_restatement false` in your model configuration.
939
945
940
946
There are two ways to tracking changes: By Time (Recommended) or By Column.
941
947
@@ -1283,11 +1289,11 @@ This is the most accurate representation of the menu based on the source data pr
1283
1289
1284
1290
### Processing Source Table with Historical Data
1285
1291
1286
-
The most common case for SCD Type 2 is creating history for a table that it doesn't have it already.
1292
+
The most common case for SCD Type 2 is creating history for a table that it doesn't have it already.
1287
1293
In the example of the restaurant menu, the menu just tells you what is offered right now, but you want to know what was offered over time.
1288
1294
In this case, the default setting of `None` for `batch_size` is the best option.
1289
1295
1290
-
Another use case though is processing a source table that already has history in it.
1296
+
Another use case though is processing a source table that already has history in it.
1291
1297
A common example of this is a "daily snapshot" table that is created by a source system that takes a snapshot of the data at the end of each day.
1292
1298
If your source table has historical records, like a "daily snapshot" table, then set `batch_size` to `1` to process each interval (each day if a `@daily` cron) in sequential order.
1293
1299
That way the historical records will be properly captured in the SCD Type 2 table.
@@ -1433,11 +1439,14 @@ GROUP BY
1433
1439
id
1434
1440
```
1435
1441
1436
-
### Reset SCD Type 2 Model (clearing history)
1442
+
### SCD Type 2 Restatements
1437
1443
1438
1444
SCD Type 2 models are designed by default to protect the data that has been captured because it is not possible to recreate the history once it has been lost.
1439
1445
However, there are cases where you may want to clear the history and start fresh.
1440
-
For this use use case you will want to start by setting `disable_restatement` to `false` in the model definition.
1446
+
1447
+
#### Enabling Restatements
1448
+
1449
+
To enable restatements for an SCD Type 2 model, set `disable_restatement` to `false` in the model definition:
1441
1450
1442
1451
```sql linenums="1" hl_lines="5"
1443
1452
MODEL (
@@ -1449,16 +1458,39 @@ MODEL (
1449
1458
);
1450
1459
```
1451
1460
1452
-
Plan/apply this change to production.
1453
-
Then you will want to [restate the model](../plans.md#restatement-plans).
1461
+
#### Full Restatements (Clearing All History)
1462
+
1463
+
To clear all history and recreate the entire table from scratch:
1454
1464
1455
1465
```bash
1456
1466
sqlmesh plan --restate-model db.menu_items
1457
1467
```
1458
1468
1459
1469
!!! warning
1460
1470
1461
-
This will remove the historical data on the model which in most situations cannot be recovered.
1471
+
This will remove **all** historical data on the model which in most situations cannot be recovered.
1472
+
1473
+
#### Partial Restatements (From a Specific Date)
1474
+
1475
+
You can restate data from a specific start date onwards. This will:
1476
+
- Delete all records with `valid_from >= start_date`
1477
+
- Reprocess the data from the start date to the latest interval
1478
+
1479
+
```bash
1480
+
sqlmesh plan --restate-model db.menu_items --start "2023-01-15"
1481
+
```
1482
+
1483
+
!!! note
1484
+
1485
+
If you specify an end date for SCD Type 2 restatements, it will be ignored and automatically set to the latest interval's end date.
1486
+
1487
+
```bash
1488
+
# This end date will be ignored and set to the latest interval
1489
+
sqlmesh plan --restate-model db.menu_items --start "2023-01-15" --end "2023-01-20"
1490
+
```
1491
+
1492
+
1493
+
#### Re-enabling Protection
1462
1494
1463
1495
Once complete you will want to remove `disable_restatement` on the model definition which will set it back to `true` and prevent accidental data loss.
0 commit comments