> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sqlbuild.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Contract Adoption

> Compare repository declarations with existing warehouse schemas and generate safe updates.

Ordinary `sqb compile` validates authored contracts offline. When adopting an existing physical
schema, `sqb contract` performs an explicit online, read-only inspection and compares warehouse
columns with repository declarations.

```bash theme={null}
sqb contract diff --from prod --select tag:finance
sqb contract generate --from prod --select tag:finance
```

The target named by `--from` supplies the database and schema namespace to inspect. SQLBuild uses
the active project connection and never resolves a second set of origin credentials.

## Preview and write

`contract diff` reports differences and returns exit code `1` when declarations and physical
relations disagree. `contract generate` prints the proposed declaration changes.

```bash theme={null}
# Fill missing types and append physical columns missing from code
sqb contract generate --from prod --select tag:finance --write

# Explicitly replace conflicting types/names and remove declarations absent physically
sqb contract generate --from prod --select tag:finance --write --overwrite
```

Without `--write`, no files change. Contract commands never mutate warehouse relations, lifecycle
state, or fingerprints.

## Safety rules

* Additive generation preserves existing types on conflict, repository-only columns, comments,
  descriptions, audits, nullability, enums, tags, freshness, and loader configuration.
* `--overwrite` is an explicit repository replacement policy, not a warehouse write.
* Generation does not add `contract enforced`; contract activation remains an authored decision.
* A shared `SCHEMA()` is not changed from one model's evidence. Ownership conflicts are reported.
* Writes are atomic and SQLBuild recompiles the project, restoring prior contents if validation
  fails.
* Sources are first-class selectable resources, for example `--select source:raw_orders`.

## Semantic validation

Enforced upstream contracts are authoritative compile-time interfaces. When input evidence is
complete, missing or ambiguous columns fail in projections, joins, filters, grouping, windows, and
ordering. Partial or opaque schemas remain open; SQLBuild does not turn missing metadata into a
false error.

Use `sqb lineage <model> --include-uses --format json` to inspect direct non-projection column uses
such as `join_on`, `where`, `group_by`, and `window_order_by`. These direct-use facts are distinct
from value-producing column-lineage edges.

See [Model Contracts](/concepts/models/contracts) and [`sqb contract`](/cli/contract).
