示例工程 38-schema-migrations-concurrency

38-schema-migrations-concurrency/app/shared/ipc.ts

typescript · 1.6 KB

import type { MigrationResult, HistoryRow } from "./migrations";
import type { ContentionResult } from "./contention";

export const CH = {
  snapshot: "schema:snapshot",
  migrate: "schema:migrate",
  reset: "schema:reset",
  fail: "schema:fail-migration",
  queue: "schema:queue-writes",
  contention: "schema:contention",
  refresh: "schema:refresh",
} as const;

export type EventRow = {
  at: number;
  kind: string;
  detail: string;
};

export type QueueResult = {
  count: number;
  order: number[];
  min: number;
  max: number;
  ms: number;
  at: number;
};

export type FailureInfo = {
  from: number;
  to: number;
  error: string;
  steps: MigrationResult["steps"];
  columns: string[];
  titlePresent: boolean;
  at: number;
};

export type Snapshot = {
  platform: string;
  electronVersion: string;
  nodeVersion: string;
  schema: {
    path: string;
    version: number;
    target: number;
    tables: string[];
    columns: string[];
    counts: { notes: number; events: number; history: number };
    history: HistoryRow[];
  };
  migration: MigrationResult | null;
  failure: FailureInfo | null;
  queue: { pending: number; last: QueueResult | null };
  contention: ContentionResult | null;
  events: EventRow[];
};

export function assertQueueReq(
  value: unknown,
): asserts value is { count: number } {
  if (typeof value !== "object" || value === null) {
    throw new Error("BAD_QUEUE_REQ");
  }
  const count = (value as { count?: unknown }).count;
  if (
    typeof count !== "number" ||
    !Number.isInteger(count) ||
    count < 1 ||
    count > 50
  ) {
    throw new Error("BAD_QUEUE_COUNT");
  }
}