---
status: accepted
---
# A rework keeps the previous version in every deployment's history

`mitch rework A` archives the current files of A under their original ID `A` and lets the author edit the file at the original path, which gets a new ID (for example `A@2`) and depends on `A`. Every deployment therefore runs A first and the new version on top of it, whether it was deployed before the rework or afterwards. Editing an applied migration without `rework` still counts as modified; for idempotent migrations `status` hints at `rework`.

## Considered options

**Supersede: fresh deployments run only the new version.** Rejected. A deployment that had A, B, C, D, E before the rework ends up with the history A, B, C, D, E, A@2, a fresh one with A@2, B, C, D, E. Even with idempotent scripts the results can differ: column order after `ADD COLUMN`, enum value order after `ADD VALUE` (which changes comparisons), OIDs, catalog dependencies, and the effect of a `DROP ... CASCADE` on objects that only exist on the older deployment. If the new version gains a dependency on B, the orders diverge further. Sqitch keeps old versions as `name@tag` files for the same reason: reproducible histories, not because its plan is linear.

**Implicit rework: an edited idempotent migration is simply re-run.** Rejected for the same reason, since the previous version would be lost from fresh deployments. It also cannot tell a deliberate rework from an accidental edit.

## How `mitch rework A` lays out files

- The current files are archived next to the original as `A@1.up.sql`, `A@1.down.sql` and `A@1.config.toml`, with `id = "A"` in the config. The file at the original path gets `id = "A@2"`. The next rework archives `A@2.*` with `id = "A@2"` and gives the path `A@3`.
- The new down script is prefilled with A's up script, because reverting `A@2` must restore A's state while A stays applied.
- The new config copies `transactional` and `idempotent` from A and gets a fresh `created_at`. It depends on the newest previous version and lists the current transitive dependants of every previous version in the own project as ordering constraints (`after`), so the new version runs after whatever of them a deployment has, without rolling out optional ones that it does not have. The commented-out `rebuild_dependants_of` names all previous versions for the same reason. Dependants in other projects cannot be listed, because a project only references projects it links.
- `mitch rework A` finds the newest version itself, and `status` and `log` group versions. Dependencies always name an exact version; there is no "A means the newest".

## Consequences

- The previous version keeps its ID, so existing records on deployments still match by ID and hash, and existing dependants keep referring to the version they were written against.
- In the runner, A and `A@2` are two ordinary migrations. All rework-specific behaviour lives in the `rework` command and in how versions are presented.
- Replacing a chain of migrations so that fresh deployments skip the old ones is a separate concept (squash), with its own trade-offs.
- A linked project that reworks a migration causes no downgrade ping-pong between consumers with older copies: an older copy simply lacks `A@2`, and missing migrations are not reported for linked projects.
