---
name: write-audit-publish
description: Build a replacement batch in isolation, test it against a declared contract, and publish it only after every check passes and the user approves that run. Use for derived tables read by people or downstream systems. Do not use unchanged for streams or in-place application updates.
---

# Write-audit-publish

Keep the published batch intact until its replacement passes every audit.

A failed audit is a good stop. Report the evidence and leave production alone.

## Process

1. Inspect the source and target read-only.
2. Agree on the contract.
3. Build a new version beside the published one.
4. Run every audit and save the evidence.
5. Stop for approval.
6. Publish in one guarded transaction.
7. Verify the published result.

## Start read-only

Confirm:

- the database engine and version;
- the source snapshot or file checksum;
- the target owner, schema, row count, and current version;
- the target's views, foreign keys, triggers, grants, policies, partitions, and replication settings.

If you cannot inspect the target or preserve its dependencies, stop. Do not guess that a table rename is safe.

Read credentials from a named environment variable. Never print or store the credential. Quote validated identifiers with the database driver and bind values as parameters.

## Declare the contract

Write the target columns and seven checks before building:

1. Volume. Set the expected row count or change range.
2. Grain. Name the key and reject null or duplicate keys.
3. Conservation. Explain how source rows become target rows, including filters and deduplication.
4. Bounds. Set allowed values, ranges, and null rules.
5. Reconciliation. Name the totals that must agree and any allowed tolerance.
6. Freshness. Set the source cadence, timezone, maximum age, and no-future-dates rule.
7. Known quirks. Turn known exceptions into named checks.

If the contract is missing, profile the source and propose one. Wait for approval before using it. Do not weaken a failed check during the same run.

## Write a new version

Create a fresh `RUN_ID`. Build an isolated staging table or versioned backing table. Never write to the published target during this step.

Record:

```yaml
run_id: <uuid>
source_fingerprint: <snapshot, version, or checksum>
contract_hash: <sha256>
transform_hash: <commit or sha256>
stage_name: <schema.table>
stage_row_count: <count>
stage_hash: <sha256 of canonical ordered rows>
status: built
```

Keep one immutable run record. Use a new `RUN_ID` after any fix.

## Audit and stop

Run every named check against the stage. Save the observed value, expectation, and pass or fail result. An audit error is a failure, not a pass.

Stop if a check fails or is missing. Do not repair the stage in place. Fix the transform or get approval for a new contract, then start a new run.

If all checks pass, show the user:

- the run ID and target;
- the source, contract, and transform hashes;
- every audit result;
- the stage row count and content hash;
- the exact publish method and rollback version.

Wait for explicit approval of that run. Earlier approval does not carry forward.

## Publish once

Prefer a stable view over versioned backing tables. Build `target__<RUN_ID>`, then point the stable view at it after approval. Preserve the view owner, grants, and column contract.

Use table replacement only when the dependency inventory proves that replacement is safe and the new table preserves every required constraint, index, trigger, policy, grant, and setting.

Inside one transaction:

1. Take a target-scoped lock and set finite timeouts.
2. Recheck the run ID, approval, audit set, hashes, row count, stage identity, and target state.
3. Reject any change since approval.
4. Publish with the engine's atomic operation.
5. Keep the previous version for rollback.

Do not delete the previous version in the publish transaction.

## Verify the real target

Read the published target through the same name consumers use. Confirm its row count, content hash, schema, owner, grants, and dependencies. Run a small consumer query.

If verification fails, point consumers back to the retained version. Report what failed and what changed. Do not keep publishing fixes under the same run ID.

## Adapt with care

PostgreSQL is the reference model. Before using Snowflake, BigQuery, Databricks, Iceberg, Hudi, Delta, dbt, or another engine, verify its atomic publish, locking, dependency, and rollback behavior. Keep the same contract, evidence, approval, and verification rules.

## Invocation example

> Apply the write-audit-publish skill. Read the connection from `DATABASE_URL` and do not print it. Build `TARGET` from `SOURCE`. Use `CONTRACT`, or profile the source and propose one. Show every audit result and stop for my approval before publishing. If a check fails, leave the published target unchanged.
