testMigrateMovesAnOlderLayout

tcmts.cli.test.e2e.migrate.testMigrateMovesAnOlderLayout · #fwcvhfqm · term

A codebase in an older table layout (tcmts.fixtures.codebase-layout-8f648138: patches kept whole for each branch, made by that layout's tcm) isn't opened, and says to migrate; tcm migrate moves its tables, keeping it as it was beside it, and each branch reads the patches it had.

import { okIn, tcmIn, tempDir } from "#codebase/tcmts/cli/test/e2e/harness";
import { "codebase-layout-8f648138" as codebase_layout_8f648138 } from "#codebase/tcmts/fixtures";
import { Store } from "#codebase/tcmts/store";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { DatabaseSync } from "node:sqlite";

export const testMigrateMovesAnOlderLayout = () => {
  const dir = tempDir("migrate layout");
  fs.mkdirSync(path.join(dir, ".tcm"));
  const file = path.join(dir, ".tcm", "codebase.sqlite");
  fs.writeFileSync(file, codebase_layout_8f648138);
  // what each branch had, as that layout kept it
  const old = new DatabaseSync(file, { readOnly: true });
  const had = Object.fromEntries(
    (old.prepare("SELECT id, name FROM branches").all() as { id: number; name: string }[]).map(
      (b) => [
        b.name,
        (
          old
            .prepare(
              "SELECT old_hash AS o, new_hash AS n FROM patches WHERE branch_id = ? ORDER BY old_hash",
            )
            .all(b.id) as { o: string; n: string }[]
        ).map((r) => ({ old: r.o, new: r.n })),
      ],
    ),
  );
  old.close();
  assert.deepEqual(Object.keys(had).sort(), ["feature", "main"]);
  assert.notDeepEqual(had.main, had.feature);

  const refused = tcmIn(dir, "ls");
  assert.notEqual(refused.status, 0);
  assert.match(refused.out, /laid out as an older tcm laid codebases out: `tcm migrate` moves it/);
  assert.match(
    okIn(dir, "migrate"),
    /migrated its tables from layout 8f648138cf12612e to [0-9a-f]{16} \(\d+ definition\(s\) checked\)/,
  );
  assert.ok(
    fs.existsSync(path.join(dir, ".tcm", "codebase.layout-8f648138cf12612e.sqlite")),
    "the codebase as it was",
  );
  const store = new Store(file);
  try {
    for (const [branch, patches] of Object.entries(had))
      assert.deepEqual(store.patchesOf(branch), patches, branch);
  } finally {
    store.close();
  }
  assert.match(okIn(dir, "view", "lib.base"), /base = 3/);
  okIn(dir, "switch", "feature");
  assert.match(okIn(dir, "view", "lib.base"), /base = 4/);
  assert.match(okIn(dir, "todo"), /nothing to do/);
  assert.match(okIn(dir, "migrate"), /nothing to do/);
}

uses