@prisma-next/migration-tools 0.5.0 → 0.5.1

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.
@@ -1,7 +1,6 @@
1
1
  import type { Contract } from '@prisma-next/contract/types';
2
2
  import { notOk, ok, type Result } from '@prisma-next/utils/result';
3
3
  import { EMPTY_CONTRACT_HASH } from '../constants';
4
- import { detectSpaceContractDrift } from '../detect-space-contract-drift';
5
4
  import { readMigrationsDir } from '../io';
6
5
  import { reconstructGraph } from '../migration-graph';
7
6
  import type { OnDiskMigrationPackage } from '../package';
@@ -11,18 +10,6 @@ import { APP_SPACE_ID, spaceMigrationDirectory } from '../space-layout';
11
10
  import { listContractSpaceDirectories } from '../verify-contract-spaces';
12
11
  import type { ContractSpaceAggregate, ContractSpaceMember, HydratedMigrationGraph } from './types';
13
12
 
14
- /**
15
- * Hash function used by drift detection. Defaults to a canonical-JSON +
16
- * SHA-256 pipeline that mirrors the framework's contract-hash convention,
17
- * but the loader accepts a callback so SQL-family callers can pass their
18
- * `coreHash` / `storageHash` derivation through unchanged.
19
- *
20
- * The contract value passed in is the framework-neutral `unknown` form;
21
- * callers that have already validated typed contracts can simply hand
22
- * the validated value back through.
23
- */
24
- export type AggregateContractHasher = (contract: unknown) => string;
25
-
26
13
  /**
27
14
  * Single declared extension entry the loader needs from `Config.extensionPacks`.
28
15
  *
@@ -32,20 +19,20 @@ export type AggregateContractHasher = (contract: unknown) => string;
32
19
  * - `targetId` — the configured `Config.adapter.targetId` value the
33
20
  * declaring extension declared. The loader rejects mismatches against
34
21
  * the aggregate's `targetId` with `targetMismatch`.
35
- * - `contractSpace` — present iff the descriptor declares a contract
36
- * space (extensions can ship without one and remain runtime-only /
37
- * codec-only). Drift detection compares the descriptor's
38
- * `contractJson` hash against the on-disk on-disk head hash; the loader
39
- * rejects drift fatally.
22
+ *
23
+ * Whether the descriptor declares a contract space is decided by whether
24
+ * its corresponding `migrations/<id>/` directory exists on disk
25
+ * (materialised by the seed phase before the loader runs); the loader
26
+ * never reads the descriptor's `contractJson` itself. That makes the
27
+ * aggregate's apply / verify paths byte-for-byte independent of the
28
+ * descriptor module — `db verify` succeeds even if the descriptor's
29
+ * `contractJson` is a throwing getter.
40
30
  *
41
31
  * Typed structurally so the migration-tools layer stays framework-neutral.
42
32
  */
43
33
  export interface DeclaredExtensionEntry {
44
34
  readonly id: string;
45
35
  readonly targetId: string;
46
- readonly contractSpace?: {
47
- readonly contractJson: unknown;
48
- };
49
36
  }
50
37
 
51
38
  /**
@@ -63,7 +50,6 @@ export interface LoadAggregateInput {
63
50
  readonly appContract: Contract;
64
51
  readonly declaredExtensions: ReadonlyArray<DeclaredExtensionEntry>;
65
52
  readonly validateContract: (contractJson: unknown) => Contract;
66
- readonly hashContract: AggregateContractHasher;
67
53
  /**
68
54
  * Hydrated migration graph for the **app member**.
69
55
  *
@@ -90,12 +76,6 @@ export type LoadAggregateError =
90
76
  | { readonly kind: 'layoutViolation'; readonly violations: readonly LayoutViolation[] }
91
77
  | { readonly kind: 'integrityFailure'; readonly spaceId: string; readonly detail: string }
92
78
  | { readonly kind: 'validationFailure'; readonly spaceId: string; readonly detail: string }
93
- | {
94
- readonly kind: 'driftViolation';
95
- readonly spaceId: string;
96
- readonly priorHeadHash: string;
97
- readonly liveHash: string;
98
- }
99
79
  | {
100
80
  readonly kind: 'disjointnessViolation';
101
81
  readonly element: string;
@@ -139,20 +119,22 @@ interface LoadedExtensionState {
139
119
 
140
120
  /**
141
121
  * Hydrate a {@link ContractSpaceAggregate} from on-disk state and
142
- * caller-provided descriptor data.
122
+ * the app contract value the caller supplies.
143
123
  *
144
- * This is the **only** descriptor-import boundary in the post-M2.5
145
- * pipeline: callers read `extensionPacks` from `Config`, validate the
146
- * app contract, and pass everything through. The loader composes
147
- * existing migration-tools primitives layout precheck (via
124
+ * The loader is the **only** descriptor-import boundary at apply /
125
+ * verify time, but it intentionally does **not** read the extension
126
+ * descriptor's `contractJson` value. Each extension space's contract
127
+ * is read from its on-disk `migrations/<id>/contract.json` mirror; the
128
+ * descriptor's role is exhausted by the seed phase that wrote that
129
+ * mirror in the first place. The loader composes existing
130
+ * migration-tools primitives — layout precheck (via
148
131
  * {@link listContractSpaceDirectories}), integrity checks (via
149
132
  * {@link readMigrationsDir} / {@link readContractSpaceHeadRef} /
150
- * {@link readContractSpaceContract} / `validateContract`), drift detection
151
- * (via {@link detectSpaceContractDrift}), and disjointness — into a
152
- * single typed value.
133
+ * {@link readContractSpaceContract} / `validateContract`), and
134
+ * disjointness — into a single typed value.
153
135
  *
154
136
  * Failure semantics: every failure variant in {@link LoadAggregateError}
155
- * short-circuits the load. Drift is fatal (M2.5 spec § Loader, step 5).
137
+ * short-circuits the load.
156
138
  */
