@prisma-next/migration-tools 0.5.0-dev.8 → 0.5.0-dev.80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -22
- package/dist/{constants-BRi0X7B_.mjs → constants-DWV9_o2Z.mjs} +2 -2
- package/dist/{constants-BRi0X7B_.mjs.map → constants-DWV9_o2Z.mjs.map} +1 -1
- package/dist/errors-EPL_9p9f.mjs +297 -0
- package/dist/errors-EPL_9p9f.mjs.map +1 -0
- package/dist/exports/aggregate.d.mts +614 -0
- package/dist/exports/aggregate.d.mts.map +1 -0
- package/dist/exports/aggregate.mjs +611 -0
- package/dist/exports/aggregate.mjs.map +1 -0
- package/dist/exports/constants.d.mts.map +1 -1
- package/dist/exports/constants.mjs +2 -3
- package/dist/exports/errors.d.mts +68 -0
- package/dist/exports/errors.d.mts.map +1 -0
- package/dist/exports/errors.mjs +2 -0
- package/dist/exports/graph.d.mts +2 -0
- package/dist/exports/graph.mjs +1 -0
- package/dist/exports/hash.d.mts +52 -0
- package/dist/exports/hash.d.mts.map +1 -0
- package/dist/exports/hash.mjs +2 -0
- package/dist/exports/invariants.d.mts +34 -0
- package/dist/exports/invariants.d.mts.map +1 -0
- package/dist/exports/invariants.mjs +2 -0
- package/dist/exports/io.d.mts +66 -6
- package/dist/exports/io.d.mts.map +1 -1
- package/dist/exports/io.mjs +2 -3
- package/dist/exports/metadata.d.mts +2 -0
- package/dist/exports/metadata.mjs +1 -0
- package/dist/exports/migration-graph.d.mts +2 -0
- package/dist/exports/migration-graph.mjs +2 -0
- package/dist/exports/migration-ts.d.mts.map +1 -1
- package/dist/exports/migration-ts.mjs +2 -4
- package/dist/exports/migration-ts.mjs.map +1 -1
- package/dist/exports/migration.d.mts +15 -14
- package/dist/exports/migration.d.mts.map +1 -1
- package/dist/exports/migration.mjs +70 -43
- package/dist/exports/migration.mjs.map +1 -1
- package/dist/exports/package.d.mts +3 -0
- package/dist/exports/package.mjs +1 -0
- package/dist/exports/refs.d.mts.map +1 -1
- package/dist/exports/refs.mjs +3 -4
- package/dist/exports/refs.mjs.map +1 -1
- package/dist/exports/spaces.d.mts +550 -0
- package/dist/exports/spaces.d.mts.map +1 -0
- package/dist/exports/spaces.mjs +223 -0
- package/dist/exports/spaces.mjs.map +1 -0
- package/dist/graph-HMWAldoR.d.mts +28 -0
- package/dist/graph-HMWAldoR.d.mts.map +1 -0
- package/dist/hash-By50zM_E.mjs +74 -0
- package/dist/hash-By50zM_E.mjs.map +1 -0
- package/dist/invariants-Duc8f9NM.mjs +52 -0
- package/dist/invariants-Duc8f9NM.mjs.map +1 -0
- package/dist/io-D13dLvUh.mjs +239 -0
- package/dist/io-D13dLvUh.mjs.map +1 -0
- package/dist/metadata-CFvm3ayn.d.mts +2 -0
- package/dist/migration-graph-DGNnKDY5.mjs +523 -0
- package/dist/migration-graph-DGNnKDY5.mjs.map +1 -0
- package/dist/migration-graph-DulOITvG.d.mts +124 -0
- package/dist/migration-graph-DulOITvG.d.mts.map +1 -0
- package/dist/op-schema-D5qkXfEf.mjs +13 -0
- package/dist/op-schema-D5qkXfEf.mjs.map +1 -0
- package/dist/package-BjiZ7KDy.d.mts +21 -0
- package/dist/package-BjiZ7KDy.d.mts.map +1 -0
- package/dist/read-contract-space-contract-C3-1eyaI.mjs +298 -0
- package/dist/read-contract-space-contract-C3-1eyaI.mjs.map +1 -0
- package/package.json +44 -19
- package/src/aggregate/loader.ts +409 -0
- package/src/aggregate/marker-types.ts +16 -0
- package/src/aggregate/planner-types.ts +171 -0
- package/src/aggregate/planner.ts +158 -0
- package/src/aggregate/project-schema-to-space.ts +64 -0
- package/src/aggregate/strategies/graph-walk.ts +118 -0
- package/src/aggregate/strategies/synth.ts +122 -0
- package/src/aggregate/types.ts +89 -0
- package/src/aggregate/verifier.ts +230 -0
- package/src/assert-descriptor-self-consistency.ts +70 -0
- package/src/compute-extension-space-apply-path.ts +152 -0
- package/src/concatenate-space-apply-inputs.ts +90 -0
- package/src/detect-space-contract-drift.ts +91 -0
- package/src/emit-contract-space-artefacts.ts +70 -0
- package/src/errors.ts +251 -17
- package/src/exports/aggregate.ts +42 -0
- package/src/exports/errors.ts +8 -0
- package/src/exports/graph.ts +1 -0
- package/src/exports/hash.ts +2 -0
- package/src/exports/invariants.ts +1 -0
- package/src/exports/io.ts +3 -1
- package/src/exports/metadata.ts +1 -0
- package/src/exports/{dag.ts → migration-graph.ts} +3 -2
- package/src/exports/migration.ts +0 -1
- package/src/exports/package.ts +2 -0
- package/src/exports/spaces.ts +49 -0
- package/src/gather-disk-contract-space-state.ts +62 -0
- package/src/graph-ops.ts +57 -30
- package/src/graph.ts +25 -0
- package/src/hash.ts +91 -0
- package/src/invariants.ts +56 -0
- package/src/io.ts +163 -40
- package/src/metadata.ts +1 -0
- package/src/migration-base.ts +97 -56
- package/src/migration-graph.ts +676 -0
- package/src/op-schema.ts +11 -0
- package/src/package.ts +21 -0
- package/src/plan-all-spaces.ts +76 -0
- package/src/read-contract-space-contract.ts +44 -0
- package/src/read-contract-space-head-ref.ts +63 -0
- package/src/space-layout.ts +48 -0
- package/src/verify-contract-spaces.ts +272 -0
- package/dist/attestation-BnzTb0Qp.mjs +0 -65
- package/dist/attestation-BnzTb0Qp.mjs.map +0 -1
- package/dist/errors-BmiSgz1j.mjs +0 -160
- package/dist/errors-BmiSgz1j.mjs.map +0 -1
- package/dist/exports/attestation.d.mts +0 -37
- package/dist/exports/attestation.d.mts.map +0 -1
- package/dist/exports/attestation.mjs +0 -4
- package/dist/exports/dag.d.mts +0 -51
- package/dist/exports/dag.d.mts.map +0 -1
- package/dist/exports/dag.mjs +0 -386
- package/dist/exports/dag.mjs.map +0 -1
- package/dist/exports/types.d.mts +0 -35
- package/dist/exports/types.d.mts.map +0 -1
- package/dist/exports/types.mjs +0 -3
- package/dist/io-Cd6GLyjK.mjs +0 -153
- package/dist/io-Cd6GLyjK.mjs.map +0 -1
- package/dist/types-DyGXcWWp.d.mts +0 -71
- package/dist/types-DyGXcWWp.d.mts.map +0 -1
- package/src/attestation.ts +0 -81
- package/src/dag.ts +0 -426
- package/src/exports/attestation.ts +0 -2
- package/src/exports/types.ts +0 -10
- package/src/types.ts +0 -66
package/dist/exports/dag.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dag.mjs","names":["migration: MigrationChainEntry","LABEL_PRIORITY: Record<string, number>","path: MigrationChainEntry[]","tieBreakReasons: string[]","leaves: string[]","cycles: string[][]","stack: Frame[]","cycle: string[]","startNodes: string[]","orphans: MigrationChainEntry[]"],"sources":["../../src/queue.ts","../../src/graph-ops.ts","../../src/dag.ts"],"sourcesContent":["/**\n * FIFO queue with amortised O(1) push and shift.\n *\n * Uses a head-index cursor over a backing array rather than\n * `Array.prototype.shift()`, which is O(n) on V8. Intended for BFS-shaped\n * traversals where the queue is drained in a single pass — it does not\n * reclaim memory for already-shifted items, so it is not suitable for\n * long-lived queues with many push/shift cycles.\n */\nexport class Queue<T> {\n private readonly items: T[];\n private head = 0;\n\n constructor(initial: Iterable<T> = []) {\n this.items = [...initial];\n }\n\n push(item: T): void {\n this.items.push(item);\n }\n\n /**\n * Remove and return the next item. Caller must check `isEmpty` first —\n * shifting an empty queue throws.\n */\n shift(): T {\n if (this.head >= this.items.length) {\n throw new Error('Queue.shift called on empty queue');\n }\n // biome-ignore lint/style/noNonNullAssertion: bounds-checked on the line above\n return this.items[this.head++]!;\n }\n\n get isEmpty(): boolean {\n return this.head >= this.items.length;\n }\n}\n","import { Queue } from './queue';\n\n/**\n * One step of a BFS traversal.\n *\n * `parent` and `incomingEdge` are `null` for start nodes — they were not\n * reached via any edge. For every other node they record the node and edge\n * by which this node was first reached.\n */\nexport interface BfsStep<E> {\n readonly node: string;\n readonly parent: string | null;\n readonly incomingEdge: E | null;\n}\n\n/**\n * Generic breadth-first traversal.\n *\n * Direction (forward/reverse) is expressed by the caller's `neighbours`\n * closure: return `{ next, edge }` pairs where `next` is the node to visit\n * next and `edge` is the edge that connects them. Callers that don't need\n * path reconstruction can ignore the `parent`/`incomingEdge` fields of each\n * yielded step.\n *\n * Stops are intrinsic — callers `break` out of the `for..of` loop when\n * they've found what they're looking for.\n *\n * `ordering`, if provided, controls the order in which neighbours of each\n * node are enqueued. Only matters for path-finding: a deterministic ordering\n * makes BFS return a deterministic shortest path when multiple exist.\n */\nexport function* bfs<E>(\n starts: Iterable<string>,\n neighbours: (node: string) => Iterable<{ next: string; edge: E }>,\n ordering?: (items: readonly { next: string; edge: E }[]) => readonly { next: string; edge: E }[],\n): Generator<BfsStep<E>> {\n const visited = new Set<string>();\n const parentMap = new Map<string, { parent: string; edge: E }>();\n const queue = new Queue<string>();\n for (const start of starts) {\n if (!visited.has(start)) {\n visited.add(start);\n queue.push(start);\n }\n }\n while (!queue.isEmpty) {\n const current = queue.shift();\n const parentInfo = parentMap.get(current);\n yield {\n node: current,\n parent: parentInfo?.parent ?? null,\n incomingEdge: parentInfo?.edge ?? null,\n };\n\n const items = neighbours(current);\n const toVisit = ordering ? ordering([...items]) : items;\n for (const { next, edge } of toVisit) {\n if (!visited.has(next)) {\n visited.add(next);\n parentMap.set(next, { parent: current, edge });\n queue.push(next);\n }\n }\n }\n}\n","import { ifDefined } from '@prisma-next/utils/defined';\nimport { EMPTY_CONTRACT_HASH } from './constants';\nimport {\n errorAmbiguousTarget,\n errorDuplicateMigrationId,\n errorNoInitialMigration,\n errorNoTarget,\n errorSameSourceAndTarget,\n} from './errors';\nimport { bfs } from './graph-ops';\nimport type { MigrationBundle, MigrationChainEntry, MigrationGraph } from './types';\n\n/** Forward-edge neighbours for BFS: edge `e` from `n` visits `e.to` next. */\nfunction forwardNeighbours(graph: MigrationGraph, node: string) {\n return (graph.forwardChain.get(node) ?? []).map((edge) => ({ next: edge.to, edge }));\n}\n\n/** Reverse-edge neighbours for BFS: edge `e` from `n` visits `e.from` next. */\nfunction reverseNeighbours(graph: MigrationGraph, node: string) {\n return (graph.reverseChain.get(node) ?? []).map((edge) => ({ next: edge.from, edge }));\n}\n\nfunction appendEdge(\n map: Map<string, MigrationChainEntry[]>,\n key: string,\n entry: MigrationChainEntry,\n): void {\n const bucket = map.get(key);\n if (bucket) bucket.push(entry);\n else map.set(key, [entry]);\n}\n\nexport function reconstructGraph(packages: readonly MigrationBundle[]): MigrationGraph {\n const nodes = new Set<string>();\n const forwardChain = new Map<string, MigrationChainEntry[]>();\n const reverseChain = new Map<string, MigrationChainEntry[]>();\n const migrationById = new Map<string, MigrationChainEntry>();\n\n for (const pkg of packages) {\n const { from, to } = pkg.manifest;\n\n if (from === to) {\n throw errorSameSourceAndTarget(pkg.dirName, from);\n }\n\n nodes.add(from);\n nodes.add(to);\n\n const migration: MigrationChainEntry = {\n from,\n to,\n migrationId: pkg.manifest.migrationId,\n dirName: pkg.dirName,\n createdAt: pkg.manifest.createdAt,\n labels: pkg.manifest.labels,\n };\n\n if (migrationById.has(migration.migrationId)) {\n throw errorDuplicateMigrationId(migration.migrationId);\n }\n migrationById.set(migration.migrationId, migration);\n\n appendEdge(forwardChain, from, migration);\n appendEdge(reverseChain, to, migration);\n }\n\n return { nodes, forwardChain, reverseChain, migrationById };\n}\n\n// ---------------------------------------------------------------------------\n// Deterministic tie-breaking for BFS neighbour order.\n// Used by `findPath` and `findPathWithDecision` only; not a general-purpose\n// utility. Ordering: label priority → createdAt → to → migrationId.\n// ---------------------------------------------------------------------------\n\nconst LABEL_PRIORITY: Record<string, number> = { main: 0, default: 1, feature: 2 };\n\nfunction labelPriority(labels: readonly string[]): number {\n let best = 3;\n for (const l of labels) {\n const p = LABEL_PRIORITY[l];\n if (p !== undefined && p < best) best = p;\n }\n return best;\n}\n\nfunction compareTieBreak(a: MigrationChainEntry, b: MigrationChainEntry): number {\n const lp = labelPriority(a.labels) - labelPriority(b.labels);\n if (lp !== 0) return lp;\n const ca = a.createdAt.localeCompare(b.createdAt);\n if (ca !== 0) return ca;\n const tc = a.to.localeCompare(b.to);\n if (tc !== 0) return tc;\n return a.migrationId.localeCompare(b.migrationId);\n}\n\nfunction sortedNeighbors(edges: readonly MigrationChainEntry[]): readonly MigrationChainEntry[] {\n return [...edges].sort(compareTieBreak);\n}\n\n/** Ordering adapter for `bfs` — sorts `{next, edge}` pairs by tie-break. */\nfunction bfsOrdering(\n items: readonly { next: string; edge: MigrationChainEntry }[],\n): readonly { next: string; edge: MigrationChainEntry }[] {\n return items.slice().sort((a, b) => compareTieBreak(a.edge, b.edge));\n}\n\n/**\n * Find the shortest path from `fromHash` to `toHash` using BFS over the\n * contract-hash graph. Returns the ordered list of edges, or null if no path\n * exists. Returns an empty array when `fromHash === toHash` (no-op).\n *\n * Neighbor ordering is deterministic via the tie-break sort key:\n * label priority → createdAt → to → migrationId.\n */\nexport function findPath(\n graph: MigrationGraph,\n fromHash: string,\n toHash: string,\n): readonly MigrationChainEntry[] | null {\n if (fromHash === toHash) return [];\n\n const parents = new Map<string, { parent: string; edge: MigrationChainEntry }>();\n for (const step of bfs([fromHash], (n) => forwardNeighbours(graph, n), bfsOrdering)) {\n if (step.parent !== null && step.incomingEdge !== null) {\n parents.set(step.node, { parent: step.parent, edge: step.incomingEdge });\n }\n if (step.node === toHash) {\n const path: MigrationChainEntry[] = [];\n let cur = toHash;\n let p = parents.get(cur);\n while (p) {\n path.push(p.edge);\n cur = p.parent;\n p = parents.get(cur);\n }\n path.reverse();\n return path;\n }\n }\n\n return null;\n}\n\n/**\n * Reverse-BFS from `toHash` over `reverseChain` to collect every node from\n * which `toHash` is reachable (inclusive of `toHash` itself).\n */\nfunction collectNodesReachingTarget(graph: MigrationGraph, toHash: string): Set<string> {\n const reached = new Set<string>();\n for (const step of bfs([toHash], (n) => reverseNeighbours(graph, n))) {\n reached.add(step.node);\n }\n return reached;\n}\n\nexport interface PathDecision {\n readonly selectedPath: readonly MigrationChainEntry[];\n readonly fromHash: string;\n readonly toHash: string;\n readonly alternativeCount: number;\n readonly tieBreakReasons: readonly string[];\n readonly refName?: string;\n}\n\n/**\n * Find the shortest path from `fromHash` to `toHash` and return structured\n * path-decision metadata for machine-readable output.\n */\nexport function findPathWithDecision(\n graph: MigrationGraph,\n fromHash: string,\n toHash: string,\n refName?: string,\n): PathDecision | null {\n if (fromHash === toHash) {\n return {\n selectedPath: [],\n fromHash,\n toHash,\n alternativeCount: 0,\n tieBreakReasons: [],\n ...ifDefined('refName', refName),\n };\n }\n\n const path = findPath(graph, fromHash, toHash);\n if (!path) return null;\n\n // Single reverse BFS marks every node from which `toHash` is reachable.\n // Replaces a per-edge `findPath(e.to, toHash)` call inside the loop below,\n // which made the whole function O(|path| · (V + E)) instead of O(V + E).\n const reachesTarget = collectNodesReachingTarget(graph, toHash);\n\n const tieBreakReasons: string[] = [];\n let alternativeCount = 0;\n\n for (const edge of path) {\n const outgoing = graph.forwardChain.get(edge.from);\n if (outgoing && outgoing.length > 1) {\n const reachable = outgoing.filter((e) => reachesTarget.has(e.to));\n if (reachable.length > 1) {\n alternativeCount += reachable.length - 1;\n const sorted = sortedNeighbors(reachable);\n if (sorted[0] && sorted[0].migrationId === edge.migrationId) {\n if (reachable.some((e) => e.migrationId !== edge.migrationId)) {\n tieBreakReasons.push(\n `at ${edge.from}: ${reachable.length} candidates, selected by tie-break`,\n );\n }\n }\n }\n }\n }\n\n return {\n selectedPath: path,\n fromHash,\n toHash,\n alternativeCount,\n tieBreakReasons,\n ...ifDefined('refName', refName),\n };\n}\n\n/**\n * Walk ancestors of each branch tip back to find the last node\n * that appears on all paths. Returns `fromHash` if no shared ancestor is found.\n */\nfunction findDivergencePoint(\n graph: MigrationGraph,\n fromHash: string,\n leaves: readonly string[],\n): string {\n const ancestorSets = leaves.map((leaf) => {\n const ancestors = new Set<string>();\n for (const step of bfs([leaf], (n) => reverseNeighbours(graph, n))) {\n ancestors.add(step.node);\n }\n return ancestors;\n });\n\n const commonAncestors = [...(ancestorSets[0] ?? [])].filter((node) =>\n ancestorSets.every((s) => s.has(node)),\n );\n\n let deepest = fromHash;\n let deepestDepth = -1;\n for (const ancestor of commonAncestors) {\n const path = findPath(graph, fromHash, ancestor);\n const depth = path ? path.length : 0;\n if (depth > deepestDepth) {\n deepestDepth = depth;\n deepest = ancestor;\n }\n }\n return deepest;\n}\n\n/**\n * Find all branch tips (nodes with no outgoing edges) reachable from\n * `fromHash` via forward edges.\n */\nexport function findReachableLeaves(graph: MigrationGraph, fromHash: string): readonly string[] {\n const leaves: string[] = [];\n for (const step of bfs([fromHash], (n) => forwardNeighbours(graph, n))) {\n if (!graph.forwardChain.get(step.node)?.length) {\n leaves.push(step.node);\n }\n }\n return leaves;\n}\n\n/**\n * Find the target contract hash of the migration graph reachable from\n * EMPTY_CONTRACT_HASH. Returns `null` for a graph that has no target\n * state (either empty, or containing only the root with no outgoing\n * edges). Throws NO_INITIAL_MIGRATION if the graph has nodes but none\n * originate from the empty hash, and AMBIGUOUS_TARGET if multiple\n * branch tips exist.\n */\nexport function findLeaf(graph: MigrationGraph): string | null {\n if (graph.nodes.size === 0) {\n return null;\n }\n\n if (!graph.nodes.has(EMPTY_CONTRACT_HASH)) {\n throw errorNoInitialMigration([...graph.nodes]);\n }\n\n const leaves = findReachableLeaves(graph, EMPTY_CONTRACT_HASH);\n\n if (leaves.length === 0) {\n const reachable = [...graph.nodes].filter((n) => n !== EMPTY_CONTRACT_HASH);\n if (reachable.length > 0) {\n throw errorNoTarget(reachable);\n }\n return null;\n }\n\n if (leaves.length > 1) {\n const divergencePoint = findDivergencePoint(graph, EMPTY_CONTRACT_HASH, leaves);\n const branches = leaves.map((tip) => {\n const path = findPath(graph, divergencePoint, tip);\n return {\n tip,\n edges: (path ?? []).map((e) => ({ dirName: e.dirName, from: e.from, to: e.to })),\n };\n });\n throw errorAmbiguousTarget(leaves, { divergencePoint, branches });\n }\n\n // biome-ignore lint/style/noNonNullAssertion: leaves.length is neither 0 nor >1 per the branches above, so exactly one leaf remains\n return leaves[0]!;\n}\n\n/**\n * Find the latest migration entry by traversing from EMPTY_CONTRACT_HASH\n * to the single target. Returns null for an empty graph.\n * Throws AMBIGUOUS_TARGET if the graph has multiple branch tips.\n */\nexport function findLatestMigration(graph: MigrationGraph): MigrationChainEntry | null {\n const leafHash = findLeaf(graph);\n if (leafHash === null) return null;\n\n const path = findPath(graph, EMPTY_CONTRACT_HASH, leafHash);\n return path?.at(-1) ?? null;\n}\n\nexport function detectCycles(graph: MigrationGraph): readonly string[][] {\n const WHITE = 0;\n const GRAY = 1;\n const BLACK = 2;\n\n const color = new Map<string, number>();\n const parentMap = new Map<string, string | null>();\n const cycles: string[][] = [];\n\n for (const node of graph.nodes) {\n color.set(node, WHITE);\n }\n\n // Iterative three-color DFS. A frame is (node, outgoing edges, next-index).\n interface Frame {\n node: string;\n outgoing: readonly MigrationChainEntry[];\n index: number;\n }\n const stack: Frame[] = [];\n\n function pushFrame(u: string): void {\n color.set(u, GRAY);\n stack.push({ node: u, outgoing: graph.forwardChain.get(u) ?? [], index: 0 });\n }\n\n for (const root of graph.nodes) {\n if (color.get(root) !== WHITE) continue;\n parentMap.set(root, null);\n pushFrame(root);\n\n while (stack.length > 0) {\n // biome-ignore lint/style/noNonNullAssertion: stack.length > 0 should guarantee that this cannot be undefined\n const frame = stack[stack.length - 1]!;\n if (frame.index >= frame.outgoing.length) {\n color.set(frame.node, BLACK);\n stack.pop();\n continue;\n }\n // biome-ignore lint/style/noNonNullAssertion: the early-continue above guarantees frame.index < frame.outgoing.length here, so this is defined\n const edge = frame.outgoing[frame.index++]!;\n const v = edge.to;\n const vColor = color.get(v);\n if (vColor === GRAY) {\n const cycle: string[] = [v];\n let cur = frame.node;\n while (cur !== v) {\n cycle.push(cur);\n cur = parentMap.get(cur) ?? v;\n }\n cycle.reverse();\n cycles.push(cycle);\n } else if (vColor === WHITE) {\n parentMap.set(v, frame.node);\n pushFrame(v);\n }\n }\n }\n\n return cycles;\n}\n\nexport function detectOrphans(graph: MigrationGraph): readonly MigrationChainEntry[] {\n if (graph.nodes.size === 0) return [];\n\n const reachable = new Set<string>();\n const startNodes: string[] = [];\n\n if (graph.forwardChain.has(EMPTY_CONTRACT_HASH)) {\n startNodes.push(EMPTY_CONTRACT_HASH);\n } else {\n const allTargets = new Set<string>();\n for (const edges of graph.forwardChain.values()) {\n for (const edge of edges) {\n allTargets.add(edge.to);\n }\n }\n for (const node of graph.nodes) {\n if (!allTargets.has(node)) {\n startNodes.push(node);\n }\n }\n }\n\n for (const step of bfs(startNodes, (n) => forwardNeighbours(graph, n))) {\n reachable.add(step.node);\n }\n\n const orphans: MigrationChainEntry[] = [];\n for (const [from, migrations] of graph.forwardChain) {\n if (!reachable.has(from)) {\n orphans.push(...migrations);\n }\n }\n\n return orphans;\n}\n"],"mappings":";;;;;;;;;;;;;;AASA,IAAa,QAAb,MAAsB;CACpB,AAAiB;CACjB,AAAQ,OAAO;CAEf,YAAY,UAAuB,EAAE,EAAE;AACrC,OAAK,QAAQ,CAAC,GAAG,QAAQ;;CAG3B,KAAK,MAAe;AAClB,OAAK,MAAM,KAAK,KAAK;;;;;;CAOvB,QAAW;AACT,MAAI,KAAK,QAAQ,KAAK,MAAM,OAC1B,OAAM,IAAI,MAAM,oCAAoC;AAGtD,SAAO,KAAK,MAAM,KAAK;;CAGzB,IAAI,UAAmB;AACrB,SAAO,KAAK,QAAQ,KAAK,MAAM;;;;;;;;;;;;;;;;;;;;;;ACHnC,UAAiB,IACf,QACA,YACA,UACuB;CACvB,MAAM,0BAAU,IAAI,KAAa;CACjC,MAAM,4BAAY,IAAI,KAA0C;CAChE,MAAM,QAAQ,IAAI,OAAe;AACjC,MAAK,MAAM,SAAS,OAClB,KAAI,CAAC,QAAQ,IAAI,MAAM,EAAE;AACvB,UAAQ,IAAI,MAAM;AAClB,QAAM,KAAK,MAAM;;AAGrB,QAAO,CAAC,MAAM,SAAS;EACrB,MAAM,UAAU,MAAM,OAAO;EAC7B,MAAM,aAAa,UAAU,IAAI,QAAQ;AACzC,QAAM;GACJ,MAAM;GACN,QAAQ,YAAY,UAAU;GAC9B,cAAc,YAAY,QAAQ;GACnC;EAED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ChainEntry, o as MigrationOps, r as MigrationGraph, t as MigrationBundle } from "../types-DyGXcWWp.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/errors.d.ts
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Structured error for migration tooling operations.
|
|
7
|
-
*
|
|
8
|
-
* Follows the NAMESPACE.SUBCODE convention from ADR 027. All codes live under
|
|
9
|
-
* the MIGRATION namespace. These are tooling-time errors (file I/O, attestation,
|
|
10
|
-
* migration history reconstruction), distinct from the runtime MIGRATION.* codes for apply-time
|
|
11
|
-
* failures (PRECHECK_FAILED, POSTCHECK_FAILED, etc.).
|
|
12
|
-
*
|
|
13
|
-
* Fields:
|
|
14
|
-
* - code: Stable machine-readable code (MIGRATION.SUBCODE)
|
|
15
|
-
* - category: Always 'MIGRATION'
|
|
16
|
-
* - why: Explains the cause in plain language
|
|
17
|
-
* - fix: Actionable remediation step
|
|
18
|
-
* - details: Machine-readable structured data for agents
|
|
19
|
-
*/
|
|
20
|
-
declare class MigrationToolsError extends Error {
|
|
21
|
-
readonly code: string;
|
|
22
|
-
readonly category: "MIGRATION";
|
|
23
|
-
readonly why: string;
|
|
24
|
-
readonly fix: string;
|
|
25
|
-
readonly details: Record<string, unknown> | undefined;
|
|
26
|
-
constructor(code: string, summary: string, options: {
|
|
27
|
-
readonly why: string;
|
|
28
|
-
readonly fix: string;
|
|
29
|
-
readonly details?: Record<string, unknown>;
|
|
30
|
-
});
|
|
31
|
-
static is(error: unknown): error is MigrationToolsError;
|
|
32
|
-
}
|
|
33
|
-
//#endregion
|
|
34
|
-
export { type MigrationBundle, type MigrationBundle as MigrationPackage, type MigrationChainEntry, type MigrationGraph, type MigrationHints, type MigrationManifest, type MigrationOps, MigrationToolsError };
|
|
35
|
-
//# sourceMappingURL=types.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.mts","names":[],"sources":["../../src/errors.ts"],"sourcesContent":[],"mappings":";;;;;;;AAeA;;;;;;;;;;;;cAAa,mBAAA,SAA4B,KAAA;;;;;oBAKrB;;;;uBAQK;;sCAWa"}
|
package/dist/exports/types.mjs
DELETED
package/dist/io-Cd6GLyjK.mjs
DELETED
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import { a as errorInvalidDestName, d as errorInvalidSlug, f as errorMissingFile, o as errorInvalidJson, r as errorDirectoryExists, s as errorInvalidManifest } from "./errors-BmiSgz1j.mjs";
|
|
2
|
-
import { copyFile, mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
|
|
3
|
-
import { type } from "arktype";
|
|
4
|
-
import { basename, dirname, join } from "pathe";
|
|
5
|
-
|
|
6
|
-
//#region src/io.ts
|
|
7
|
-
const MANIFEST_FILE = "migration.json";
|
|
8
|
-
const OPS_FILE = "ops.json";
|
|
9
|
-
const MAX_SLUG_LENGTH = 64;
|
|
10
|
-
function hasErrnoCode(error, code) {
|
|
11
|
-
return error instanceof Error && error.code === code;
|
|
12
|
-
}
|
|
13
|
-
const MigrationManifestSchema = type({
|
|
14
|
-
from: "string",
|
|
15
|
-
to: "string",
|
|
16
|
-
migrationId: "string",
|
|
17
|
-
kind: "'regular' | 'baseline'",
|
|
18
|
-
fromContract: "object | null",
|
|
19
|
-
toContract: "object",
|
|
20
|
-
hints: type({
|
|
21
|
-
used: "string[]",
|
|
22
|
-
applied: "string[]",
|
|
23
|
-
plannerVersion: "string"
|
|
24
|
-
}),
|
|
25
|
-
labels: "string[]",
|
|
26
|
-
"authorship?": type({
|
|
27
|
-
"author?": "string",
|
|
28
|
-
"email?": "string"
|
|
29
|
-
}),
|
|
30
|
-
"signature?": type({
|
|
31
|
-
keyId: "string",
|
|
32
|
-
value: "string"
|
|
33
|
-
}).or("null"),
|
|
34
|
-
createdAt: "string"
|
|
35
|
-
});
|
|
36
|
-
const MigrationOpsSchema = type({
|
|
37
|
-
id: "string",
|
|
38
|
-
label: "string",
|
|
39
|
-
operationClass: "'additive' | 'widening' | 'destructive' | 'data'"
|
|
40
|
-
}).array();
|
|
41
|
-
async function writeMigrationPackage(dir, manifest, ops) {
|
|
42
|
-
await mkdir(dirname(dir), { recursive: true });
|
|
43
|
-
try {
|
|
44
|
-
await mkdir(dir);
|
|
45
|
-
} catch (error) {
|
|
46
|
-
if (hasErrnoCode(error, "EEXIST")) throw errorDirectoryExists(dir);
|
|
47
|
-
throw error;
|
|
48
|
-
}
|
|
49
|
-
await writeFile(join(dir, MANIFEST_FILE), JSON.stringify(manifest, null, 2), { flag: "wx" });
|
|
50
|
-
await writeFile(join(dir, OPS_FILE), JSON.stringify(ops, null, 2), { flag: "wx" });
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Copy a list of files into `destDir`, optionally renaming each one.
|
|
54
|
-
*
|
|
55
|
-
* The destination directory is created (with `recursive: true`) if it
|
|
56
|
-
* does not already exist. Each source path is copied byte-for-byte into
|
|
57
|
-
* `destDir/<destName>`; missing sources throw `ENOENT`. The helper is
|
|
58
|
-
* intentionally generic: callers own the list of files (e.g. a contract
|
|
59
|
-
* emitter's emitted output) and the naming convention (e.g. renaming
|
|
60
|
-
* the destination contract to `end-contract.*` and the source contract
|
|
61
|
-
* to `start-contract.*`).
|
|
62
|
-
*/
|
|
63
|
-
async function copyFilesWithRename(destDir, files) {
|
|
64
|
-
await mkdir(destDir, { recursive: true });
|
|
65
|
-
for (const file of files) {
|
|
66
|
-
if (basename(file.destName) !== file.destName) throw errorInvalidDestName(file.destName);
|
|
67
|
-
await copyFile(file.sourcePath, join(destDir, file.destName));
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
async function writeMigrationManifest(dir, manifest) {
|
|
71
|
-
await writeFile(join(dir, MANIFEST_FILE), `${JSON.stringify(manifest, null, 2)}\n`);
|
|
72
|
-
}
|
|
73
|
-
async function writeMigrationOps(dir, ops) {
|
|
74
|
-
await writeFile(join(dir, OPS_FILE), `${JSON.stringify(ops, null, 2)}\n`);
|
|
75
|
-
}
|
|
76
|
-
async function readMigrationPackage(dir) {
|
|
77
|
-
const manifestPath = join(dir, MANIFEST_FILE);
|
|
78
|
-
const opsPath = join(dir, OPS_FILE);
|
|
79
|
-
let manifestRaw;
|
|
80
|
-
try {
|
|
81
|
-
manifestRaw = await readFile(manifestPath, "utf-8");
|
|
82
|
-
} catch (error) {
|
|
83
|
-
if (hasErrnoCode(error, "ENOENT")) throw errorMissingFile(MANIFEST_FILE, dir);
|
|
84
|
-
throw error;
|
|
85
|
-
}
|
|
86
|
-
let opsRaw;
|
|
87
|
-
try {
|
|
88
|
-
opsRaw = await readFile(opsPath, "utf-8");
|
|
89
|
-
} catch (error) {
|
|
90
|
-
if (hasErrnoCode(error, "ENOENT")) throw errorMissingFile(OPS_FILE, dir);
|
|
91
|
-
throw error;
|
|
92
|
-
}
|
|
93
|
-
let manifest;
|
|
94
|
-
try {
|
|
95
|
-
manifest = JSON.parse(manifestRaw);
|
|
96
|
-
} catch (e) {
|
|
97
|
-
throw errorInvalidJson(manifestPath, e instanceof Error ? e.message : String(e));
|
|
98
|
-
}
|
|
99
|
-
let ops;
|
|
100
|
-
try {
|
|
101
|
-
ops = JSON.parse(opsRaw);
|
|
102
|
-
} catch (e) {
|
|
103
|
-
throw errorInvalidJson(opsPath, e instanceof Error ? e.message : String(e));
|
|
104
|
-
}
|
|
105
|
-
validateManifest(manifest, manifestPath);
|
|
106
|
-
validateOps(ops, opsPath);
|
|
107
|
-
return {
|
|
108
|
-
dirName: basename(dir),
|
|
109
|
-
dirPath: dir,
|
|
110
|
-
manifest,
|
|
111
|
-
ops
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
function validateManifest(manifest, filePath) {
|
|
115
|
-
const result = MigrationManifestSchema(manifest);
|
|
116
|
-
if (result instanceof type.errors) throw errorInvalidManifest(filePath, result.summary);
|
|
117
|
-
}
|
|
118
|
-
function validateOps(ops, filePath) {
|
|
119
|
-
const result = MigrationOpsSchema(ops);
|
|
120
|
-
if (result instanceof type.errors) throw errorInvalidManifest(filePath, result.summary);
|
|
121
|
-
}
|
|
122
|
-
async function readMigrationsDir(migrationsRoot) {
|
|
123
|
-
let entries;
|
|
124
|
-
try {
|
|
125
|
-
entries = await readdir(migrationsRoot);
|
|
126
|
-
} catch (error) {
|
|
127
|
-
if (hasErrnoCode(error, "ENOENT")) return [];
|
|
128
|
-
throw error;
|
|
129
|
-
}
|
|
130
|
-
const packages = [];
|
|
131
|
-
for (const entry of entries.sort()) {
|
|
132
|
-
const entryPath = join(migrationsRoot, entry);
|
|
133
|
-
if (!(await stat(entryPath)).isDirectory()) continue;
|
|
134
|
-
const manifestPath = join(entryPath, MANIFEST_FILE);
|
|
135
|
-
try {
|
|
136
|
-
await stat(manifestPath);
|
|
137
|
-
} catch {
|
|
138
|
-
continue;
|
|
139
|
-
}
|
|
140
|
-
packages.push(await readMigrationPackage(entryPath));
|
|
141
|
-
}
|
|
142
|
-
return packages;
|
|
143
|
-
}
|
|
144
|
-
function formatMigrationDirName(timestamp, slug) {
|
|
145
|
-
const sanitized = slug.toLowerCase().replace(/[^a-z0-9]/g, "_").replace(/_+/g, "_").replace(/^_|_$/g, "");
|
|
146
|
-
if (sanitized.length === 0) throw errorInvalidSlug(slug);
|
|
147
|
-
const truncated = sanitized.slice(0, MAX_SLUG_LENGTH);
|
|
148
|
-
return `${timestamp.getUTCFullYear()}${String(timestamp.getUTCMonth() + 1).padStart(2, "0")}${String(timestamp.getUTCDate()).padStart(2, "0")}T${String(timestamp.getUTCHours()).padStart(2, "0")}${String(timestamp.getUTCMinutes()).padStart(2, "0")}_${truncated}`;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
//#endregion
|
|
152
|
-
export { writeMigrationManifest as a, readMigrationsDir as i, formatMigrationDirName as n, writeMigrationOps as o, readMigrationPackage as r, writeMigrationPackage as s, copyFilesWithRename as t };
|
|
153
|
-
//# sourceMappingURL=io-Cd6GLyjK.mjs.map
|
package/dist/io-Cd6GLyjK.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"io-Cd6GLyjK.mjs","names":["manifestRaw: string","opsRaw: string","manifest: MigrationManifest","ops: MigrationOps","entries: string[]","packages: MigrationBundle[]"],"sources":["../src/io.ts"],"sourcesContent":["import { copyFile, mkdir, readdir, readFile, stat, writeFile } from 'node:fs/promises';\nimport { type } from 'arktype';\nimport { basename, dirname, join } from 'pathe';\nimport {\n errorDirectoryExists,\n errorInvalidDestName,\n errorInvalidJson,\n errorInvalidManifest,\n errorInvalidSlug,\n errorMissingFile,\n} from './errors';\nimport type { MigrationBundle, MigrationManifest, MigrationOps } from './types';\n\nconst MANIFEST_FILE = 'migration.json';\nconst OPS_FILE = 'ops.json';\nconst MAX_SLUG_LENGTH = 64;\n\nfunction hasErrnoCode(error: unknown, code: string): boolean {\n return error instanceof Error && (error as { code?: string }).code === code;\n}\n\nconst MigrationHintsSchema = type({\n used: 'string[]',\n applied: 'string[]',\n plannerVersion: 'string',\n});\n\nconst MigrationManifestSchema = type({\n from: 'string',\n to: 'string',\n migrationId: 'string',\n kind: \"'regular' | 'baseline'\",\n fromContract: 'object | null',\n toContract: 'object',\n hints: MigrationHintsSchema,\n labels: 'string[]',\n 'authorship?': type({\n 'author?': 'string',\n 'email?': 'string',\n }),\n 'signature?': type({\n keyId: 'string',\n value: 'string',\n }).or('null'),\n createdAt: 'string',\n});\n\nconst MigrationOpSchema = type({\n id: 'string',\n label: 'string',\n operationClass: \"'additive' | 'widening' | 'destructive' | 'data'\",\n});\n\n// Intentionally shallow: operation-specific payload validation is owned by planner/runner layers.\nconst MigrationOpsSchema = MigrationOpSchema.array();\n\nexport async function writeMigrationPackage(\n dir: string,\n manifest: MigrationManifest,\n ops: MigrationOps,\n): Promise<void> {\n await mkdir(dirname(dir), { recursive: true });\n\n try {\n await mkdir(dir);\n } catch (error) {\n if (hasErrnoCode(error, 'EEXIST')) {\n throw errorDirectoryExists(dir);\n }\n throw error;\n }\n\n await writeFile(join(dir, MANIFEST_FILE), JSON.stringify(manifest, null, 2), { flag: 'wx' });\n await writeFile(join(dir, OPS_FILE), JSON.stringify(ops, null, 2), { flag: 'wx' });\n}\n\n/**\n * Copy a list of files into `destDir`, optionally renaming each one.\n *\n * The destination directory is created (with `recursive: true`) if it\n * does not already exist. Each source path is copied byte-for-byte into\n * `destDir/<destName>`; missing sources throw `ENOENT`. The helper is\n * intentionally generic: callers own the list of files (e.g. a contract\n * emitter's emitted output) and the naming convention (e.g. renaming\n * the destination contract to `end-contract.*` and the source contract\n * to `start-contract.*`).\n */\nexport async function copyFilesWithRename(\n destDir: string,\n files: readonly { readonly sourcePath: string; readonly destName: string }[],\n): Promise<void> {\n await mkdir(destDir, { recursive: true });\n for (const file of files) {\n if (basename(file.destName) !== file.destName) {\n throw errorInvalidDestName(file.destName);\n }\n await copyFile(file.sourcePath, join(destDir, file.destName));\n }\n}\n\nexport async function writeMigrationManifest(\n dir: string,\n manifest: MigrationManifest,\n): Promise<void> {\n await writeFile(join(dir, MANIFEST_FILE), `${JSON.stringify(manifest, null, 2)}\\n`);\n}\n\nexport async function writeMigrationOps(dir: string, ops: MigrationOps): Promise<void> {\n await writeFile(join(dir, OPS_FILE), `${JSON.stringify(ops, null, 2)}\\n`);\n}\n\nexport async function readMigrationPackage(dir: string): Promise<MigrationBundle> {\n const manifestPath = join(dir, MANIFEST_FILE);\n const opsPath = join(dir, OPS_FILE);\n\n let manifestRaw: string;\n try {\n manifestRaw = await readFile(manifestPath, 'utf-8');\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n throw errorMissingFile(MANIFEST_FILE, dir);\n }\n throw error;\n }\n\n let opsRaw: string;\n try {\n opsRaw = await readFile(opsPath, 'utf-8');\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n throw errorMissingFile(OPS_FILE, dir);\n }\n throw error;\n }\n\n let manifest: MigrationManifest;\n try {\n manifest = JSON.parse(manifestRaw);\n } catch (e) {\n throw errorInvalidJson(manifestPath, e instanceof Error ? e.message : String(e));\n }\n\n let ops: MigrationOps;\n try {\n ops = JSON.parse(opsRaw);\n } catch (e) {\n throw errorInvalidJson(opsPath, e instanceof Error ? e.message : String(e));\n }\n\n validateManifest(manifest, manifestPath);\n validateOps(ops, opsPath);\n\n return {\n dirName: basename(dir),\n dirPath: dir,\n manifest,\n ops,\n };\n}\n\nfunction validateManifest(\n manifest: unknown,\n filePath: string,\n): asserts manifest is MigrationManifest {\n const result = MigrationManifestSchema(manifest);\n if (result instanceof type.errors) {\n throw errorInvalidManifest(filePath, result.summary);\n }\n}\n\nfunction validateOps(ops: unknown, filePath: string): asserts ops is MigrationOps {\n const result = MigrationOpsSchema(ops);\n if (result instanceof type.errors) {\n throw errorInvalidManifest(filePath, result.summary);\n }\n}\n\nexport async function readMigrationsDir(\n migrationsRoot: string,\n): Promise<readonly MigrationBundle[]> {\n let entries: string[];\n try {\n entries = await readdir(migrationsRoot);\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n return [];\n }\n throw error;\n }\n\n const packages: MigrationBundle[] = [];\n\n for (const entry of entries.sort()) {\n const entryPath = join(migrationsRoot, entry);\n const entryStat = await stat(entryPath);\n if (!entryStat.isDirectory()) continue;\n\n const manifestPath = join(entryPath, MANIFEST_FILE);\n try {\n await stat(manifestPath);\n } catch {\n continue; // skip non-migration directories\n }\n\n packages.push(await readMigrationPackage(entryPath));\n }\n\n return packages;\n}\n\nexport function formatMigrationDirName(timestamp: Date, slug: string): string {\n const sanitized = slug\n .toLowerCase()\n .replace(/[^a-z0-9]/g, '_')\n .replace(/_+/g, '_')\n .replace(/^_|_$/g, '');\n\n if (sanitized.length === 0) {\n throw errorInvalidSlug(slug);\n }\n\n const truncated = sanitized.slice(0, MAX_SLUG_LENGTH);\n\n const y = timestamp.getUTCFullYear();\n const mo = String(timestamp.getUTCMonth() + 1).padStart(2, '0');\n const d = String(timestamp.getUTCDate()).padStart(2, '0');\n const h = String(timestamp.getUTCHours()).padStart(2, '0');\n const mi = String(timestamp.getUTCMinutes()).padStart(2, '0');\n\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AS,YAAY,KAAc,UAA+C;CAChF,MAAM,SAAS,mBAAmB,IAAI;AACtC,KAAI,kBAAkB,KAAK,OACzB,OAAM,qBAAqB,UAAU,OAAO,QAAQ;;AAIxD,eAAsB,kBACpB,gBACqC;CACrC,IAAIC;AACJ,KAAI;AACF,YAAU,MAAM,QAAQ,eAAe;UAChC,OAAO;AACd,MAAI,aAAa,OAAO,SAAS,CAC/B,QAAO,EAAE;AAEX,QAAM;;CAGR,MAAMC,WAA8B,EAAE;AAEtC,MAAK,MAAM,SAAS,QAAQ,MAAM,EAAE;EAClC,MAAM,YAAY,KAAK,gBAAgB,MAAM;AAE7C,MAAI,EADc,MAAM,KAAK,UAAU,EACxB,aAAa,CAAE;EAE9B,MAAM,eAAe,KAAK,WAAW,cAAc;AACnD,MAAI;AACF,SAAM,KAAK,aAAa;UAClB;AACN;;AAGF,WAAS,KAAK,MAAM,qBAAqB,UAAU,CAAC;;AAGtD,QAAO;;AAGT,SAAgB,uBAAuB,WAAiB,MAAsB;CAC5E,MAAM,YAAY,KACf,aAAa,CACb,QAAQ,cAAc,IAAI,CAC1B,QAAQ,OAAO,IAAI,CACnB,QAAQ,UAAU,GAAG;AAExB,KAAI,UAAU,WAAW,EACvB,OAAM,iBAAiB,KAAK;CAG9B,MAAM,YAAY,UAAU,MAAM,GAAG,gBAAgB;AAQrD,QAAO,GANG,UAAU,gBAAgB,GACzB,OAAO,UAAU,aAAa,GAAG,EAAE,CAAC,SAAS,GAAG,IAAI,GACrD,OAAO,UAAU,YAAY,CAAC,CAAC,SAAS,GAAG,IAAI,CAIpC,GAHX,OAAO,UAAU,aAAa,CAAC,CAAC,SAAS,GAAG,IAAI,GAC/C,OAAO,UAAU,eAAe,CAAC,CAAC,SAAS,GAAG,IAAI,CAE9B,GAAG"}
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { Contract } from "@prisma-next/contract/types";
|
|
2
|
-
import { MigrationPlanOperation } from "@prisma-next/framework-components/control";
|
|
3
|
-
|
|
4
|
-
//#region src/types.d.ts
|
|
5
|
-
interface MigrationHints {
|
|
6
|
-
readonly used: readonly string[];
|
|
7
|
-
readonly applied: readonly string[];
|
|
8
|
-
readonly plannerVersion: string;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* On-disk migration manifest. Every migration is content-addressed: the
|
|
12
|
-
* `migrationId` is a hash over the manifest envelope plus the operations
|
|
13
|
-
* list, computed at write time. There is no draft state — a migration
|
|
14
|
-
* directory either exists with a fully attested manifest or it does not.
|
|
15
|
-
*
|
|
16
|
-
* When the planner cannot lower an operation because of an unfilled
|
|
17
|
-
* `placeholder(...)` slot, the migration is still written with
|
|
18
|
-
* `migrationId` hashed over `ops: []`. Re-running self-emit after the
|
|
19
|
-
* user fills the placeholder produces a *different* `migrationId`
|
|
20
|
-
* (committed to the real ops); this is intentional.
|
|
21
|
-
*/
|
|
22
|
-
interface MigrationManifest {
|
|
23
|
-
readonly migrationId: string;
|
|
24
|
-
readonly from: string;
|
|
25
|
-
readonly to: string;
|
|
26
|
-
readonly kind: 'regular' | 'baseline';
|
|
27
|
-
readonly fromContract: Contract | null;
|
|
28
|
-
readonly toContract: Contract;
|
|
29
|
-
readonly hints: MigrationHints;
|
|
30
|
-
readonly labels: readonly string[];
|
|
31
|
-
readonly authorship?: {
|
|
32
|
-
readonly author?: string;
|
|
33
|
-
readonly email?: string;
|
|
34
|
-
};
|
|
35
|
-
readonly signature?: {
|
|
36
|
-
readonly keyId: string;
|
|
37
|
-
readonly value: string;
|
|
38
|
-
} | null;
|
|
39
|
-
readonly createdAt: string;
|
|
40
|
-
}
|
|
41
|
-
type MigrationOps = readonly MigrationPlanOperation[];
|
|
42
|
-
/**
|
|
43
|
-
* An on-disk migration directory containing a manifest and operations.
|
|
44
|
-
*/
|
|
45
|
-
interface MigrationBundle {
|
|
46
|
-
readonly dirName: string;
|
|
47
|
-
readonly dirPath: string;
|
|
48
|
-
readonly manifest: MigrationManifest;
|
|
49
|
-
readonly ops: MigrationOps;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* An entry in the migration graph. All on-disk migrations are attested,
|
|
53
|
-
* so `migrationId` is always a string.
|
|
54
|
-
*/
|
|
55
|
-
interface MigrationChainEntry {
|
|
56
|
-
readonly from: string;
|
|
57
|
-
readonly to: string;
|
|
58
|
-
readonly migrationId: string;
|
|
59
|
-
readonly dirName: string;
|
|
60
|
-
readonly createdAt: string;
|
|
61
|
-
readonly labels: readonly string[];
|
|
62
|
-
}
|
|
63
|
-
interface MigrationGraph {
|
|
64
|
-
readonly nodes: ReadonlySet<string>;
|
|
65
|
-
readonly forwardChain: ReadonlyMap<string, readonly MigrationChainEntry[]>;
|
|
66
|
-
readonly reverseChain: ReadonlyMap<string, readonly MigrationChainEntry[]>;
|
|
67
|
-
readonly migrationById: ReadonlyMap<string, MigrationChainEntry>;
|
|
68
|
-
}
|
|
69
|
-
//#endregion
|
|
70
|
-
export { MigrationManifest as a, MigrationHints as i, MigrationChainEntry as n, MigrationOps as o, MigrationGraph as r, MigrationBundle as t };
|
|
71
|
-
//# sourceMappingURL=types-DyGXcWWp.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types-DyGXcWWp.d.mts","names":[],"sources":["../src/types.ts"],"sourcesContent":[],"mappings":";;;;UAGiB,cAAA;;EAAA,SAAA,OAAA,EAAc,SAAA,MAAA,EAAA;EAkBd,SAAA,cAAiB,EAAA,MAAA;;;;;AAclC;AAKA;AAWA;AASA;;;;;;AAI8C,UA3C7B,iBAAA,CA2C6B;EAApB,SAAA,WAAA,EAAA,MAAA;EAAW,SAAA,IAAA,EAAA,MAAA;;;yBAtCZ;uBACF;kBACL;;;;;;;;;;;;KAON,YAAA,YAAwB;;;;UAKnB,eAAA;;;qBAGI;gBACL;;;;;;UAOC,mBAAA;;;;;;;;UASA,cAAA;kBACC;yBACO,6BAA6B;yBAC7B,6BAA6B;0BAC5B,oBAAoB"}
|
package/src/attestation.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { createHash } from 'node:crypto';
|
|
2
|
-
import { canonicalizeJson } from './canonicalize-json';
|
|
3
|
-
import { readMigrationPackage } from './io';
|
|
4
|
-
import type { MigrationBundle, MigrationManifest, MigrationOps } from './types';
|
|
5
|
-
|
|
6
|
-
export interface VerifyResult {
|
|
7
|
-
readonly ok: boolean;
|
|
8
|
-
readonly reason?: 'mismatch';
|
|
9
|
-
readonly storedMigrationId?: string;
|
|
10
|
-
readonly computedMigrationId?: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function sha256Hex(input: string): string {
|
|
14
|
-
return createHash('sha256').update(input).digest('hex');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Content-addressed migration identity over (manifest envelope sans
|
|
19
|
-
* contracts/hints, ops). See ADR 199 "Storage-only migration identity"
|
|
20
|
-
* for the rationale: contracts are anchored separately by the
|
|
21
|
-
* storage-hash bookends inside the envelope; planner hints are advisory
|
|
22
|
-
* and must not affect identity.
|
|
23
|
-
*
|
|
24
|
-
* The `migrationId` field on the manifest is stripped before hashing so
|
|
25
|
-
* the function can be used both at write time (when no id exists yet)
|
|
26
|
-
* and at verify time (rehashing an already-attested manifest).
|
|
27
|
-
*/
|
|
28
|
-
export function computeMigrationId(
|
|
29
|
-
manifest: Omit<MigrationManifest, 'migrationId'> & { readonly migrationId?: string },
|
|
30
|
-
ops: MigrationOps,
|
|
31
|
-
): string {
|
|
32
|
-
const {
|
|
33
|
-
migrationId: _migrationId,
|
|
34
|
-
signature: _signature,
|
|
35
|
-
fromContract: _fromContract,
|
|
36
|
-
toContract: _toContract,
|
|
37
|
-
hints: _hints,
|
|
38
|
-
...strippedMeta
|
|
39
|
-
} = manifest;
|
|
40
|
-
|
|
41
|
-
const canonicalManifest = canonicalizeJson(strippedMeta);
|
|
42
|
-
const canonicalOps = canonicalizeJson(ops);
|
|
43
|
-
|
|
44
|
-
const partHashes = [canonicalManifest, canonicalOps].map(sha256Hex);
|
|
45
|
-
const hash = sha256Hex(canonicalizeJson(partHashes));
|
|
46
|
-
|
|
47
|
-
return `sha256:${hash}`;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Re-hash an on-disk migration bundle and compare against the stored
|
|
52
|
-
* `migrationId`. Returns `{ ok: true }` when the package is internally
|
|
53
|
-
* consistent (manifest + ops still produce the recorded id), or
|
|
54
|
-
* `{ ok: false, reason: 'mismatch', stored, computed }` when they do
|
|
55
|
-
* not — typically a sign of FS corruption, partial writes, or a
|
|
56
|
-
* post-emit hand edit.
|
|
57
|
-
*/
|
|
58
|
-
export function verifyMigrationBundle(bundle: MigrationBundle): VerifyResult {
|
|
59
|
-
const computed = computeMigrationId(bundle.manifest, bundle.ops);
|
|
60
|
-
|
|
61
|
-
if (bundle.manifest.migrationId === computed) {
|
|
62
|
-
return {
|
|
63
|
-
ok: true,
|
|
64
|
-
storedMigrationId: bundle.manifest.migrationId,
|
|
65
|
-
computedMigrationId: computed,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return {
|
|
70
|
-
ok: false,
|
|
71
|
-
reason: 'mismatch',
|
|
72
|
-
storedMigrationId: bundle.manifest.migrationId,
|
|
73
|
-
computedMigrationId: computed,
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/** Convenience wrapper: read the package from disk then verify it. */
|
|
78
|
-
export async function verifyMigration(dir: string): Promise<VerifyResult> {
|
|
79
|
-
const pkg = await readMigrationPackage(dir);
|
|
80
|
-
return verifyMigrationBundle(pkg);
|
|
81
|
-
}
|