relayout

tcmts.store.relayout · #3p4rc2ec · term · runs on the server

uses tcmts.store.LAYOUT, which uses node:crypto

Lay a codebase's tables out as this tcm does, taking LAYOUT_STEPS in turn, in the caller's transaction: the layout it was in and is now, or undefined if it was in this one already.

import { LAYOUT, LAYOUT_STEPS } from "#codebase/tcmts/store";
import type { DatabaseSync } from "node:sqlite";

export function relayout(db: DatabaseSync): { from: string; to: string } | undefined {
  const from = (
    db.prepare("SELECT value FROM settings WHERE key = 'layout'").get() as
      | { value: string }
      | undefined
  )?.value;
  if (!from || from === LAYOUT) return undefined;
  for (let at = from; at !== LAYOUT;) {
    const s = LAYOUT_STEPS[at];
    if (!s)
      throw new Error(
        `its tables are laid out as no tcm this one knows (${at}): make the codebase again with this tcm (\`tcm import\` its files, or \`tcm pull\` it)`,
      );
    s.step(db);
    at = s.to;
  }
  db.prepare("UPDATE settings SET value = ? WHERE key = 'layout'").run(LAYOUT);
  return { from, to: LAYOUT };
}

uses

used by