← Statpro dev log

The import ratchet: CI that keeps deleted code dead

#automation#infra#data

A sawtooth staircase descends to the right; an amber ball rests in a notch, with dashed outlines marking the higher steps it has already left.

I deleted about 930 files of legacy data code in 26 hours, and my real worry was the month after. A deprecated package doesn't come back in one big commit. It comes back one convenient import at a time.

Statpro, my sports stats site, is moving its data layer (the code that reads and writes its database) from a legacy database to a new one built around shared tables of games, players and teams. Ten commits removed roughly 134,000 lines of the old surface. Another 517 files still import the deprecated package, the old data library, and I can't fix them all this week. What I can do is make sure that number only goes down.

A one-way ratchet in CI

The check has four parts:

  • A sorted JSON baseline, checked into the repo, lists every file that imports the deprecated package.
  • On every pull request, git grep finds the current importers.
  • A file that isn't in the baseline fails the build, with a pointer to the replacement.
  • A file that has left the list passes, with a hint to regenerate the baseline.

The script is 66 lines of plain Node with no AST parsing. The core is short enough to copy into any repo with the same problem:

function currentImporters() {
  const out = execFileSync("git",
    ["grep", "-l", "--untracked", 'from "@deprecated/pkg"',
     "--", "apps", "packages", "scripts"],
    { cwd: repoRoot, encoding: "utf8" });
  return out.split("\n")
    .filter((f) => /\.(ts|tsx|mts|mjs)$/.test(f))
    .sort();
}

const baseline = new Set(JSON.parse(readFileSync(baselinePath)));
const added = currentImporters().filter((f) => !baseline.has(f));
if (added.length > 0) { console.error("NEW importers"); process.exit(1); }

It runs as the 13th quality check on every change.

The exit code that would have bitten me last

git grep exits with status 1 when it finds no matches. For this ratchet that's the goal, and for execFileSync it's a thrown error. Without a catch around the call, the guard would have crashed on the exact day I deleted the last importer. Test a ratchet at zero before you trust it on the way down.

This is the build log for Statpro (statpro.io) · All entries · Weekly essays