---
status: accepted
---
# A rebuild re-applies dependants after the new version, despite ordering constraints

When a reworked version requests a rebuild, mitch reverts its dependants, applies the new version, and re-applies the dependants after it. The rework at the same time lists those dependants as ordering constraints (`after`), which say the new version runs *after* them. We accept this apparent contradiction on purpose: the ordering constraint decides where the new version enters a deployment's history, and the rebuild decides what happens at that point. Together they give an old and a fresh deployment the same history, which is what reworks are for (ADR `cli/rework/rework-keeps-the-previous-version-in-every-history`).

## Example

A view `order_summary` (migration `order-summary`) has a dependant view `summary_report` (migration `summary-report`). A column is to disappear from `order_summary`, which is impossible while `summary_report` exists on a database that tracks view dependencies. After `mitch rework order-summary` and enabling the prepared rebuild, the new version's config reads:

```toml
id = "order-summary@2"
dependencies = ["order-summary"]
after = ["summary-report"]
rebuild_dependants_of = ["order-summary"]
```

A deployment that was migrated before the rework, and a fresh one, then record the same sequence in their audit tables:

| Step | Old deployment | Fresh deployment |
|---|---|---|
| 1 | apply `orders` (before the rework) | apply `orders` |
| 2 | apply `order-summary` (before the rework) | apply `order-summary` |
| 3 | apply `summary-report` (before the rework) | apply `summary-report`, ordered before `order-summary@2` by `after` |
| 4 | revert `summary-report` (rebuild) | revert `summary-report` (rebuild) |
| 5 | apply `order-summary@2` | apply `order-summary@2` |
| 6 | apply `summary-report` (rebuild) | apply `summary-report` (rebuild) |

The sequence was checked by running exactly this example against SQLite.

## Considered options

- **Exclude migrations named in `after` from the rebuild.** The ordering would hold literally, but `summary_report` would stay in place while `order_summary` is recreated, and the `DROP` the rebuild exists for would fail.
- **Let `rework` leave out `after` when a rebuild is enabled.** The fresh deployment would then run `order-summary`, `order-summary@2`, `summary-report`: with nothing to revert, the rebuild would do nothing, and the history would differ from the old deployment's at steps 3 to 6. The end state would likely match, but reproducible histories are the point of rework, and subtle differences such as column order or catalog entries are exactly what ADR `cli/rework/rework-keeps-the-previous-version-in-every-history` warns about.
- **Warn that the re-applied dependants violate the ordering constraint.** Every rework with a rebuild would warn on every deployment about intended behaviour.

## Consequences

- An ordering constraint governs when a migration first arrives on a deployment, not every later re-apply. Mitch only checks it for pending migrations, so a rebuild's re-apply is never reported as a deviation.
- After the rebuild, the last apply of a dependant is later than the new version, while the recorded constraint still says the new version comes after it. Anyone reading the audit table should read the constraint as "entered the history after", not "was last applied after".
