Skip to content

Commit 713744a

Browse files
committed
Add documentation
1 parent 3b75e3c commit 713744a

2 files changed

Lines changed: 110 additions & 1 deletion

File tree

docs/guides/configuration.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,115 @@ Example showing default values:
381381
)
382382
```
383383

384+
385+
### Always comparing against production
386+
387+
By default, SQLMesh compares the current state of project files to the target `<env>` environment when `sqlmesh plan <env>` is run. However, a common expectation is that local changes should always be compared to the production environment.
388+
389+
The `always_compare_against_prod` boolean plan option can alter this behavior. When enabled, SQLMesh will always attempt to compare against the production environment; If that does not exist, SQLMesh will fall back to comparing against the target environment.
390+
391+
**NOTE:**: Upon succesfull plan application, changes are still promoted to the target `<env>` environment.
392+
393+
=== "YAML"
394+
395+
```yaml linenums="1"
396+
plan:
397+
always_compare_against_prod: True
398+
```
399+
400+
=== "Python"
401+
402+
```python linenums="1"
403+
from sqlmesh.core.config import (
404+
Config,
405+
ModelDefaultsConfig,
406+
PlanConfig,
407+
)
408+
409+
config = Config(
410+
model_defaults=ModelDefaultsConfig(dialect=<dialect>),
411+
plan=PlanConfig(
412+
always_compare_against_prod=True,
413+
),
414+
)
415+
```
416+
417+
#### Change Categorization Example
418+
419+
Consider this scenario with `always_compare_against_prod` enabled:
420+
421+
1. Initial state in `prod`:
422+
```sql
423+
MODEL (name sqlmesh_example.test_model, kind FULL);
424+
SELECT 1 AS col
425+
```
426+
427+
1. First (breaking) change in `dev`:
428+
```sql
429+
MODEL (name test.a, kind FULL);
430+
SELECT 2 AS col
431+
```
432+
433+
??? "Output plan example #1"
434+
435+
```bash
436+
New environment `dev` will be created from `prod`
437+
438+
Differences from the `prod` environment:
439+
440+
Models:
441+
└── Directly Modified:
442+
└── sqlmesh_example__dev.test_model
443+
444+
---
445+
+++
446+
447+
448+
kind FULL
449+
)
450+
SELECT
451+
- 1 AS col
452+
+ 2 AS col
453+
```
454+
455+
3. Second (metadata) change in `dev`:
456+
```sql
457+
MODEL (name test.a, kind FULL, owner 'John Doe');
458+
SELECT 5 AS col
459+
```
460+
461+
??? "Output plan example #2"
462+
463+
```bash
464+
Differences from the `prod` environment:
465+
466+
Models:
467+
└── Directly Modified:
468+
└── sqlmesh_example__dev.test_model
469+
470+
---
471+
472+
+++
473+
474+
@@ -1,8 +1,9 @@
475+
476+
MODEL (
477+
name sqlmesh_example.test_model,
478+
+ owner "John Doe",
479+
kind FULL
480+
)
481+
SELECT
482+
- 1 AS col
483+
+ 2 AS col
484+
485+
Directly Modified: sqlmesh_example__dev.test_model (Breaking)
486+
Models needing backfill:
487+
└── sqlmesh_example__dev.test_model: [full refresh]
488+
```
489+
490+
Even though the second change should have been a metadata change (thus not requiring a backfill), it will still be classified as a breaking change because the comparison is against production instead of the previous development state. This is intentional and may cause additional backfills as more changes are accumulated.
491+
492+
384493
### Gateways
385494
386495
The `gateways` configuration defines how SQLMesh should connect to the data warehouse, state backend, and scheduler. These options are in the [gateway](../reference/configuration.md#gateway) section of the configuration reference page.

sqlmesh/core/context_diff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def create(
150150
s.snapshot_id for s in initial_env.promoted_snapshots
151151
}
152152

153-
# Find the proper environment to diff against, this might be different than the "initial" (i.e user provided) environment
153+
# Find the proper environment to diff against, this might be different than the initial (i.e user provided) environment
154154
# e.g it will default to prod if the plan option `always_compare_against_prod` is set.
155155
environment = _get_diff_environment(environment, state_reader, always_compare_against_prod)
156156
env = (

0 commit comments

Comments
 (0)