157
139
  export async function loadContractSpaceAggregate(
158
140
  input: LoadAggregateInput,
@@ -180,8 +162,14 @@ export async function loadContractSpaceAggregate(
180
162
  }
181
163
 
182
164
  // 2. Layout precheck: bundle every layout offence at once.
183
- const declaredWithSpace = input.declaredExtensions.filter((e) => e.contractSpace !== undefined);
184
- const declaredSpaceIds = new Set(declaredWithSpace.map((e) => e.id));
165
+ //
166
+ // Every declared extension contributes an entry to the aggregate when
167
+ // a corresponding `migrations/<id>/` directory exists on disk. The
168
+ // loader treats the directory's presence as the membership signal —
169
+ // the descriptor itself is not read — so codec-only extensions (no
170
+ // on-disk dir) and contract-space extensions (dir present) are
171
+ // distinguished structurally.
172
+ const declaredSpaceIds = new Set(input.declaredExtensions.map((e) => e.id));
185
173
  const allDirs = await listContractSpaceDirectories(input.migrationsDir);
186
174
  // The app member is implicitly declared (it is always part of the
187
175
  // aggregate); its `migrations/<APP_SPACE_ID>/` directory may exist or
@@ -206,9 +194,9 @@ export async function loadContractSpaceAggregate(
206
194
  return notOk({ kind: 'layoutViolation', violations: layoutViolations });
207
195
  }
208
196
 
209
- // 3-5. Per-extension: read + validate + integrity-check + drift.
197
+ // 3-5. Per-extension: read + validate + integrity-check.
210
198
  const loadedExtensions: LoadedExtensionState[] = [];
211
- for (const entry of [...declaredWithSpace].sort((a, b) => a.id.localeCompare(b.id))) {
199
+ for (const entry of [...input.declaredExtensions].sort((a, b) => a.id.localeCompare(b.id))) {
212
200
  const headRef = await readContractSpaceHeadRef(input.migrationsDir, entry.id);
213
201
  if (headRef === null) {
214
202
  return notOk({
@@ -249,24 +237,6 @@ export async function loadContractSpaceAggregate(
249
237
  });
250
238
  }
251
239
 
252
- // Drift: compare descriptor's live `contractJson` to on-disk
253
- // `refs/head.json.hash`.
254
- if (entry.contractSpace) {
255
- const liveHash = input.hashContract(entry.contractSpace.contractJson);
256
- const drift = detectSpaceContractDrift(entry.id, {
257
- descriptorHash: liveHash,
258
- priorHeadHash: headRef.hash,
259
- });
260
- if (drift.kind === 'drift') {
261
- return notOk({
262
- kind: 'driftViolation',
263
- spaceId: entry.id,
264
- priorHeadHash: drift.priorHeadHash ?? '',
265
- liveHash: drift.descriptorHash,
266
- });
267
- }
268
- }
269
-
270
240
  // Read + integrity-check the migration packages. `readMigrationsDir`
271
241
  // re-derives `providedInvariants` and verifies migrationHash for
272
242
  // every package.
@@ -11,6 +11,7 @@ import type { ContractSpaceMember } from './types';
11
11
 
12
12
  export type {
13
13
  AggregateCurrentDBState,
14
+ AggregateMigrationEdgeRef,
14
15
  AggregatePerSpacePlan,
15
16
  AggregatePlannerError,
16
17
  AggregatePlannerInput,
@@ -0,0 +1,63 @@
1
+ import type { Contract } from '@prisma-next/contract/types';
2
+ import type {
3
+ ContractSpace,
4
+ ContractSpaceHeadRef,
5
+ MigrationPackage,
6
+ MigrationPlanOperation,
7
+ } from '@prisma-next/framework-components/control';
8
+ import type { MigrationMetadata } from './metadata';
9
+
10
+ /**
11
+ * Materialise a typed {@link ContractSpace} from the JSON artefacts a
12
+ * contract-space extension package emits to disk.
13
+ *
14
+ * Extension descriptors wire `contract.json`, per-migration
15
+ * `migration.json` / `ops.json`, and `refs/head.json` to the framework's
16
+ * typed surfaces. TypeScript widens JSON imports to a structural record
17
+ * that does not preserve readonly modifiers or branded scalars (e.g.
18
+ * `StorageHashBase<'sha256:...'>`), so authoring the descriptor inline
19
+ * forces every wiring site to cast through `unknown`. This helper
20
+ * encapsulates the single narrowing point: descriptor sources stay
21
+ * cast-free, and the (necessary) coercion is colocated with the
22
+ * documentation explaining why it is safe.
23
+ *
24
+ * Safety: the JSON files passed here are produced by the framework's own
25
+ * emit pipeline (`prisma-next contract emit` and `MigrationCLI.run`)
26
+ * and re-validated downstream by the runner / verifier. The descriptor
27
+ * is a pass-through wiring layer — no descriptor consumer treats the
28
+ * narrowed types as a stronger guarantee than "these came from the
29
+ * canonical emit pipeline".
30
+ *
31
+ * The helper does not introspect or schema-validate the inputs; runtime
32
+ * validation is the responsibility of `validateContract` (codec-aware,
33
+ * called by `family.validateContract` at control-stack construction)
34
+ * and the per-migration `readMigrationPackage` reader used when loading
35
+ * from disk. JSON-imported packages flow through the descriptor without
36
+ * a disk read, so the equivalent runtime guarantee comes from the emit
37
+ * pipeline that produced the JSON in the first place.
38
+ */
39
+ export function contractSpaceFromJson<TContract extends Contract = Contract>(inputs: {
40
+ readonly contractJson: unknown;
41
+ readonly migrations: ReadonlyArray<{
42
+ readonly dirName: string;
43
+ readonly metadata: unknown;
44
+ readonly ops: unknown;
45
+ }>;
46
+ readonly headRef: ContractSpaceHeadRef;
47
+ }): ContractSpace<TContract> {
48
+ // The narrowing happens once, here. Casting via `unknown` rather than a
49
+ // direct cast preserves TS's structural soundness checks for the
50
+ // inputs (they must be assignable to `unknown`, which is trivial); the
51
+ // resulting type is the family-specific Contract / MigrationPackage
52
+ // surface descriptors publish.
53
+ const migrations: readonly MigrationPackage[] = inputs.migrations.map((m) => ({
54
+ dirName: m.dirName,
55
+ metadata: m.metadata as MigrationMetadata,
56
+ ops: m.ops as readonly MigrationPlanOperation[],
57
+ }));
58
+ return {
59
+ contractJson: inputs.contractJson as TContract,
60
+ migrations,
61
+ headRef: inputs.headRef,
62
+ };
63
+ }
@@ -1,5 +1,4 @@
1
1
  export {
2
- type AggregateContractHasher,
3
2
  type DeclaredExtensionEntry,
4
3
  type LayoutViolation,
5
4
  type LoadAggregateError,
@@ -10,6 +9,7 @@ export {
10
9
  export type { ContractMarkerRecordLike } from '../aggregate/marker-types';
11
10
  export {
12
11
  type AggregateCurrentDBState,
12
+ type AggregateMigrationEdgeRef,
13
13
  type AggregatePerSpacePlan,
14
14
  type AggregatePlannerError,
15
15
  type AggregatePlannerInput,
@@ -8,11 +8,7 @@ export {
8
8
  type ExtensionSpaceApplyPathOutcome,
9
9
  } from '../compute-extension-space-apply-path';
10
10
  export type { SpaceApplyInput } from '../concatenate-space-apply-inputs';
11
- export {
12
- type DetectSpaceContractDriftInputs,
13
- detectSpaceContractDrift,
14
- type SpaceContractDriftResult,
15
- } from '../detect-space-contract-drift';
11
+ export { contractSpaceFromJson } from '../contract-space-from-json';
16
12
  export {
17
13
  type ContractSpaceArtefactInputs,
18
14
  emitContractSpaceArtefacts,
package/src/invariants.ts CHANGED
@@ -32,6 +32,11 @@ export function validateInvariantId(invariantId: string): boolean {
32
32
  *
33
33
  * Throws `MIGRATION.INVALID_INVARIANT_ID` on a malformed id and
34
34
  * `MIGRATION.DUPLICATE_INVARIANT_IN_EDGE` on duplicates.
35
+ *
36
+ * @see docs/architecture docs/adrs/ADR 212 - Contract spaces.md
37
+ * — extension migrations carry `invariantId`s on additive ops; e.g.
38
+ * cipherstash's `installEqlBundle` and structural `create-*` ops are
39
+ * additive-class but carry `cipherstash:*` invariantIds.
35
40
  */
36
41
  export function deriveProvidedInvariants(ops: MigrationOps): readonly string[] {
37
42
  const seen = new Set<string>();
@@ -1 +0,0 @@
1
- {"version":3,"file":"invariants-Duc8f9NM.mjs","names":[],"sources":["../src/invariants.ts"],"sourcesContent":["import type { MigrationPlanOperation } from '@prisma-next/framework-components/control';\nimport { errorDuplicateInvariantInEdge, errorInvalidInvariantId } from './errors';\nimport type { MigrationOps } from './package';\n\n/**\n * Hygiene check for `invariantId`. Rejects empty values plus any\n * whitespace or control character (including Unicode whitespace like\n * NBSP and em space, which are visually identical to ASCII space and\n * routinely sneak in via paste).\n */\nexport function validateInvariantId(invariantId: string): boolean {\n if (invariantId.length === 0) return false;\n return !/[\\p{Cc}\\p{White_Space}]/u.test(invariantId);\n}\n\n/**\n * Walk a migration's operations and produce its `providedInvariants`\n * aggregate: the sorted, deduplicated list of `invariantId`s declared\n * by ops in the migration. Ops without an `invariantId` are skipped.\n *\n * Both `data`-class ops (data-transforms, e.g. backfills) and\n * `additive`-class opaque DDL (e.g. cipherstash's vendored EQL bundle\n * via `installEqlBundleOp`) may declare invariantIds: the\n * `operationClass` axis classifies *policy gating* (which kinds of ops\n * a `db init` / `db update` policy permits), while `invariantId`\n * classifies *marker bookkeeping* (which named bundles of work a\n * future regeneration knows to skip). The two concerns are\n * intentionally orthogonal — an extension can ship additive\n * non-IR-derivable DDL (the only way the planner can know the bundle\n * is already applied is via the invariantId on the marker) without\n * needing to mis-classify it as `data`-class.\n *\n * Throws `MIGRATION.INVALID_INVARIANT_ID` on a malformed id and\n * `MIGRATION.DUPLICATE_INVARIANT_IN_EDGE` on duplicates.\n */\nexport function deriveProvidedInvariants(ops: MigrationOps): readonly string[] {\n const seen = new Set<string>();\n for (const op of ops) {\n const invariantId = readInvariantId(op);\n if (invariantId === undefined) continue;\n if (!validateInvariantId(invariantId)) {\n throw errorInvalidInvariantId(invariantId);\n }\n if (seen.has(invariantId)) {\n throw errorDuplicateInvariantInEdge(invariantId);\n }\n seen.add(invariantId);\n }\n return [...seen].sort();\n}\n\nfunction readInvariantId(op: MigrationPlanOperation): string | undefined {\n if (!Object.hasOwn(op, 'invariantId')) return undefined;\n const candidate = (op as { invariantId?: unknown }).invariantId;\n return typeof candidate === 'string' ? candidate : undefined;\n}\n"],"mappings":";;;;;;;;AAUA,SAAgB,oBAAoB,aAA8B;CAChE,IAAI,YAAY,WAAW,GAAG,OAAO;CACrC,OAAO,CAAC,2BAA2B,KAAK,YAAY;;;;;;;;;;;;;;;;;;;;;;AAuBtD,SAAgB,yBAAyB,KAAsC;CAC7E,MAAM,uBAAO,IAAI,KAAa;CAC9B,KAAK,MAAM,MAAM,KAAK;EACpB,MAAM,cAAc,gBAAgB,GAAG;EACvC,IAAI,gBAAgB,KAAA,GAAW;EAC/B,IAAI,CAAC,oBAAoB,YAAY,EACnC,MAAM,wBAAwB,YAAY;EAE5C,IAAI,KAAK,IAAI,YAAY,EACvB,MAAM,8BAA8B,YAAY;EAElD,KAAK,IAAI,YAAY;;CAEvB,OAAO,CAAC,GAAG,KAAK,CAAC,MAAM;;AAGzB,SAAS,gBAAgB,IAAgD;CACvE,IAAI,CAAC,OAAO,OAAO,IAAI,cAAc,EAAE,OAAO,KAAA;CAC9C,MAAM,YAAa,GAAiC;CACpD,OAAO,OAAO,cAAc,WAAW,YAAY,KAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"read-contract-space-contract-C3-1eyaI.mjs","names":["hasErrnoCode","hasErrnoCode"],"sources":["../src/space-layout.ts","../src/read-contract-space-head-ref.ts","../src/detect-space-contract-drift.ts","../src/verify-contract-spaces.ts","../src/read-contract-space-contract.ts"],"sourcesContent":["import { APP_SPACE_ID } from '@prisma-next/framework-components/control';\nimport { join } from 'pathe';\nimport { errorInvalidSpaceId } from './errors';\n\nexport { APP_SPACE_ID };\n\n/**\n * Branded string carrying a compile-time guarantee that the value has\n * been validated by {@link assertValidSpaceId}. Downstream filesystem\n * helpers (e.g. {@link spaceMigrationDirectory}) accept this type to\n * make \"validated\" tracking visible at the type level rather than\n * relying purely on a runtime check.\n */\nexport type ValidSpaceId = string & { readonly __brand: 'ValidSpaceId' };\n\n/**\n * Pattern a contract-space identifier must match. The constraint is\n * filesystem-friendly: lowercase letters / digits / hyphen / underscore,\n * starts with a letter, max 64 characters.\n */\nconst SPACE_ID_PATTERN = /^[a-z][a-z0-9_-]{0,63}$/;\n\nexport function isValidSpaceId(spaceId: string): spaceId is ValidSpaceId {\n return SPACE_ID_PATTERN.test(spaceId);\n}\n\nexport function assertValidSpaceId(spaceId: string): asserts spaceId is ValidSpaceId {\n if (!isValidSpaceId(spaceId)) {\n throw errorInvalidSpaceId(spaceId);\n }\n}\n\n/**\n * Resolve the migrations subdirectory for a given contract space.\n *\n * Every contract space — including the app space (default `'app'`) —\n * lands under `<projectMigrationsDir>/<spaceId>/`. The space id is\n * validated against {@link SPACE_ID_PATTERN} because it becomes a\n * filesystem directory name verbatim.\n *\n * `projectMigrationsDir` is the project's top-level `migrations/`\n * directory; the helper does not assume anything about its absolute /\n * relative shape and is symmetric with `pathe.join`.\n */\nexport function spaceMigrationDirectory(projectMigrationsDir: string, spaceId: string): string {\n assertValidSpaceId(spaceId);\n return join(projectMigrationsDir, spaceId);\n}\n","import { readFile } from 'node:fs/promises';\nimport type { ContractSpaceHeadRef } from '@prisma-next/framework-components/control';\nimport { join } from 'pathe';\nimport { errorInvalidJson, errorInvalidRefFile } from './errors';\nimport { assertValidSpaceId } from './space-layout';\n\nexport type { ContractSpaceHeadRef };\n\nfunction hasErrnoCode(error: unknown, code: string): boolean {\n return error instanceof Error && (error as { code?: string }).code === code;\n}\n\n/**\n * Read the head ref (`hash` + `invariants`) for a contract space from\n * `<projectMigrationsDir>/<spaceId>/refs/head.json`.\n *\n * Returns `null` when the file does not exist (first emit). Surfaces\n * `MIGRATION.INVALID_JSON` / `MIGRATION.INVALID_REF_FILE` on a corrupt\n * `refs/head.json` so callers can distinguish \"no head ref on disk\"\n * (returns `null`) from \"head ref present but unreadable\" (throws).\n *\n * Validates the space id against `[a-z][a-z0-9_-]{0,63}` for the same\n * filesystem-safety reasons as the rest of the per-space helpers. The\n * helper is uniform across the app and extension spaces.\n */\nexport async function readContractSpaceHeadRef(\n projectMigrationsDir: string,\n spaceId: string,\n): Promise<ContractSpaceHeadRef | null> {\n assertValidSpaceId(spaceId);\n\n const filePath = join(projectMigrationsDir, spaceId, 'refs', 'head.json');\n\n let raw: string;\n try {\n raw = await readFile(filePath, 'utf-8');\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n return null;\n }\n throw error;\n }\n\n let parsed: unknown;\n try {\n parsed = JSON.parse(raw);\n } catch (e) {\n throw errorInvalidJson(filePath, e instanceof Error ? e.message : String(e));\n }\n\n if (typeof parsed !== 'object' || parsed === null) {\n throw errorInvalidRefFile(filePath, 'expected an object');\n }\n const obj = parsed as { hash?: unknown; invariants?: unknown };\n if (typeof obj.hash !== 'string') {\n throw errorInvalidRefFile(filePath, 'expected an object with a string `hash` field');\n }\n if (!Array.isArray(obj.invariants) || obj.invariants.some((value) => typeof value !== 'string')) {\n throw errorInvalidRefFile(filePath, 'expected an object with an `invariants` array of strings');\n }\n\n return { hash: obj.hash, invariants: obj.invariants as readonly string[] };\n}\n","/**\n * Inputs for {@link detectSpaceContractDrift}.\n *\n * Both hashes are produced by the caller (the SQL-family wiring at the\n * consumption site) using the canonical contract hashing pipeline.\n * Keeping the helper pure lets `migration-tools` stay framework-neutral\n * — the SQL family already speaks `Contract<SqlStorage>`, the Mongo\n * family speaks its own contract type, and both reduce to a hash string\n * before drift detection runs.\n *\n * `priorHeadHash` is `null` when no `contract.json` exists yet on disk for\n * the space (the descriptor declares an extension that has never been\n * emitted into the user's repo). That's the \"first emit\" case — no\n * drift to surface; the migrate emit will create the on-disk artefacts.\n */\nexport interface DetectSpaceContractDriftInputs {\n readonly descriptorHash: string;\n readonly priorHeadHash: string | null;\n}\n\n/**\n * Result discriminant for {@link detectSpaceContractDrift}.\n *\n * - `noDrift`: descriptor hash and on-disk head hash agree byte-for-byte.\n * The migrate emit can proceed with no warning.\n * - `firstEmit`: no on-disk `contract.json` on disk yet. The extension\n * was just added to `extensionPacks`; this run will create the\n * on-disk artefacts. No warning either — the user's intent is to install\n * the extension, not to \"drift\" from a state they haven't recorded.\n * - `drift`: descriptor hash differs from on-disk head hash. The caller\n * surfaces a non-fatal warning naming the extension and the\n * diff direction (descriptor → on-disk head). The migrate emit proceeds\n * normally so the bump is materialised this run; the warning just\n * confirms the bump is being captured.\n *\n * `spaceId`, `descriptorHash`, and `priorHeadHash` are threaded through\n * verbatim so the caller (logger / TerminalUI / strict-mode envelope)\n * has everything it needs to format the warning message without\n * re-reading the descriptor or the on-disk artefact.\n */\nexport type SpaceContractDriftResult = {\n readonly kind: 'noDrift' | 'firstEmit' | 'drift';\n readonly spaceId: string;\n readonly descriptorHash: string;\n readonly priorHeadHash: string | null;\n};\n\n/**\n * Pure drift-detection primitive for a single contract space.\n *\n * Runs once per loaded extension space, just before computing the\n * `priorContract` that feeds {@link import('./plan-all-spaces').planAllSpaces}.\n * Hash equality is byte-for-byte (no normalisation) — both sides are\n * already canonical hashes produced by the same pipeline, so any\n * difference is meaningful drift.\n *\n * Synchronous, pure, no I/O. The caller (SQL family) reads the on-disk\n * `contract.json` and computes its hash, then invokes this helper\n * alongside the descriptor's `headRef.hash`. Composes naturally with\n * {@link import('./read-contract-space-head-ref').readContractSpaceHeadRef}\n * which provides the read-side primitive.\n *\n * The drift warning surfaces the extension name and the diff direction.\n */\nexport function detectSpaceContractDrift(\n spaceId: string,\n inputs: DetectSpaceContractDriftInputs,\n): SpaceContractDriftResult {\n if (inputs.priorHeadHash === null) {\n return {\n kind: 'firstEmit',\n spaceId,\n descriptorHash: inputs.descriptorHash,\n priorHeadHash: null,\n };\n }\n if (inputs.descriptorHash === inputs.priorHeadHash) {\n return {\n kind: 'noDrift',\n spaceId,\n descriptorHash: inputs.descriptorHash,\n priorHeadHash: inputs.priorHeadHash,\n };\n }\n return {\n kind: 'drift',\n spaceId,\n descriptorHash: inputs.descriptorHash,\n priorHeadHash: inputs.priorHeadHash,\n };\n}\n","import { readdir, stat } from 'node:fs/promises';\nimport { join } from 'pathe';\nimport { MANIFEST_FILE } from './io';\nimport { APP_SPACE_ID } from './space-layout';\n\nfunction hasErrnoCode(error: unknown, code: string): boolean {\n return error instanceof Error && (error as { code?: string }).code === code;\n}\n\n/**\n * List the per-space subdirectories under\n * `<projectRoot>/migrations/`. Returns space-id directory names (sorted\n * alphabetically) — i.e. any non-dot-prefixed subdirectory whose root\n * does **not** contain a `migration.json` manifest. The manifest is the\n * structural marker of a user-authored migration directory (see\n * `readMigrationsDir` in `./io`); directory names themselves belong to\n * the user and are not part of the contract.\n *\n * Returns `[]` if the migrations directory does not exist (greenfield\n * project).\n *\n * Reads only the user's repo. **No descriptor import.** The caller\n * (verifier) feeds the result into {@link verifyContractSpaces} alongside\n * the loaded-space set and the marker rows.\n */\nexport async function listContractSpaceDirectories(\n projectMigrationsDir: string,\n): Promise<readonly string[]> {\n let entries: { readonly name: string; readonly isDirectory: boolean }[];\n try {\n const dirents = await readdir(projectMigrationsDir, { withFileTypes: true });\n entries = dirents.map((d) => ({ name: d.name, isDirectory: d.isDirectory() }));\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n return [];\n }\n throw error;\n }\n\n const namedCandidates = entries\n .filter((e) => e.isDirectory)\n .map((e) => e.name)\n .filter((name) => !name.startsWith('.'))\n .sort();\n\n const manifestChecks = await Promise.all(\n namedCandidates.map(async (name) => {\n try {\n await stat(join(projectMigrationsDir, name, MANIFEST_FILE));\n return { name, isMigrationDir: true };\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n return { name, isMigrationDir: false };\n }\n throw error;\n }\n }),\n );\n\n return manifestChecks.filter((c) => !c.isMigrationDir).map((c) => c.name);\n}\n\n/**\n * On-disk head value (`(hash, invariants)`) for one contract space.\n * The verifier compares this against the marker row for the same space\n * to detect drift between the user-emitted artefacts and the live DB\n * marker.\n */\nexport interface ContractSpaceHeadRecord {\n readonly hash: string;\n readonly invariants: readonly string[];\n}\n\n/**\n * Marker row read from `prisma_contract.marker` (one per `space`).\n * Caller resolves these via the family runtime's marker reader before\n * invoking {@link verifyContractSpaces}.\n */\nexport interface SpaceMarkerRecord {\n readonly hash: string;\n readonly invariants: readonly string[];\n}\n\nexport interface VerifyContractSpacesInputs {\n /**\n * Set of contract spaces the project declares: `'app'` plus each\n * extension space in `extensionPacks`. The caller's discovery path\n * never reads the extension descriptor module — it walks the\n * `extensionPacks` configuration in `prisma-next.config.ts` for the\n * space ids.\n */\n readonly loadedSpaces: ReadonlySet<string>;\n\n /**\n * Per-space subdirectories observed under\n * `<projectRoot>/migrations/`. Resolved via\n * {@link listContractSpaceDirectories}.\n */\n readonly spaceDirsOnDisk: readonly string[];\n\n /**\n * Head ref per space, keyed by space id. Caller reads\n * `<projectRoot>/migrations/<space-id>/contract.json` and\n * `<projectRoot>/migrations/<space-id>/refs/head.json` to construct\n * this map. Spaces with no contract-space dir on disk simply omit a\n * map entry.\n */\n readonly headRefsBySpace: ReadonlyMap<string, ContractSpaceHeadRecord>;\n\n /**\n * Marker rows keyed by `space`. Caller reads them from the\n * `prisma_contract.marker` table.\n */\n readonly markerRowsBySpace: ReadonlyMap<string, SpaceMarkerRecord>;\n}\n\nexport type SpaceVerifierViolation =\n | {\n readonly kind: 'declaredButUnmigrated';\n readonly spaceId: string;\n readonly remediation: string;\n }\n | {\n readonly kind: 'orphanMarker';\n readonly spaceId: string;\n readonly remediation: string;\n }\n | {\n readonly kind: 'orphanSpaceDir';\n readonly spaceId: string;\n readonly remediation: string;\n }\n | {\n readonly kind: 'hashMismatch';\n readonly spaceId: string;\n readonly priorHeadHash: string;\n readonly markerHash: string;\n readonly remediation: string;\n }\n | {\n readonly kind: 'invariantsMismatch';\n readonly spaceId: string;\n readonly onDiskInvariants: readonly string[];\n readonly markerInvariants: readonly string[];\n readonly remediation: string;\n };\n\nexport type VerifyContractSpacesResult =\n | { readonly ok: true }\n | { readonly ok: false; readonly violations: readonly SpaceVerifierViolation[] };\n\n/**\n * Pure structural verifier for the per-space mechanism. Aggregates the\n * three orphan / missing checks plus per-space hash and invariant\n * comparison.\n *\n * Algorithm:\n *\n * - For every extension space declared in `loadedSpaces` (`'app'`\n * excluded — the per-space verifier is scoped to extension members;\n * the app is verified through the aggregate path):\n * - If no contract-space dir on disk → `declaredButUnmigrated`.\n * - Else if `markerRowsBySpace` lacks an entry → no violation here;\n * the live-DB compare done outside this helper is where the\n * absence shows up.\n * - Else compare marker hash / invariants vs. on-disk head hash /\n * invariants → `hashMismatch` / `invariantsMismatch` on drift.\n * - For every contract-space dir on disk that is not in `loadedSpaces` →\n * `orphanSpaceDir`.\n * - For every marker row whose `space` is not in `loadedSpaces` →\n * `orphanMarker`. The app-space marker is always loaded (`'app'` is\n * in `loadedSpaces` by definition).\n *\n * Output is deterministic: violations are sorted first by `kind`\n * (`declaredButUnmigrated` → `orphanMarker` → `orphanSpaceDir` →\n * `hashMismatch` → `invariantsMismatch`) then by `spaceId`. Two callers\n * passing equivalent inputs see byte-identical violation lists.\n *\n * Synchronous, pure, no I/O. **Does not import the extension descriptor**\n * (the inputs are pre-resolved by the caller); the verifier reads only\n * the user repo, not `node_modules`.\n */\nexport function verifyContractSpaces(\n inputs: VerifyContractSpacesInputs,\n): VerifyContractSpacesResult {\n const violations: SpaceVerifierViolation[] = [];\n\n for (const spaceId of [...inputs.loadedSpaces].sort()) {\n if (spaceId === APP_SPACE_ID) continue;\n\n if (!inputs.spaceDirsOnDisk.includes(spaceId)) {\n violations.push({\n kind: 'declaredButUnmigrated',\n spaceId,\n remediation: `Extension '${spaceId}' is declared in extensionPacks but has not been emitted; run \\`prisma-next migrate\\`.`,\n });\n continue;\n }\n\n const head = inputs.headRefsBySpace.get(spaceId);\n const marker = inputs.markerRowsBySpace.get(spaceId);\n if (!head || !marker) {\n continue;\n }\n\n if (head.hash !== marker.hash) {\n violations.push({\n kind: 'hashMismatch',\n spaceId,\n priorHeadHash: head.hash,\n markerHash: marker.hash,\n remediation: `Marker row for space '${spaceId}' is keyed at ${marker.hash}, but the on-disk ${join('migrations', spaceId, 'contract.json')} resolves to ${head.hash}. Run \\`prisma-next db update\\` to advance the database, or \\`prisma-next migrate\\` if the descriptor was bumped without re-emitting.`,\n });\n continue;\n }\n\n const onDiskInvariants = [...head.invariants].sort();\n const markerInvariants = new Set(marker.invariants);\n const missing = onDiskInvariants.filter((id) => !markerInvariants.has(id));\n if (missing.length > 0) {\n violations.push({\n kind: 'invariantsMismatch',\n spaceId,\n onDiskInvariants,\n markerInvariants: [...marker.invariants].sort(),\n remediation: `Marker row for space '${spaceId}' is missing invariants [${missing.map((s) => JSON.stringify(s)).join(', ')}]. Run \\`prisma-next db update\\` to apply the corresponding data-transform migrations.`,\n });\n }\n }\n\n for (const dir of [...inputs.spaceDirsOnDisk].sort()) {\n if (!inputs.loadedSpaces.has(dir)) {\n violations.push({\n kind: 'orphanSpaceDir',\n spaceId: dir,\n remediation: `Orphan contract-space directory \\`${join('migrations', dir)}/\\` for an extension not in extensionPacks; remove the directory or re-add the extension.`,\n });\n }\n }\n\n for (const space of [...inputs.markerRowsBySpace.keys()].sort()) {\n if (!inputs.loadedSpaces.has(space)) {\n violations.push({\n kind: 'orphanMarker',\n spaceId: space,\n remediation: `Orphan marker row for space '${space}' (no longer in extensionPacks); remediation: manually delete the row from \\`prisma_contract.marker\\`.`,\n });\n }\n }\n\n if (violations.length === 0) {\n return { ok: true };\n }\n\n const kindOrder: Record<SpaceVerifierViolation['kind'], number> = {\n declaredButUnmigrated: 0,\n orphanMarker: 1,\n orphanSpaceDir: 2,\n hashMismatch: 3,\n invariantsMismatch: 4,\n };\n\n violations.sort((a, b) => {\n const k = kindOrder[a.kind] - kindOrder[b.kind];\n if (k !== 0) return k;\n if (a.spaceId < b.spaceId) return -1;\n if (a.spaceId > b.spaceId) return 1;\n return 0;\n });\n\n return { ok: false, violations };\n}\n","import { readFile } from 'node:fs/promises';\nimport { join } from 'pathe';\nimport { errorInvalidJson, errorMissingFile } from './errors';\nimport { assertValidSpaceId } from './space-layout';\n\nfunction hasErrnoCode(error: unknown, code: string): boolean {\n return error instanceof Error && (error as { code?: string }).code === code;\n}\n\n/**\n * Read the on-disk contract value for a contract space\n * (`<projectMigrationsDir>/<spaceId>/contract.json`). Returns the parsed\n * JSON value as `unknown` — callers that need a typed contract validate\n * via their family's `validateContract` to surface schema issues.\n *\n * Companion to {@link import('./read-contract-space-head-ref').readContractSpaceHeadRef}\n * — same ENOENT-throws / corrupt-file-error semantics. Returns the\n * canonical-JSON value the framework wrote during emit, so re-running\n * this helper across machines / runs yields a byte-identical value.\n */\nexport async function readContractSpaceContract(\n projectMigrationsDir: string,\n spaceId: string,\n): Promise<unknown> {\n assertValidSpaceId(spaceId);\n\n const filePath = join(projectMigrationsDir, spaceId, 'contract.json');\n\n let raw: string;\n try {\n raw = await readFile(filePath, 'utf-8');\n } catch (error) {\n if (hasErrnoCode(error, 'ENOENT')) {\n throw errorMissingFile('contract.json', join(projectMigrationsDir, spaceId));\n }\n throw error;\n }\n\n try {\n return JSON.parse(raw);\n } catch (e) {\n throw errorInvalidJson(filePath, e instanceof Error ? e.message : String(e));\n }\n}\n"],"mappings":";;;;;;;;;;;AAoBA,MAAM,mBAAmB;AAEzB,SAAgB,eAAe,SAA0C;CACvE,OAAO,iBAAiB,KAAK,QAAQ;;AAGvC,SAAgB,mBAAmB,SAAkD;CACnF,IAAI,CAAC,eAAe,QAAQ,EAC1B,MAAM,oBAAoB,QAAQ;;;;;;;;;;;;;;AAgBtC,SAAgB,wBAAwB,sBAA8B,SAAyB;CAC7F,mBAAmB,QAAQ;CAC3B,OAAO,KAAK,sBAAsB,QAAQ;;;;ACtC5C,SAASA,eAAa,OAAgB,MAAuB;CAC3D,OAAO,iBAAiB,SAAU,MAA4B,SAAS;;;;;;;;;;;;;;;AAgBzE,eAAsB,yBACpB,sBACA,SACsC;CACtC,mBAAmB,QAAQ;CAE3B,MAAM,WAAW,KAAK,sBAAsB,SAAS,QAAQ,YAAY;CAEzE,IAAI;CACJ,IAAI;EACF,MAAM,MAAM,SAAS,UAAU,QAAQ;UAChC,OAAO;EACd,IAAIA,eAAa,OAAO,SAAS,EAC/B,OAAO;EAET,MAAM;;CAGR,IAAI;CACJ,IAAI;EACF,SAAS,KAAK,MAAM,IAAI;UACjB,GAAG;EACV,MAAM,iBAAiB,UAAU,aAAa,QAAQ,E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AO,SAAS,EAC/B,MAAM,iBAAiB,iBAAiB,KAAK,sBAAsB,QAAQ,CAAC;EAE9E,MAAM;;CAGR,IAAI;EACF,OAAO,KAAK,MAAM,IAAI;UACf,GAAG;EACV,MAAM,iBAAiB,UAAU,aAAa,QAAQ,EAAE,UAAU,OAAO,EAAE,CAAC"}
@@ -1,91 +0,0 @@
1
- /**
2
- * Inputs for {@link detectSpaceContractDrift}.
3
- *
4
- * Both hashes are produced by the caller (the SQL-family wiring at the
5
- * consumption site) using the canonical contract hashing pipeline.
6
- * Keeping the helper pure lets `migration-tools` stay framework-neutral
7
- * — the SQL family already speaks `Contract<SqlStorage>`, the Mongo
8
- * family speaks its own contract type, and both reduce to a hash string
9
- * before drift detection runs.
10
- *
11
- * `priorHeadHash` is `null` when no `contract.json` exists yet on disk for
12
- * the space (the descriptor declares an extension that has never been
13
- * emitted into the user's repo). That's the "first emit" case — no
14
- * drift to surface; the migrate emit will create the on-disk artefacts.
15
- */
16
- export interface DetectSpaceContractDriftInputs {
17
- readonly descriptorHash: string;
18
- readonly priorHeadHash: string | null;
19
- }
20
-
21
- /**
22
- * Result discriminant for {@link detectSpaceContractDrift}.
23
- *
24
- * - `noDrift`: descriptor hash and on-disk head hash agree byte-for-byte.
25
- * The migrate emit can proceed with no warning.
26
- * - `firstEmit`: no on-disk `contract.json` on disk yet. The extension
27
- * was just added to `extensionPacks`; this run will create the
28
- * on-disk artefacts. No warning either — the user's intent is to install
29
- * the extension, not to "drift" from a state they haven't recorded.
30
- * - `drift`: descriptor hash differs from on-disk head hash. The caller
31
- * surfaces a non-fatal warning naming the extension and the
32
- * diff direction (descriptor → on-disk head). The migrate emit proceeds
33
- * normally so the bump is materialised this run; the warning just
34
- * confirms the bump is being captured.
35
- *
36
- * `spaceId`, `descriptorHash`, and `priorHeadHash` are threaded through
37
- * verbatim so the caller (logger / TerminalUI / strict-mode envelope)
38
- * has everything it needs to format the warning message without
39
- * re-reading the descriptor or the on-disk artefact.
40
- */
41
- export type SpaceContractDriftResult = {
42
- readonly kind: 'noDrift' | 'firstEmit' | 'drift';
43
- readonly spaceId: string;
44
- readonly descriptorHash: string;
45
- readonly priorHeadHash: string | null;
46
- };
47
-
48
- /**
49
- * Pure drift-detection primitive for a single contract space.
50
- *
51
- * Runs once per loaded extension space, just before computing the
52
- * `priorContract` that feeds {@link import('./plan-all-spaces').planAllSpaces}.
53
- * Hash equality is byte-for-byte (no normalisation) — both sides are
54
- * already canonical hashes produced by the same pipeline, so any
55
- * difference is meaningful drift.
56
- *
57
- * Synchronous, pure, no I/O. The caller (SQL family) reads the on-disk
58
- * `contract.json` and computes its hash, then invokes this helper
59
- * alongside the descriptor's `headRef.hash`. Composes naturally with
60
- * {@link import('./read-contract-space-head-ref').readContractSpaceHeadRef}
61
- * which provides the read-side primitive.
62
- *
63
- * The drift warning surfaces the extension name and the diff direction.
64
- */
65
- export function detectSpaceContractDrift(
66
- spaceId: string,
67
- inputs: DetectSpaceContractDriftInputs,
68
- ): SpaceContractDriftResult {
69
- if (inputs.priorHeadHash === null) {
70
- return {
71
- kind: 'firstEmit',
72
- spaceId,
73
- descriptorHash: inputs.descriptorHash,
74
- priorHeadHash: null,
75
- };
76
- }
77
- if (inputs.descriptorHash === inputs.priorHeadHash) {
78
- return {
79
- kind: 'noDrift',
80
- spaceId,
81
- descriptorHash: inputs.descriptorHash,
82
- priorHeadHash: inputs.priorHeadHash,
83
- };
84
- }
85
- return {
86
- kind: 'drift',
87
- spaceId,
88
- descriptorHash: inputs.descriptorHash,
89
- priorHeadHash: inputs.priorHeadHash,
90
- };
91
- }