Skip to content

Commit 14fc3fa

Browse files
authored
Inline referenced definitions in dbt sync (#1766)
* Inline referenced definitions in dbt sync Closes #1765 * Add the issue number to the changelog entry
1 parent 2569861 commit 14fc3fa

5 files changed

Lines changed: 76 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
- - `datacontract dbt sync` inlines the definitions a contract's properties link to, like `lint`, `test` and `export` (#1765)
11+
1012
## [1.2.9] - 2026-10-30
1113

1214
### Added

‎datacontract/command_dbt.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
console,
1313
debug_option,
1414
enable_debug_logging,
15+
inline_references_option,
1516
validate_publish_url,
1617
)
1718
from datacontract.config import cli_config
@@ -337,6 +338,7 @@ def sync_command(
337338
),
338339
] = None,
339340
ssl_verification: Annotated[bool, typer.Option(help="SSL verification when publishing test results.")] = True,
341+
inline_references: inline_references_option = True,
340342
debug: debug_option = None,
341343
):
342344
"""
@@ -371,7 +373,7 @@ def sync_command(
371373
console.print(f"[yellow]Skipping {path}: not an ODCS data contract.[/yellow]")
372374
continue
373375
try:
374-
odcs = resolve_data_contract(str(path))
376+
odcs = resolve_data_contract(str(path), inline_references=inline_references)
375377
model_names = list(resolve_model_names(odcs, model_resolution, schema_name).values())
376378
dbt_version = resolve_versioned_target(project_dir, model_names, parse_filename_version(path))
377379
loaded.append((path, odcs, model_names, dbt_version))
@@ -399,6 +401,7 @@ def sync_command(
399401
model_version=dbt_version,
400402
server=server,
401403
dry_run=dry_run,
404+
inline_references=inline_references,
402405
)
403406
except Exception as e:
404407
if not multiple_contracts:

‎datacontract/integration/dbt_sync.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2873,20 +2873,22 @@ def generate_dbt_tests(
28732873
model_version: Optional[str] = None,
28742874
server: Optional[str] = None,
28752875
dry_run: bool = False,
2876+
inline_references: bool = True,
28762877
) -> DbtTestGenerationResult:
28772878
"""Resolve the contract, merge its tests + metadata into the dbt project's model YAML in place.
28782879
28792880
`model_version` (a dbt model version like "2", from the contract filename) merges this contract
28802881
into a versioned model — a `versions:` block — additively, leaving sibling versions untouched.
28812882
`server` selects the ODCS server whose `type` maps `logicalType`s to a `data_type`.
28822883
`dry_run` computes and reports the same plan without writing, deleting, or moving any files.
2884+
`inline_references` inlines the definitions the contract's properties link to, as `lint`, `test` and `export` do.
28832885
"""
28842886
explicit_project_dir = project_dir is not None
28852887
project_dir = (project_dir or Path.cwd()).resolve()
28862888
_ensure_dbt_project(project_dir, explicit=explicit_project_dir)
28872889

28882890
contract_path = Path(contract).resolve()
2889-
odcs = resolve_data_contract(str(contract_path))
2891+
odcs = resolve_data_contract(str(contract_path), inline_references=inline_references)
28902892
logger.info(f"Resolved contract {odcs.id}@{odcs.version} from {contract_path}")
28912893
dialect = _resolve_type_dialect(odcs, server)
28922894

‎docs/docs/semantics.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ An IRI lookup always requires an API key — `/api/semantics` is API-key only. A
141141

142142
## Turning resolution off
143143

144-
Resolution runs by default on [`lint`](./commands/lint.md), [`test`](./commands/test.md), [`ci`](./commands/ci.md), [`changelog`](./commands/changelog.md), [`breaking`](./commands/breaking.md), and every [`export`](./commands/export/index.md) format. Pass `--no-inline-references` to skip it and work with the contract exactly as written:
144+
Resolution runs by default on [`lint`](./commands/lint.md), [`test`](./commands/test.md), [`ci`](./commands/ci.md), [`changelog`](./commands/changelog.md), [`breaking`](./commands/breaking.md), [`dbt sync`](./commands/dbt/sync.md), and every [`export`](./commands/export/index.md) format. Pass `--no-inline-references` to skip it and work with the contract exactly as written:
145145

146146
```bash
147147
datacontract lint --no-inline-references datacontract.yaml

‎tests/test_dbt_sync.py‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,72 @@ def test_cli_no_dry_run_flag_writes_files(tmp_path: Path):
14041404
assert (project / GENERATED_TESTS_DIR).is_dir()
14051405

14061406

1407+
def _contract_linking_a_definition(directory: Path) -> Path:
1408+
"""An orders contract whose `order_id` takes its meaning from a definition file."""
1409+
(directory / "order_id.yaml").write_text(
1410+
yaml.safe_dump({"description": "Identifies an order across systems.", "required": True})
1411+
)
1412+
contract = directory / "orders.odcs.yaml"
1413+
contract.write_text(
1414+
yaml.safe_dump(
1415+
{
1416+
"apiVersion": "v3.1.0",
1417+
"kind": "DataContract",
1418+
"id": "orders-sync-test",
1419+
"version": "1.0.0",
1420+
"status": "active",
1421+
"schema": [
1422+
{
1423+
"name": "orders",
1424+
"properties": [
1425+
{
1426+
"name": "order_id",
1427+
"logicalType": "string",
1428+
"authoritativeDefinitions": [{"type": "definition", "url": "order_id.yaml"}],
1429+
}
1430+
],
1431+
}
1432+
],
1433+
}
1434+
)
1435+
)
1436+
return contract
1437+
1438+
1439+
def _order_id_column(project: Path) -> dict:
1440+
(model,) = yaml.safe_load((project / "models" / "orders.yml").read_text())["models"]
1441+
return next(c for c in model["columns"] if c["name"] == "order_id")
1442+
1443+
1444+
def test_cli_sync_inlines_linked_definitions(tmp_path: Path):
1445+
project = _copy_dbt_project(tmp_path)
1446+
_orders_model_sql(project)
1447+
1448+
result = CliRunner().invoke(
1449+
app, ["dbt", "sync", str(_contract_linking_a_definition(tmp_path)), "--project-dir", str(project)]
1450+
)
1451+
1452+
assert result.exit_code == 0, result.output
1453+
column = _order_id_column(project)
1454+
assert column["description"] == "Identifies an order across systems."
1455+
assert "not_null" in yaml.safe_dump(column)
1456+
1457+
1458+
def test_cli_sync_no_inline_references_syncs_the_contract_as_written(tmp_path: Path):
1459+
project = _copy_dbt_project(tmp_path)
1460+
_orders_model_sql(project)
1461+
contract = _contract_linking_a_definition(tmp_path)
1462+
1463+
result = CliRunner().invoke(
1464+
app, ["dbt", "sync", str(contract), "--project-dir", str(project), "--no-inline-references"]
1465+
)
1466+
1467+
assert result.exit_code == 0, result.output
1468+
column = _order_id_column(project)
1469+
assert "description" not in column
1470+
assert "not_null" not in yaml.safe_dump(column)
1471+
1472+
14071473
def test_sync_no_dry_run_matches_default_behavior(tmp_path: Path):
14081474
"""`dry_run=False` (the default) and an explicit `--no-dry-run` write identical files."""
14091475
project_default = _copy_dbt_project(tmp_path / "default")

0 commit comments

Comments
 (0)