Public repository: timo / mitch

Reader

A migration stitcher (therefore mitch) for PostgreSQL, SQLite, MariaDB, and others

#2 Spec: Rework, rebuild, ordering constraints and ID-based checkout plan open

opened by timo

Labels

Description

Problem Statement

Some migrations define objects that keep evolving: functions, views, policies, grants. Today the only way to change an applied migration is to edit it, which mitch flags as modified, and then redeploy. A redeploy reverts the migration and all its dependants, which is destructive and different from what a fresh deployment does. Writing a new migration for every change instead buries the current definition across many files and makes the change hard to review.

Changes that need dependent objects out of the way are worse. Examples are a view losing a column while policies use it, or a materialized view that cannot be altered. They currently force workarounds such as wrapper views, or SQL functions that read pg_depend and drop and recreate objects themselves. That amounts to a second migration system inside a migration.

Solution

mitch rework A creates a new version of a migration. The previous version is archived next to it under its original ID. The file at the original path becomes the new version with a new ID such as A@2, so git diff shows the change as an edit. The new version depends on the previous one and is ordered after everything that built on it. Every deployment, old or fresh, therefore runs the same history: first the previous version, then the new one.

A migration can request a rebuild of the applied dependants of other migrations. Mitch then reverts them through their down scripts, applies the migration, and re-applies them. Mitch's own dependency graph replaces hand-written catalog walking.

Ordering constraints (after) order migrations without requiring them, so optional dependants are never rolled out just because something was reworked. Checkout compares migrations by ID and hash instead of by path, so switching across a rework touches only what actually differs.

User Stories

  1. As a migration author, I want mitch rework A to archive the current files of A as A@1.* with id = "A" in the archived config, so that deployments that applied A still match it by ID and hash.
  2. As a migration author, I want the file at the original path to become the new version with id = "A@2", so that reviewers see the rework as an edit in git diff.
  3. As a migration author, I want the new version to depend on A, so that every deployment runs A before it.
  4. As a migration author, I want the new version to list all current transitive dependants of A in its own project as ordering constraints, so that fresh deployments run it after the same migrations as old deployments did.
  5. As a migration author, I want the new down script prefilled with A's up script, so that reverting the new version restores A's state while A stays applied.
  6. As a migration author, I want the new config to copy transactional and idempotent from A and get a fresh created_at, so that I only adjust what changes.
  7. As a migration author, I want the rework to include a commented-out rebuild_dependants_of = ["A"], so that I can enable a rebuild with one edit.
  8. As a migration author, I want mitch rework A to find the newest version itself when A@2 exists, archiving A@2.* and creating A@3, so that I never need to remember the latest suffix.
  9. As a migration author, I want mitch rework to fail for an unknown ID, so that typos do not create stray files.
  10. As a migration author, I want mitch rework to reject qualified IDs, so that a linked project's migrations are only reworked in that project.
  11. As an operator of an existing deployment, I want up after a rework to apply only the new version, so that nothing is reverted.
  12. As an operator of a fresh deployment, I want up to apply A, then the dependants of A it requires, then the new version, so that its history matches older deployments.
  13. As an operator of a fresh deployment, I want optional dependants of A that nothing requires to stay unapplied after a rework, so that a rework never rolls out unrelated migrations.
  14. As a migration author, I want editing an applied migration without rework to keep counting as modified, so that accidental edits are still caught.
  15. As a migration author, I want status to suggest mitch rework when a modified migration is marked idempotent, so that I learn the intended workflow.
  16. As a developer, I want status and log to group the versions of a migration, so that I see its history at a glance.
  17. As a migration author, I want dependencies to always name an exact version, so that a later rework never silently changes what an existing dependant builds on.
  18. As a migration author, I want to declare after = [...] in any migration, so that I can fix the order of two migrations, such as two enum value additions, without making one require the other.
  19. As an operator, I want an ordering constraint to take effect only when its target is applied or part of the same run, so that it never pulls migrations onto a deployment.
  20. As a developer, I want an error for a cycle formed by dependencies and active ordering constraints, so that impossible orders are caught.
  21. As an operator, I want a migration that arrives after a migration ordered after it to be applied anyway, with a warning that the order deviates, so that optional migrations can still be installed in any order.
  22. As an operator, I want a flag that restores the order instead, by reverting the later migration, applying the newcomer and re-applying it, so that I can get an exact history when it matters.
  23. As an operator, I want ordering constraints stored on the deployment like dependency edges, so that a deviation is detected even when the declaring project is not loaded.
  24. As a migration author, I want to declare rebuild_dependants_of = ["X"], so that mitch reverts the applied transitive dependants of X, applies my migration, and re-applies them in their current versions.
  25. As a migration author, I want a rebuild to exclude my migration itself and its own dependants, so that it never reverts what it is about to create.
  26. As a migration author, I want a rebuild to work for ordinary migrations too, not only reworks, so that recreating a view with dependent policies needs no rework.
  27. As an operator, I want a rebuild to run in one transaction, so that a failure leaves the deployment unchanged.
  28. As an operator, I want a rebuild refused when a dependant has no down script, so that it never half-succeeds.
  29. As an operator, I want a rebuild refused when a dependant belongs to a project that is not loaded, so that I never break another project's schema.
  30. As an operator, I want a rebuild refused by default when a participant is non-transactional, so that a partial rebuild cannot happen by accident.
  31. As an operator, I want a flag that allows such a rebuild in autocommit mode, so that I can still proceed when I accept the risk.
  32. As an operator, I want mitch to print the command that reaches the intended state after a failed autocommit rebuild, such as mitch add c d ..., so that I can resume once I have fixed the cause.
  33. As a migration author, I want a database error during a rebuild's revert phase to hint that a dependency may be undeclared, so that I fix the graph instead of writing catalog workarounds.
  34. As a developer, I want checkout to a commit before a rework to revert only the new version and keep A, so that switching branches does not revert A and its dependants needlessly.
  35. As a developer, I want checkout to a commit after a rework to treat the new version like any migration added on the target, so that it is applied only with --and-up.
  36. As a developer, I want checkout to keep handling changed IDs in configs, added or removed configs, and renamed files correctly, so that the switch to ID-based planning causes no regressions.
  37. As a developer, I want stash to use the same ID-based comparison, so that stashing a rework in progress behaves like checkout.

