Public repository: timo / mitch

Reader

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

mitch / docs / adr / runner / rebuild-dependants-through-the-migration-graph.md · 2.5 KB · at 76a2c3d332a1 · raw


status: accepted

Dependent objects are rebuilt through mitch's graph, not the database catalog

Some changes cannot be made while dependent objects exist: a view losing a column while policies use it, or a materialized view that cannot be altered at all. A migration may therefore request a rebuild: mitch reverts the applied transitive dependants with their down scripts in reverse graph order, applies the migration, and re-applies the dependants with their current up scripts. It is opt-in, because CREATE OR REPLACE usually suffices and recomputing materialized views can be expensive.

Considered options

  • Wrapper views that let the real object be renamed and swapped. Works, but adds permanent indirection for a one-time migration problem.
  • A SQL function that reads definitions from the catalog (pg_depend), drops them backwards and recreates them forwards. Works, but it is a second migration system written in SQL, inside a migration, and specific to PostgreSQL.
  • Mitch's own dependency graph (chosen). Mitch already knows which migrations build on which, together with their down and up scripts.

How it is declared

rebuild_dependants_of = ["X"] in a migration's config names the migrations whose applied dependants are rebuilt around it, excluding the migration itself and its own dependants. It works for any migration, not only reworks; mitch rework adds the previous version as a commented-out template.

A rebuild is all or nothing. If any participant is non-transactional, mitch refuses by default. A flag allows the rebuild in autocommit mode anyway; if it then fails halfway, mitch prints the command that brings the deployment to the intended state once the cause is fixed (for example mitch add c d ...).

Consequences

  • A rebuild re-applies the dependants after the migration, even when the migration declares ordering constraints on them, as a reworked version does. See ADR runner/rebuild-reapplies-dependants-after-the-new-version.

  • Only up and add rebuild. When checkout or stash re-apply a migration that requests a rebuild, or when such a migration is reverted, no rebuild happens, and the same blocked DROP can occur.

  • A rebuild is refused when a dependant has no down script or belongs to a project that is not loaded. Once destructive scripts can be marked, a destructive down script refuses it too, so a table with data is never dropped silently.

  • An undeclared dependency surfaces as a failing DROP during rebuild. The fix is to declare it in mitch, which improves the graph.