Skip to content

dbt sync: keep per-version descriptions on versioned models - #1841

Open
alexandersundquist wants to merge 12 commits into
datacontract:mainfrom
alexandersundquist:dbt-sync-per-version-descriptions
Open

alexandersundquist wants to merge 12 commits into
datacontract:mainfrom
alexandersundquist:dbt-sync-per-version-descriptions

Conversation

@alexandersundquist

@alexandersundquist alexandersundquist commented Nov 20, 2026 •

Copy link
Copy Markdown

Fixes #1840.

dbt sync now gives each version of a versioned model its own table and column descriptions. The top level holds the latest version's descriptions. When another version's description differs, it is written on that version's entry, and for a column in that version's own entry for the column. That entry also carries the column's data_type, which dbt doesn't take over from the top level, but not its tests, which dbt does. Once the descriptions agree again, these per-version entries are removed.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

  • Tests pass (uv run pytest)
  • Code formatted (uv run ruff check --fix && uv run ruff format)
  • Docs updated (if relevant)
  • CHANGELOG.md entry added

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 datacontract#1840

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@alexandersundquist
alexandersundquist marked this pull request as ready for review November 20, 2026 12:24
@vjhwong

vjhwong commented Nov 20, 2026

Copy link
Copy Markdown

Looks good overall. Could we cover one edge case before merging?

Updating v2’s description can add a column to v1

Say the shared column list contains id and amount, and v1 uses include: [id]. That tells dbt to take only id from the shared list.

When we sync a new description for amount in v2, the current code checks only exclude and assumes v1 already has amount. It writes a v1-specific entry to preserve the old description. dbt then expects amount in v1, even though v1’s SQL only returns id.

Proposed fix

Could we add a small _inherits_top_column helper that checks both include and exclude, with include: "*" as the default? We can use it in two places.

  1. At line 2231 in _reconcile_versioned_descriptions, replace the col_name.lower() not in {...} check with _inherits_top_column(b, top_col["name"]). Keep override is not None in the condition because v1 can also declare amount explicitly.

elif override is not None or col_name.lower() not in {n.lower() for n in _bullet_exclude_names(b)}:

  1. Just before line 2273 in _collapse_bare_override, where it reads cols = bullet.get("columns"), return early if top_col is None or not _inherits_top_column(bullet, top_col["name"]). By this point, the existing checks have found that the properties match. The new check makes sure dbt would actually inherit the shared column after deletion. With include: [id], an explicit amount entry needs to stay even when its description matches.

top = top_col if isinstance(top_col, dict) else {}
for key, value in override.items():
if key == "name":
continue
if key == "quote" and bool(value) == bool(top.get("quote")):
continue
if key in ("data_type", "description") and value == top.get(key):
continue
return
cols = bullet.get("columns")
if isinstance(cols, list) and override in cols:
cols.remove(override)
_bullet_set_exclude(bullet, _bullet_exclude_names(bullet)) # collapse `columns` if bare

Proposed helper

def _inherits_top_column(bullet: dict, column_name: str) -> bool:
    for item in bullet.get("columns") or []:
        if isinstance(item, dict) and "include" in item:
            include = item["include"]
            exclude = item.get("exclude") or []
            included = include in ("*", "all") or (
                isinstance(include, list) and column_name in include
            )
            return included and column_name not in exclude

    return True  # dbt includes all shared columns when there's no selector.

@alexandersundquist

Copy link
Copy Markdown
Author

Nice catch! I'll add a fix for this.

A version with an include list, such as include: [id], doesn't inherit the other top-level columns. Syncing a description no longer adds such a column to it, and no longer removes a column entry the version declares itself.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@alexandersundquist

Copy link
Copy Markdown
Author

While implementing the fix in the latest commit, I noticed the same problem in the per-version type handling, and it already exists on main. Say v1 and v2 both have amount, then v1 drops it, so sync gives v1 exclude: [amount]. If v2 later changes amount's type, sync adds an amount entry to v1, and dbt gives v1 the column again. The fix is small with the new _inherits_top_column: skip versions that neither inherit the column nor have their own entry for it. Want me to add it here as a separate commit, or open a follow-up PR?

@jschoedl

Copy link
Copy Markdown
Collaborator

@alexandersundquist Thanks, I think this can go into this PR as well!

alexandersundquist and others added 2 commits November 21, 2026 16:49
…anges

The per-version type handling counted versions that exclude a column, or leave it out of an include list. When another version changed the column's type, sync added the column back to them. A version that neither inherits the column nor has its own entry for it is now left out. As a result, a column added in a later version gets its data_type at the top level instead of only in that version's override.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@alexandersundquist

Copy link
Copy Markdown
Author

I added the type fix. One side effect: a column added in a later version now gets its data_type at the top level instead of only on that version's own entry, as the versions without the column no longer count as having an unknown type. dbt sees the same type for each version either way, but existing YAML changes once on the next sync.

@jschoedl jschoedl left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

3 remarks on the PR:

Comment thread datacontract/integration/dbt_sync.py Resolved
Comment thread datacontract/integration/dbt_sync.py Resolved
Comment thread datacontract/integration/dbt_sync.py Resolved
jschoedl and others added 7 commits November 23, 2026 16:43
_set_override_tests removed and re-added data_tests, moving it after the override's other keys, so re-syncing unchanged contracts rewrote the file once. It now replaces the tests in place.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
dbt drops the top-level column's properties, including config.tags and config.meta, for a version with its own entry for the column. Those entries now carry a copy of the column's tags and meta, refreshed on every sync, and are still removed once they add nothing beyond it.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…n that version

On a model without a description, the merge wrote the synced version's description to the top level before the description step ran, which then read it as what the other versions already showed. The step now uses the model description from before the sync.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dbt sync: versioned models can't have per-version table and column descriptions

3 participants