Implementation Decisions

  • Glossary and ADRs. The vocabulary is defined in CONTEXT.md: rework, rebuild, ordering constraint, idempotent migration. The decisions are recorded in the ADRs cli/rework/rework-keeps-the-previous-version-in-every-history, runner/rebuild-dependants-through-the-migration-graph, project/ordering-constraints-do-not-require and cli/checkout/transition-plan-compares-ids-and-hashes. The ADR cli/checkout/restore-applied-migrations-but-do-not-up stays valid.

  • Runner neutrality. To the runner, A and A@2 are two ordinary migrations. All rework behaviour lives in the new command and in presentation.

  • Version convention. An ID of the form <base>@<n> is version n of <base>, and a bare <base> is version 1. The convention is used only by rework to find the newest version, and by status and log for grouping. Dependency resolution never interprets it.

  • rework command. It takes an own migration ID and resolves the newest version. It copies that version's up, down and config files to <base>@<k>.*, where k is the archived version number, with the archived config's id set to the archived ID. It then rewrites the config at the original path with:

    • the new ID,
    • dependencies set to the archived ID,
    • after set to the archived version's current transitive dependants in the own project,
    • transactional and idempotent copied,
    • a fresh created_at,
    • a commented-out rebuild_dependants_of.

    Finally it prefills the down script at the original path with the archived up script. Config files are written with a TOML serializer rather than string formatting.

  • Migration metadata. Two new optional config keys, after and rebuild_dependants_of, hold migration references. Qualified IDs are allowed under the linked-project rules. References must resolve to loaded migrations, as dependencies must.

  • Ordering. The run order is a topological sort over dependencies plus active ordering constraints. A constraint is active when its target is applied on the deployment or in the planned run set, so planning needs the deployment's applied set. Cycles are errors.

  • Deployment schema. Ordering constraints are stored in the edge table from the linked-projects spec with their own edge kind. They do not count when deciding whether a migration is orphaned.

  • Late arrival. Before applying a migration, mitch looks up applied migrations with an ordering constraint pointing at it, from the source and from stored edges. By default it warns and proceeds. A flag on up and add plans a revert of those migrations and their applied dependants, then the newcomer, then a re-apply, under the rebuild safety rules.

  • Rebuild. When a migration with rebuild_dependants_of is applied, the plan becomes: revert the applied transitive dependants of the named migrations in reverse order, excluding the migration and its own dependants; apply the migration; re-apply the reverted ones in order. Mitch refuses if any of them lacks a down script or belongs to a project that is not loaded. It also refuses if any participant is non-transactional, unless a flag on up and add allows autocommit. When an autocommit rebuild fails, mitch prints the add command for the steps still missing. The check for destructive down scripts is left as an extension point for the destructive-markings topic.

  • Status hint. A modified migration marked idempotent gets a hint line suggesting mitch rework. Exit codes are unchanged.

  • Checkout and stash planning. The transition plan loads the project as it exists at the target commit and compares migrations by project ID, migration ID and normalized hash. This replaces the path-based diff classification and its special cases. Loading a commit requires the VCS managers to materialize a commit's project files into a temporary directory, for example via a git worktree or hg archive. The local-changes reconciliation before stashing uses the same comparison against the working tree.

