Skip to content

Commit 086bdaf

Browse files
dbt sync: keep per-version descriptions on versioned models
Each version of a versioned model now keeps its own contract's table and column descriptions. The top level holds the latest version's; a version whose description differs gets it on its own entry, and for a column in an override that also carries the column's data_type (dbt drops it for an overriding version) but not its tests (dbt still runs those). Fixes #1840 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 75160dc commit 086bdaf

4 files changed

Lines changed: 153 additions & 29 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- `datacontract test` finds tables in Postgres schemas with upper case letters, such as `"Reporting"`
1313
- `datacontract test` checks the other columns of a Postgres table with a column of a type ibis cannot read, such as `regclass`
1414
- `datacontract import postgres` and `import sql` no longer map bit strings of more than one bit (`bit(8)`, `bit varying`) to `boolean`
15+
- `datacontract dbt sync` keeps each version's own table and column descriptions on a versioned model (#1840)
1516

1617
## [1.2.11] - 2026-11-17
1718

‎datacontract/integration/dbt_sync.py‎

Lines changed: 98 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,7 +2117,7 @@ def _reconcile_versioned_data_types(
21172117
"""Maintain per-version `data_type` on a versioned model.
21182118
21192119
Top-level carries the *latest* version's type; every version whose type differs gets a
2120-
minimal `{name, data_type, description}` override.
2120+
minimal `{name, data_type}` override (`_reconcile_versioned_descriptions` adds its description).
21212121
"""
21222122
latest = str(entry.get("latest_version"))
21232123
unresolved: list[str] = []
@@ -2164,45 +2164,116 @@ def _reconcile_versioned_data_types(
21642164
continue
21652165
if t == new_top:
21662166
override = _bullet_column_override(b, col_name)
2167-
if override is not None and not override.get("data_tests"):
2167+
if override is not None and not override.get("data_tests") and "description" not in override:
21682168
override.pop("data_type", None)
21692169
_collapse_bare_override(b, override, top_col)
21702170
elif override is not None:
2171-
# Override survives for divergent tests → keep data_type; dbt doesn't
2172-
# inherit it into an existing override entry (breaks `contract.enforced`).
2171+
# Override survives for divergent tests or a description → keep data_type; dbt
2172+
# doesn't inherit it into an existing override entry (breaks `contract.enforced`).
21732173
override["data_type"] = t
21742174
else:
21752175
override = _bullet_column_override(b, col_name, create=True, top_col=top_col)
21762176
override["data_type"] = t
2177-
_set_override_description(override, top_col)
21782177
else:
21792178
# Some sibling's type is still unknown (e.g. synced before types existed). Don't impose a
21802179
# top-level type on it — put this version's type in its own bullet only.
21812180
override = _bullet_column_override(this_bullet, col_name, create=True, top_col=top_col)
21822181
override["data_type"] = this_type
2183-
_set_override_description(override, top_col)
21842182

21852183
_warn_unresolved_types(run, model_dict.get("name"), unresolved)
21862184

21872185

2188-
def _set_override_description(override: dict, top_col: Optional[dict]) -> None:
2189-
"""Stamp the top-level column's description onto an override (dbt doesn't inherit it)."""
2190-
desc = top_col.get("description") if isinstance(top_col, dict) else None
2191-
if desc:
2192-
override["description"] = desc
2186+
def _reconcile_versioned_descriptions(
2187+
entry: dict,
2188+
versions: CommentedSeq,
2189+
model_dict: dict,
2190+
props_by_name: dict,
2191+
this_version: str,
2192+
*,
2193+
prune: bool,
2194+
) -> None:
2195+
"""Maintain per-version descriptions on a versioned model, like `data_type`.
2196+
2197+
Top level carries the *latest* version's descriptions. A version whose model description differs
2198+
gets its own `description` on its bullet; one whose column description differs gets an override
2199+
with that `description` and the column's `data_type`: dbt drops the top-level column's properties
2200+
for a version that overrides it, but still runs its tests, so the override doesn't repeat them.
2201+
A sibling's description is read from its bullet or override, else the top level.
2202+
"""
2203+
latest = str(entry.get("latest_version"))
2204+
bullets = [b for b in versions if isinstance(b, dict)]
2205+
2206+
descs = {str(b.get("v")): b.get("description") or entry.get("description") for b in bullets}
2207+
if model_dict.get("description") or prune:
2208+
descs[this_version] = model_dict.get("description")
2209+
top = descs.get(latest, descs.get(this_version))
2210+
if top:
2211+
entry["description"] = top
2212+
else:
2213+
entry.pop("description", None)
2214+
for b in bullets:
2215+
desc = descs[str(b.get("v"))]
2216+
if desc and desc != top:
2217+
b["description"] = desc
2218+
else:
2219+
b.pop("description", None)
2220+
2221+
for desired_col in model_dict.get("columns") or []:
2222+
col_name = desired_col["name"]
2223+
top_col = _find_column(entry, col_name)
2224+
if top_col is None:
2225+
continue
2226+
col_descs: dict[str, Optional[str]] = {}
2227+
for b in bullets:
2228+
override = _bullet_column_override(b, col_name)
2229+
if override is not None and "description" in override:
2230+
col_descs[str(b.get("v"))] = override["description"]
2231+
elif override is not None or col_name.lower() not in {n.lower() for n in _bullet_exclude_names(b)}:
2232+
col_descs[str(b.get("v"))] = top_col.get("description")
2233+
prop = props_by_name.get(col_name.lower())
2234+
this_desc = prop.description.strip().replace("\n", " ") if prop is not None and prop.description else None
2235+
if this_desc or prune:
2236+
col_descs[this_version] = this_desc
2237+
top = col_descs.get(latest, col_descs.get(this_version))
2238+
if top:
2239+
top_col["description"] = top
2240+
else:
2241+
top_col.pop("description", None)
2242+
for b in bullets:
2243+
if str(b.get("v")) not in col_descs:
2244+
continue
2245+
desc = col_descs[str(b.get("v"))]
2246+
override = _bullet_column_override(b, col_name)
2247+
if override is None:
2248+
if not desc or desc == top:
2249+
continue
2250+
override = _bullet_column_override(b, col_name, create=True, top_col=top_col)
2251+
if top_col.get("data_type"):
2252+
override["data_type"] = top_col["data_type"]
2253+
if desc:
2254+
override["description"] = desc
2255+
else:
2256+
override.pop("description", None)
2257+
_collapse_bare_override(b, override, top_col)
21932258

21942259

21952260
def _collapse_bare_override(bullet: dict, override: dict, top_col: Optional[dict]) -> None:
2196-
"""Drop an override element that's decayed to just `{name}` (plus the top-level column's own
2197-
`quote`), then collapse a now-bare `columns`."""
2198-
top_quote = top_col.get("quote") if isinstance(top_col, dict) else None
2199-
if set(override.keys()) == {"name"} or (
2200-
set(override.keys()) == {"name", "quote"} and bool(override["quote"]) == bool(top_quote)
2201-
):
2202-
cols = bullet.get("columns")
2203-
if isinstance(cols, list) and override in cols:
2204-
cols.remove(override)
2205-
_bullet_set_exclude(bullet, _bullet_exclude_names(bullet)) # collapse `columns` if bare
2261+
"""Drop an override element that adds nothing to the top-level column: just its `name`, plus a
2262+
`quote`, `data_type` or `description` equal to the top-level column's. Then collapse a now-bare
2263+
`columns`."""
2264+
top = top_col if isinstance(top_col, dict) else {}
2265+
for key, value in override.items():
2266+
if key == "name":
2267+
continue
2268+
if key == "quote" and bool(value) == bool(top.get("quote")):
2269+
continue
2270+
if key in ("data_type", "description") and value == top.get(key):
2271+
continue
2272+
return
2273+
cols = bullet.get("columns")
2274+
if isinstance(cols, list) and override in cols:
2275+
cols.remove(override)
2276+
_bullet_set_exclude(bullet, _bullet_exclude_names(bullet)) # collapse `columns` if bare
22062277

22072278

22082279
def _merge_versioned_model_entry(
@@ -2226,11 +2297,10 @@ def _merge_versioned_model_entry(
22262297
props_by_name = {p.name.lower(): p for p in (schema_obj.properties or []) if p.name}
22272298
contract_version = odcs.version or "no_version"
22282299

2229-
desc = model_dict.get("description")
2230-
if desc:
2231-
entry["description"] = desc
2232-
elif prune:
2233-
entry.pop("description", None)
2300+
# `_reconcile_versioned_descriptions` sets the descriptions per version at the end, reading what the
2301+
# sibling versions inherit from the top level. Here a new entry only gets its `description` key in place.
2302+
if model_dict.get("description") and "description" not in entry:
2303+
entry["description"] = model_dict["description"]
22342304
_set_meta_keys(entry, data_contract=odcs.id, owner=(odcs.team.name if odcs.team else None))
22352305
_union_tags(entry, list(schema_obj.tags or []), reconcile=False, prune=prune)
22362306

@@ -2261,10 +2331,8 @@ def _merge_versioned_model_entry(
22612331
_mark_managed_column(target)
22622332
if _is_must_quote_name(col_name):
22632333
target["quote"] = True
2264-
if prop is not None and prop.description:
2334+
if created and prop is not None and prop.description: # in place; set per version at the end
22652335
target["description"] = prop.description.strip().replace("\n", " ")
2266-
elif prune:
2267-
target.pop("description", None)
22682336
col_tags = list(prop.tags or []) if prop else []
22692337
_union_tags(target, col_tags, reconcile=(created or _is_managed_column(target)), prune=prune)
22702338

@@ -2309,6 +2377,7 @@ def _merge_versioned_model_entry(
23092377
_reconcile_versioned_data_types(
23102378
entry, versions, model_dict, props_by_name, dialect, model_version, run, prune=prune
23112379
)
2380+
_reconcile_versioned_descriptions(entry, versions, model_dict, props_by_name, model_version, prune=prune)
23122381
for desired_col in model_dict.get("columns") or []:
23132382
if _is_must_quote_name(desired_col["name"]):
23142383
for b in versions:

‎docs/docs/dbt.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ Generated tests quote a column exactly where dbt quotes it in its own tests: whe
178178

179179
If `dbt sync` is fed with a subset of the versioned contracts, the other versions remain unchanged.
180180

181+
Each version keeps the table and column descriptions of its own contract. The model and its top-level columns carry the latest version's descriptions. When another version's description differs, `dbt sync` writes it on that version's entry, and for a column in that version's own entry for the column.
182+
181183
## `datacontract dbt test`
182184

183185
Runs the contract-managed tests that `dbt sync` generated, reports the results, and optionally publishes them. Like `dbt sync`, the command accepts multiple contracts.

‎tests/test_dbt_sync.py‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3678,6 +3678,58 @@ def types(project: Path) -> dict:
36783678
assert types(forward) == types(reverse) == {"top": "bigint", "v1_override": "integer"}
36793679

36803680

3681+
def _described_versioned_contract(version: str, description: str) -> str:
3682+
return (
3683+
_typed_versioned_contract(version, "integer")
3684+
.replace("the amount column", f"amount {description}")
3685+
.replace(" - name: customers\n", f" - name: customers\n description: customers {description}\n")
3686+
)
3687+
3688+
3689+
def _described_versioned_project(tmp_path: Path) -> Path:
3690+
project = _typed_versioned_project(tmp_path)
3691+
(project / "customers-v1.odcs.yaml").write_text(_described_versioned_contract("1.0.0", "v1"))
3692+
(project / "customers-v2.odcs.yaml").write_text(_described_versioned_contract("2.0.0", "v2"))
3693+
return project
3694+
3695+
3696+
@pytest.mark.parametrize("order", [("1", "2"), ("2", "1")])
3697+
def test_versioned_sync_keeps_per_version_descriptions(tmp_path: Path, order: tuple):
3698+
"""Each version keeps its own contract's table and column descriptions, whatever the sync order:
3699+
the latest's at top level, an older version's on its bullet and in a column override."""
3700+
project = _described_versioned_project(tmp_path)
3701+
for v in order:
3702+
_sync_versioned(project, f"customers-v{v}.odcs.yaml", v)
3703+
entry = _versioned_entry(project)
3704+
3705+
assert entry["description"] == "customers v2"
3706+
assert _bullet(entry, 1)["description"] == "customers v1"
3707+
assert "description" not in _bullet(entry, 2)
3708+
assert _col(entry, "amount")["description"] == "amount v2"
3709+
# dbt drops the top-level column's `data_type` for a version that overrides it, so the override
3710+
# repeats that, but not the top-level tests, which dbt still runs (repeating them duplicates the node).
3711+
assert _override(entry, 1, "amount") == {"name": "amount", "data_type": "integer", "description": "amount v1"}
3712+
assert "columns" not in _bullet(entry, 2)
3713+
3714+
3715+
def test_versioned_sync_drops_per_version_descriptions_once_they_agree(tmp_path: Path):
3716+
project = _described_versioned_project(tmp_path)
3717+
_sync_versioned(project, "customers-v1.odcs.yaml", "1")
3718+
_sync_versioned(project, "customers-v2.odcs.yaml", "2")
3719+
first = (project / "models" / "customers.yml").read_text()
3720+
_sync_versioned(project, "customers-v1.odcs.yaml", "1")
3721+
_sync_versioned(project, "customers-v2.odcs.yaml", "2")
3722+
assert (project / "models" / "customers.yml").read_text() == first
3723+
3724+
(project / "customers-v1.odcs.yaml").write_text(_described_versioned_contract("1.0.0", "v2"))
3725+
_sync_versioned(project, "customers-v1.odcs.yaml", "1")
3726+
entry = _versioned_entry(project)
3727+
assert "description" not in _bullet(entry, 1)
3728+
assert "columns" not in _bullet(entry, 1)
3729+
assert entry["description"] == "customers v2"
3730+
assert _col(entry, "amount")["description"] == "amount v2"
3731+
3732+
36813733
def test_sync_rejects_empty_contract(tmp_path: Path):
36823734
"""An empty contract file errors cleanly instead of leaking a raw `NoneType` AttributeError."""
36833735
project = _copy_dbt_project(tmp_path)

0 commit comments

Comments
 (0)