Testing Decisions

  • Good tests drive mitch through the CLI and assert on observable results: files rework creates and their contents, exit codes, output, schema objects, and the order of entries in the audit table. They do not assert on internal plans.
  • Seam. The CLI, invoked in-process against SQLite database files and git repositories under the test's temporary directory, as the existing tests do. The order of applies and reverts is asserted through the audit table, which records them sequentially.
  • PostgreSQL. Rebuilds with materialized views, policies, and a DROP failing because of an undeclared dependency only make sense on PostgreSQL, because SQLite does not track view dependencies. Proposal: a small set of tests marked for PostgreSQL, taking a connection URL from an environment variable and skipped when it is absent. A local PostgreSQL is available on the development machine. These are the first tests against a real PostgreSQL in the project.
  • Prior art: the checkout tests in the CLI test module, including the test for a migration edited locally back to the target branch's version, and the redeploy tests for dependants. All existing checkout tests must keep passing unchanged after the switch to ID-based planning.
  • Scenarios to cover:
    • Rework file layout and contents.
    • Repeated rework.
    • Old versus fresh deployment histories being identical in audit order.
    • Optional dependants not pulled in.
    • Late arrival with and without the order flag.
    • Rebuild success, and each refusal reason.
    • The autocommit flag and its resume hint.
    • Checkout backwards and forwards across a rework.

Out of Scope

  • Squash: replacing a chain of migrations so that fresh deployments skip the old ones. This includes any supersede semantics.
  • Marking up or down scripts as destructive, beyond leaving the extension point in rebuild.
  • Freeze: recording and replaying the exact apply order.
  • Implicit rework of edited idempotent migrations, and any "newest version" alias in dependencies.
  • Reworking a linked project's migration from a consuming project.

Further Notes

  • This spec depends on the linked-projects spec (#1) for the edge table and for qualified references in after and rebuild_dependants_of. If it lands first, it must introduce the edge table itself.
  • Rejecting qualified IDs in rework (story 10) was not discussed explicitly. It follows from add accepting only own migrations and from code delivery being out of mitch's scope.
  • The ID-based checkout plan (stories 34 to 37) is independent of rework and could be split off as a refactoring that lands first, protected by the existing checkout tests.

Transferred from the former GitLab tracker (0814/mitch#2, opened 2026-11-05, still open there). Commits referencing it: 606f427, c8f11ab, 130477d, 9e2f6a8, cdbf8a3, 36a5d64, de09935.

Relations

Dependency matrix

Feed

  1. timo opened this issue

  2. timo added the label ready-for-agent