@prisma-next/migration-tools 0.5.0-dev.9 → 0.6.0-dev.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.
- 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 +39 -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 +591 -0
- package/dist/exports/spaces.d.mts.map +1 -0
- package/dist/exports/spaces.mjs +266 -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-qgQGlsrV.mjs +57 -0
- package/dist/invariants-qgQGlsrV.mjs.map +1 -0
- package/dist/io-D5YYptRO.mjs +239 -0
- package/dist/io-D5YYptRO.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-Bj_EMYSC.mjs +298 -0
- package/dist/read-contract-space-contract-Bj_EMYSC.mjs.map +1 -0
- package/package.json +42 -17
- 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/contract-space-from-json.ts +63 -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 +50 -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 +61 -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
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { errorDuplicateSpaceId } from './errors';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Per-space input for {@link planAllSpaces}. One entry per loaded
|
|
5
|
+
* contract space (the application's `'app'` plus each extension that
|
|
6
|
+
* exposes a `contractSpace`).
|
|
7
|
+
*
|
|
8
|
+
* - `priorContract` is `null` for a space that has never been emitted
|
|
9
|
+
* (no `migrations/<space-id>/contract.json` on disk yet); otherwise it
|
|
10
|
+
* is the canonical contract value emitted for that space.
|
|
11
|
+
* - `newContract` is the canonical contract value the planner is about
|
|
12
|
+
* to emit for that space — for app-space, the just-emitted root
|
|
13
|
+
* `contract.json`; for an extension space, the descriptor's
|
|
14
|
+
* `contractSpace.contractJson`.
|
|
15
|
+
*/
|
|
16
|
+
export interface SpacePlanInput<TContract> {
|
|
17
|
+
readonly spaceId: string;
|
|
18
|
+
readonly priorContract: TContract | null;
|
|
19
|
+
readonly newContract: TContract;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface SpacePlanOutput<TPackage> {
|
|
23
|
+
readonly spaceId: string;
|
|
24
|
+
readonly migrationPackages: readonly TPackage[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Iterate the per-space planner across a set of loaded contract spaces
|
|
29
|
+
* and return a deterministic shape regardless of declaration order.
|
|
30
|
+
*
|
|
31
|
+
* Behaviour:
|
|
32
|
+
*
|
|
33
|
+
* - The output is sorted alphabetically by `spaceId`. Two callers
|
|
34
|
+
* passing the same set of inputs in different orders observe
|
|
35
|
+
* byte-identical outputs.
|
|
36
|
+
* - The per-space planner (`planSpace`) is called exactly once per
|
|
37
|
+
* input, in alphabetical-by-spaceId order. Its return value is
|
|
38
|
+
* attached to the corresponding output entry verbatim.
|
|
39
|
+
* - Duplicate `spaceId`s in the input array throw
|
|
40
|
+
* `MIGRATION.DUPLICATE_SPACE_ID` before any `planSpace` call runs,
|
|
41
|
+
* keeping the planner pure when the input is malformed.
|
|
42
|
+
*
|
|
43
|
+
* The signature is generic over `TContract` and `TPackage` because the
|
|
44
|
+
* shape is framework-neutral (SQL family today, Mongo family
|
|
45
|
+
* eventually). Callers wire in whatever contract value and migration
|
|
46
|
+
* package shape their family already speaks.
|
|
47
|
+
*
|
|
48
|
+
* Synchronous: the underlying per-space planner (target's
|
|
49
|
+
* `MigrationPlanner.plan(...)`) is synchronous; callers that need to
|
|
50
|
+
* resolve async I/O (e.g. reading on-disk `contract.json` from disk)
|
|
51
|
+
* resolve it before calling `planAllSpaces` and pass the materialised
|
|
52
|
+
* inputs through.
|
|
53
|
+
*/
|
|
54
|
+
export function planAllSpaces<TContract, TPackage>(
|
|
55
|
+
inputs: readonly SpacePlanInput<TContract>[],
|
|
56
|
+
planSpace: (input: SpacePlanInput<TContract>) => readonly TPackage[],
|
|
57
|
+
): readonly SpacePlanOutput<TPackage>[] {
|
|
58
|
+
const seen = new Set<string>();
|
|
59
|
+
for (const input of inputs) {
|
|
60
|
+
if (seen.has(input.spaceId)) {
|
|
61
|
+
throw errorDuplicateSpaceId(input.spaceId);
|
|
62
|
+
}
|
|
63
|
+
seen.add(input.spaceId);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const sorted = [...inputs].sort((a, b) => {
|
|
67
|
+
if (a.spaceId < b.spaceId) return -1;
|
|
68
|
+
if (a.spaceId > b.spaceId) return 1;
|
|
69
|
+
return 0;
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
return sorted.map((input) => ({
|
|
73
|
+
spaceId: input.spaceId,
|
|
74
|
+
migrationPackages: planSpace(input),
|
|
75
|
+
}));
|
|
76
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'pathe';
|
|
3
|
+
import { errorInvalidJson, errorMissingFile } from './errors';
|
|
4
|
+
import { assertValidSpaceId } from './space-layout';
|
|
5
|
+
|
|
6
|
+
function hasErrnoCode(error: unknown, code: string): boolean {
|
|
7
|
+
return error instanceof Error && (error as { code?: string }).code === code;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Read the on-disk contract value for a contract space
|
|
12
|
+
* (`<projectMigrationsDir>/<spaceId>/contract.json`). Returns the parsed
|
|
13
|
+
* JSON value as `unknown` — callers that need a typed contract validate
|
|
14
|
+
* via their family's `validateContract` to surface schema issues.
|
|
15
|
+
*
|
|
16
|
+
* Companion to {@link import('./read-contract-space-head-ref').readContractSpaceHeadRef}
|
|
17
|
+
* — same ENOENT-throws / corrupt-file-error semantics. Returns the
|
|
18
|
+
* canonical-JSON value the framework wrote during emit, so re-running
|
|
19
|
+
* this helper across machines / runs yields a byte-identical value.
|
|
20
|
+
*/
|
|
21
|
+
export async function readContractSpaceContract(
|
|
22
|
+
projectMigrationsDir: string,
|
|
23
|
+
spaceId: string,
|
|
24
|
+
): Promise<unknown> {
|
|
25
|
+
assertValidSpaceId(spaceId);
|
|
26
|
+
|
|
27
|
+
const filePath = join(projectMigrationsDir, spaceId, 'contract.json');
|
|
28
|
+
|
|
29
|
+
let raw: string;
|
|
30
|
+
try {
|
|
31
|
+
raw = await readFile(filePath, 'utf-8');
|
|
32
|
+
} catch (error) {
|
|
33
|
+
if (hasErrnoCode(error, 'ENOENT')) {
|
|
34
|
+
throw errorMissingFile('contract.json', join(projectMigrationsDir, spaceId));
|
|
35
|
+
}
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
return JSON.parse(raw);
|
|
41
|
+
} catch (e) {
|
|
42
|
+
throw errorInvalidJson(filePath, e instanceof Error ? e.message : String(e));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import type { ContractSpaceHeadRef } from '@prisma-next/framework-components/control';
|
|
3
|
+
import { join } from 'pathe';
|
|
4
|
+
import { errorInvalidJson, errorInvalidRefFile } from './errors';
|
|
5
|
+
import { assertValidSpaceId } from './space-layout';
|
|
6
|
+
|
|
7
|
+
export type { ContractSpaceHeadRef };
|
|
8
|
+
|
|
9
|
+
function hasErrnoCode(error: unknown, code: string): boolean {
|
|
10
|
+
return error instanceof Error && (error as { code?: string }).code === code;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Read the head ref (`hash` + `invariants`) for a contract space from
|
|
15
|
+
* `<projectMigrationsDir>/<spaceId>/refs/head.json`.
|
|
16
|
+
*
|
|
17
|
+
* Returns `null` when the file does not exist (first emit). Surfaces
|
|
18
|
+
* `MIGRATION.INVALID_JSON` / `MIGRATION.INVALID_REF_FILE` on a corrupt
|
|
19
|
+
* `refs/head.json` so callers can distinguish "no head ref on disk"
|
|
20
|
+
* (returns `null`) from "head ref present but unreadable" (throws).
|
|
21
|
+
*
|
|
22
|
+
* Validates the space id against `[a-z][a-z0-9_-]{0,63}` for the same
|
|
23
|
+
* filesystem-safety reasons as the rest of the per-space helpers. The
|
|
24
|
+
* helper is uniform across the app and extension spaces.
|
|
25
|
+
*/
|
|
26
|
+
export async function readContractSpaceHeadRef(
|
|
27
|
+
projectMigrationsDir: string,
|
|
28
|
+
spaceId: string,
|
|
29
|
+
): Promise<ContractSpaceHeadRef | null> {
|
|
30
|
+
assertValidSpaceId(spaceId);
|
|
31
|
+
|
|
32
|
+
const filePath = join(projectMigrationsDir, spaceId, 'refs', 'head.json');
|
|
33
|
+
|
|
34
|
+
let raw: string;
|
|
35
|
+
try {
|
|
36
|
+
raw = await readFile(filePath, 'utf-8');
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (hasErrnoCode(error, 'ENOENT')) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
let parsed: unknown;
|
|
45
|
+
try {
|
|
46
|
+
parsed = JSON.parse(raw);
|
|
47
|
+
} catch (e) {
|
|
48
|
+
throw errorInvalidJson(filePath, e instanceof Error ? e.message : String(e));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (typeof parsed !== 'object' || parsed === null) {
|
|
52
|
+
throw errorInvalidRefFile(filePath, 'expected an object');
|
|
53
|
+
}
|
|
54
|
+
const obj = parsed as { hash?: unknown; invariants?: unknown };
|
|
55
|
+
if (typeof obj.hash !== 'string') {
|
|
56
|
+
throw errorInvalidRefFile(filePath, 'expected an object with a string `hash` field');
|
|
57
|
+
}
|
|
58
|
+
if (!Array.isArray(obj.invariants) || obj.invariants.some((value) => typeof value !== 'string')) {
|
|
59
|
+
throw errorInvalidRefFile(filePath, 'expected an object with an `invariants` array of strings');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return { hash: obj.hash, invariants: obj.invariants as readonly string[] };
|
|
63
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { APP_SPACE_ID } from '@prisma-next/framework-components/control';
|
|
2
|
+
import { join } from 'pathe';
|
|
3
|
+
import { errorInvalidSpaceId } from './errors';
|
|
4
|
+
|
|
5
|
+
export { APP_SPACE_ID };
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Branded string carrying a compile-time guarantee that the value has
|
|
9
|
+
* been validated by {@link assertValidSpaceId}. Downstream filesystem
|
|
10
|
+
* helpers (e.g. {@link spaceMigrationDirectory}) accept this type to
|
|
11
|
+
* make "validated" tracking visible at the type level rather than
|
|
12
|
+
* relying purely on a runtime check.
|
|
13
|
+
*/
|
|
14
|
+
export type ValidSpaceId = string & { readonly __brand: 'ValidSpaceId' };
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Pattern a contract-space identifier must match. The constraint is
|
|
18
|
+
* filesystem-friendly: lowercase letters / digits / hyphen / underscore,
|
|
19
|
+
* starts with a letter, max 64 characters.
|
|
20
|
+
*/
|
|
21
|
+
const SPACE_ID_PATTERN = /^[a-z][a-z0-9_-]{0,63}$/;
|
|
22
|
+
|
|
23
|
+
export function isValidSpaceId(spaceId: string): spaceId is ValidSpaceId {
|
|
24
|
+
return SPACE_ID_PATTERN.test(spaceId);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function assertValidSpaceId(spaceId: string): asserts spaceId is ValidSpaceId {
|
|
28
|
+
if (!isValidSpaceId(spaceId)) {
|
|
29
|
+
throw errorInvalidSpaceId(spaceId);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Resolve the migrations subdirectory for a given contract space.
|
|
35
|
+
*
|
|
36
|
+
* Every contract space — including the app space (default `'app'`) —
|
|
37
|
+
* lands under `<projectMigrationsDir>/<spaceId>/`. The space id is
|
|
38
|
+
* validated against {@link SPACE_ID_PATTERN} because it becomes a
|
|
39
|
+
* filesystem directory name verbatim.
|
|
40
|
+
*
|
|
41
|
+
* `projectMigrationsDir` is the project's top-level `migrations/`
|
|
42
|
+
* directory; the helper does not assume anything about its absolute /
|
|
43
|
+
* relative shape and is symmetric with `pathe.join`.
|
|
44
|
+
*/
|
|
45
|
+
export function spaceMigrationDirectory(projectMigrationsDir: string, spaceId: string): string {
|
|
46
|
+
assertValidSpaceId(spaceId);
|
|
47
|
+
return join(projectMigrationsDir, spaceId);
|
|
48
|
+
}
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { readdir, stat } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'pathe';
|
|
3
|
+
import { MANIFEST_FILE } from './io';
|
|
4
|
+
import { APP_SPACE_ID } from './space-layout';
|
|
5
|
+
|
|
6
|
+
function hasErrnoCode(error: unknown, code: string): boolean {
|
|
7
|
+
return error instanceof Error && (error as { code?: string }).code === code;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* List the per-space subdirectories under
|
|
12
|
+
* `<projectRoot>/migrations/`. Returns space-id directory names (sorted
|
|
13
|
+
* alphabetically) — i.e. any non-dot-prefixed subdirectory whose root
|
|
14
|
+
* does **not** contain a `migration.json` manifest. The manifest is the
|
|
15
|
+
* structural marker of a user-authored migration directory (see
|
|
16
|
+
* `readMigrationsDir` in `./io`); directory names themselves belong to
|
|
17
|
+
* the user and are not part of the contract.
|
|
18
|
+
*
|
|
19
|
+
* Returns `[]` if the migrations directory does not exist (greenfield
|
|
20
|
+
* project).
|
|
21
|
+
*
|
|
22
|
+
* Reads only the user's repo. **No descriptor import.** The caller
|
|
23
|
+
* (verifier) feeds the result into {@link verifyContractSpaces} alongside
|
|
24
|
+
* the loaded-space set and the marker rows.
|
|
25
|
+
*/
|
|
26
|
+
export async function listContractSpaceDirectories(
|
|
27
|
+
projectMigrationsDir: string,
|
|
28
|
+
): Promise<readonly string[]> {
|
|
29
|
+
let entries: { readonly name: string; readonly isDirectory: boolean }[];
|
|
30
|
+
try {
|
|
31
|
+
const dirents = await readdir(projectMigrationsDir, { withFileTypes: true });
|
|
32
|
+
entries = dirents.map((d) => ({ name: d.name, isDirectory: d.isDirectory() }));
|
|
33
|
+
} catch (error) {
|
|
34
|
+
if (hasErrnoCode(error, 'ENOENT')) {
|
|
35
|
+
return [];
|
|
36
|
+
}
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const namedCandidates = entries
|
|
41
|
+
.filter((e) => e.isDirectory)
|
|
42
|
+
.map((e) => e.name)
|
|
43
|
+
.filter((name) => !name.startsWith('.'))
|
|
44
|
+
.sort();
|
|
45
|
+
|
|
46
|
+
const manifestChecks = await Promise.all(
|
|
47
|
+
namedCandidates.map(async (name) => {
|
|
48
|
+
try {
|
|
49
|
+
await stat(join(projectMigrationsDir, name, MANIFEST_FILE));
|
|
50
|
+
return { name, isMigrationDir: true };
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (hasErrnoCode(error, 'ENOENT')) {
|
|
53
|
+
return { name, isMigrationDir: false };
|
|
54
|
+
}
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
return manifestChecks.filter((c) => !c.isMigrationDir).map((c) => c.name);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* On-disk head value (`(hash, invariants)`) for one contract space.
|
|
65
|
+
* The verifier compares this against the marker row for the same space
|
|
66
|
+
* to detect drift between the user-emitted artefacts and the live DB
|
|
67
|
+
* marker.
|
|
68
|
+
*/
|
|
69
|
+
export interface ContractSpaceHeadRecord {
|
|
70
|
+
readonly hash: string;
|
|
71
|
+
readonly invariants: readonly string[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Marker row read from `prisma_contract.marker` (one per `space`).
|
|
76
|
+
* Caller resolves these via the family runtime's marker reader before
|
|
77
|
+
* invoking {@link verifyContractSpaces}.
|
|
78
|
+
*/
|
|
79
|
+
export interface SpaceMarkerRecord {
|
|
80
|
+
readonly hash: string;
|
|
81
|
+
readonly invariants: readonly string[];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface VerifyContractSpacesInputs {
|
|
85
|
+
/**
|
|
86
|
+
* Set of contract spaces the project declares: `'app'` plus each
|
|
87
|
+
* extension space in `extensionPacks`. The caller's discovery path
|
|
88
|
+
* never reads the extension descriptor module — it walks the
|
|
89
|
+
* `extensionPacks` configuration in `prisma-next.config.ts` for the
|
|
90
|
+
* space ids.
|
|
91
|
+
*/
|
|
92
|
+
readonly loadedSpaces: ReadonlySet<string>;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Per-space subdirectories observed under
|
|
96
|
+
* `<projectRoot>/migrations/`. Resolved via
|
|
97
|
+
* {@link listContractSpaceDirectories}.
|
|
98
|
+
*/
|
|
99
|
+
readonly spaceDirsOnDisk: readonly string[];
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Head ref per space, keyed by space id. Caller reads
|
|
103
|
+
* `<projectRoot>/migrations/<space-id>/contract.json` and
|
|
104
|
+
* `<projectRoot>/migrations/<space-id>/refs/head.json` to construct
|
|
105
|
+
* this map. Spaces with no contract-space dir on disk simply omit a
|
|
106
|
+
* map entry.
|
|
107
|
+
*/
|
|
108
|
+
readonly headRefsBySpace: ReadonlyMap<string, ContractSpaceHeadRecord>;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Marker rows keyed by `space`. Caller reads them from the
|
|
112
|
+
* `prisma_contract.marker` table.
|
|
113
|
+
*/
|
|
114
|
+
readonly markerRowsBySpace: ReadonlyMap<string, SpaceMarkerRecord>;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export type SpaceVerifierViolation =
|
|
118
|
+
| {
|
|
119
|
+
readonly kind: 'declaredButUnmigrated';
|
|
120
|
+
readonly spaceId: string;
|
|
121
|
+
readonly remediation: string;
|
|
122
|
+
}
|
|
123
|
+
| {
|
|
124
|
+
readonly kind: 'orphanMarker';
|
|
125
|
+
readonly spaceId: string;
|
|
126
|
+
readonly remediation: string;
|
|
127
|
+
}
|
|
128
|
+
| {
|
|
129
|
+
readonly kind: 'orphanSpaceDir';
|
|
130
|
+
readonly spaceId: string;
|
|
131
|
+
readonly remediation: string;
|
|
132
|
+
}
|
|
133
|
+
| {
|
|
134
|
+
readonly kind: 'hashMismatch';
|
|
135
|
+
readonly spaceId: string;
|
|
136
|
+
readonly priorHeadHash: string;
|
|
137
|
+
readonly markerHash: string;
|
|
138
|
+
readonly remediation: string;
|
|
139
|
+
}
|
|
140
|
+
| {
|
|
141
|
+
readonly kind: 'invariantsMismatch';
|
|
142
|
+
readonly spaceId: string;
|
|
143
|
+
readonly onDiskInvariants: readonly string[];
|
|
144
|
+
readonly markerInvariants: readonly string[];
|
|
145
|
+
readonly remediation: string;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
export type VerifyContractSpacesResult =
|
|
149
|
+
| { readonly ok: true }
|
|
150
|
+
| { readonly ok: false; readonly violations: readonly SpaceVerifierViolation[] };
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Pure structural verifier for the per-space mechanism. Aggregates the
|
|
154
|
+
* three orphan / missing checks plus per-space hash and invariant
|
|
155
|
+
* comparison.
|
|
156
|
+
*
|
|
157
|
+
* Algorithm:
|
|
158
|
+
*
|
|
159
|
+
* - For every extension space declared in `loadedSpaces` (`'app'`
|
|
160
|
+
* excluded — the per-space verifier is scoped to extension members;
|
|
161
|
+
* the app is verified through the aggregate path):
|
|
162
|
+
* - If no contract-space dir on disk → `declaredButUnmigrated`.
|
|
163
|
+
* - Else if `markerRowsBySpace` lacks an entry → no violation here;
|
|
164
|
+
* the live-DB compare done outside this helper is where the
|
|
165
|
+
* absence shows up.
|
|
166
|
+
* - Else compare marker hash / invariants vs. on-disk head hash /
|
|
167
|
+
* invariants → `hashMismatch` / `invariantsMismatch` on drift.
|
|
168
|
+
* - For every contract-space dir on disk that is not in `loadedSpaces` →
|
|
169
|
+
* `orphanSpaceDir`.
|
|
170
|
+
* - For every marker row whose `space` is not in `loadedSpaces` →
|
|
171
|
+
* `orphanMarker`. The app-space marker is always loaded (`'app'` is
|
|
172
|
+
* in `loadedSpaces` by definition).
|
|
173
|
+
*
|
|
174
|
+
* Output is deterministic: violations are sorted first by `kind`
|
|
175
|
+
* (`declaredButUnmigrated` → `orphanMarker` → `orphanSpaceDir` →
|
|
176
|
+
* `hashMismatch` → `invariantsMismatch`) then by `spaceId`. Two callers
|
|
177
|
+
* passing equivalent inputs see byte-identical violation lists.
|
|
178
|
+
*
|
|
179
|
+
* Synchronous, pure, no I/O. **Does not import the extension descriptor**
|
|
180
|
+
* (the inputs are pre-resolved by the caller); the verifier reads only
|
|
181
|
+
* the user repo, not `node_modules`.
|
|
182
|
+
*/
|
|
183
|
+
export function verifyContractSpaces(
|
|
184
|
+
inputs: VerifyContractSpacesInputs,
|
|
185
|
+
): VerifyContractSpacesResult {
|
|
186
|
+
const violations: SpaceVerifierViolation[] = [];
|
|
187
|
+
|
|
188
|
+
for (const spaceId of [...inputs.loadedSpaces].sort()) {
|
|
189
|
+
if (spaceId === APP_SPACE_ID) continue;
|
|
190
|
+
|
|
191
|
+
if (!inputs.spaceDirsOnDisk.includes(spaceId)) {
|
|
192
|
+
violations.push({
|
|
193
|
+
kind: 'declaredButUnmigrated',
|
|
194
|
+
spaceId,
|
|
195
|
+
remediation: `Extension '${spaceId}' is declared in extensionPacks but has not been emitted; run \`prisma-next migrate\`.`,
|
|
196
|
+
});
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const head = inputs.headRefsBySpace.get(spaceId);
|
|
201
|
+
const marker = inputs.markerRowsBySpace.get(spaceId);
|
|
202
|
+
if (!head || !marker) {
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (head.hash !== marker.hash) {
|
|
207
|
+
violations.push({
|
|
208
|
+
kind: 'hashMismatch',
|
|
209
|
+
spaceId,
|
|
210
|
+
priorHeadHash: head.hash,
|
|
211
|
+
markerHash: marker.hash,
|
|
212
|
+
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.`,
|
|
213
|
+
});
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const onDiskInvariants = [...head.invariants].sort();
|
|
218
|
+
const markerInvariants = new Set(marker.invariants);
|
|
219
|
+
const missing = onDiskInvariants.filter((id) => !markerInvariants.has(id));
|
|
220
|
+
if (missing.length > 0) {
|
|
221
|
+
violations.push({
|
|
222
|
+
kind: 'invariantsMismatch',
|
|
223
|
+
spaceId,
|
|
224
|
+
onDiskInvariants,
|
|
225
|
+
markerInvariants: [...marker.invariants].sort(),
|
|
226
|
+
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.`,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
for (const dir of [...inputs.spaceDirsOnDisk].sort()) {
|
|
232
|
+
if (!inputs.loadedSpaces.has(dir)) {
|
|
233
|
+
violations.push({
|
|
234
|
+
kind: 'orphanSpaceDir',
|
|
235
|
+
spaceId: dir,
|
|
236
|
+
remediation: `Orphan contract-space directory \`${join('migrations', dir)}/\` for an extension not in extensionPacks; remove the directory or re-add the extension.`,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
for (const space of [...inputs.markerRowsBySpace.keys()].sort()) {
|
|
242
|
+
if (!inputs.loadedSpaces.has(space)) {
|
|
243
|
+
violations.push({
|
|
244
|
+
kind: 'orphanMarker',
|
|
245
|
+
spaceId: space,
|
|
246
|
+
remediation: `Orphan marker row for space '${space}' (no longer in extensionPacks); remediation: manually delete the row from \`prisma_contract.marker\`.`,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (violations.length === 0) {
|
|
252
|
+
return { ok: true };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const kindOrder: Record<SpaceVerifierViolation['kind'], number> = {
|
|
256
|
+
declaredButUnmigrated: 0,
|
|
257
|
+
orphanMarker: 1,
|
|
258
|
+
orphanSpaceDir: 2,
|
|
259
|
+
hashMismatch: 3,
|
|
260
|
+
invariantsMismatch: 4,
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
violations.sort((a, b) => {
|
|
264
|
+
const k = kindOrder[a.kind] - kindOrder[b.kind];
|
|
265
|
+
if (k !== 0) return k;
|
|
266
|
+
if (a.spaceId < b.spaceId) return -1;
|
|
267
|
+
if (a.spaceId > b.spaceId) return 1;
|
|
268
|
+
return 0;
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
return { ok: false, violations };
|
|
272
|
+
}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { r as readMigrationPackage } from "./io-Cd6GLyjK.mjs";
|
|
2
|
-
import { createHash } from "node:crypto";
|
|
3
|
-
|
|
4
|
-
//#region src/canonicalize-json.ts
|
|
5
|
-
function sortKeys(value) {
|
|
6
|
-
if (value === null || typeof value !== "object") return value;
|
|
7
|
-
if (Array.isArray(value)) return value.map(sortKeys);
|
|
8
|
-
const sorted = {};
|
|
9
|
-
for (const key of Object.keys(value).sort()) sorted[key] = sortKeys(value[key]);
|
|
10
|
-
return sorted;
|
|
11
|
-
}
|
|
12
|
-
function canonicalizeJson(value) {
|
|
13
|
-
return JSON.stringify(sortKeys(value));
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
//#endregion
|
|
17
|
-
//#region src/attestation.ts
|
|
18
|
-
function sha256Hex(input) {
|
|
19
|
-
return createHash("sha256").update(input).digest("hex");
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Content-addressed migration identity over (manifest envelope sans
|
|
23
|
-
* contracts/hints, ops). See ADR 199 "Storage-only migration identity"
|
|
24
|
-
* for the rationale: contracts are anchored separately by the
|
|
25
|
-
* storage-hash bookends inside the envelope; planner hints are advisory
|
|
26
|
-
* and must not affect identity.
|
|
27
|
-
*
|
|
28
|
-
* The `migrationId` field on the manifest is stripped before hashing so
|
|
29
|
-
* the function can be used both at write time (when no id exists yet)
|
|
30
|
-
* and at verify time (rehashing an already-attested manifest).
|
|
31
|
-
*/
|
|
32
|
-
function computeMigrationId(manifest, ops) {
|
|
33
|
-
const { migrationId: _migrationId, signature: _signature, fromContract: _fromContract, toContract: _toContract, hints: _hints, ...strippedMeta } = manifest;
|
|
34
|
-
return `sha256:${sha256Hex(canonicalizeJson([canonicalizeJson(strippedMeta), canonicalizeJson(ops)].map(sha256Hex)))}`;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Re-hash an on-disk migration bundle and compare against the stored
|
|
38
|
-
* `migrationId`. Returns `{ ok: true }` when the package is internally
|
|
39
|
-
* consistent (manifest + ops still produce the recorded id), or
|
|
40
|
-
* `{ ok: false, reason: 'mismatch', stored, computed }` when they do
|
|
41
|
-
* not — typically a sign of FS corruption, partial writes, or a
|
|
42
|
-
* post-emit hand edit.
|
|
43
|
-
*/
|
|
44
|
-
function verifyMigrationBundle(bundle) {
|
|
45
|
-
const computed = computeMigrationId(bundle.manifest, bundle.ops);
|
|
46
|
-
if (bundle.manifest.migrationId === computed) return {
|
|
47
|
-
ok: true,
|
|
48
|
-
storedMigrationId: bundle.manifest.migrationId,
|
|
49
|
-
computedMigrationId: computed
|
|
50
|
-
};
|
|
51
|
-
return {
|
|
52
|
-
ok: false,
|
|
53
|
-
reason: "mismatch",
|
|
54
|
-
storedMigrationId: bundle.manifest.migrationId,
|
|
55
|
-
computedMigrationId: computed
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
/** Convenience wrapper: read the package from disk then verify it. */
|
|
59
|
-
async function verifyMigration(dir) {
|
|
60
|
-
return verifyMigrationBundle(await readMigrationPackage(dir));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
//#endregion
|
|
64
|
-
export { verifyMigration as n, verifyMigrationBundle as r, computeMigrationId as t };
|
|
65
|
-
//# sourceMappingURL=attestation-BnzTb0Qp.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"attestation-BnzTb0Qp.mjs","names":["sorted: Record<string, unknown>"],"sources":["../src/canonicalize-json.ts","../src/attestation.ts"],"sourcesContent":["function sortKeys(value: unknown): unknown {\n if (value === null || typeof value !== 'object') {\n return value;\n }\n if (Array.isArray(value)) {\n return value.map(sortKeys);\n }\n const sorted: Record<string, unknown> = {};\n for (const key of Object.keys(value).sort()) {\n sorted[key] = sortKeys((value as Record<string, unknown>)[key]);\n }\n return sorted;\n}\n\nexport function canonicalizeJson(value: unknown): string {\n return JSON.stringify(sortKeys(value));\n}\n","import { createHash } from 'node:crypto';\nimport { canonicalizeJson } from './canonicalize-json';\nimport { readMigrationPackage } from './io';\nimport type { MigrationBundle, MigrationManifest, MigrationOps } from './types';\n\nexport interface VerifyResult {\n readonly ok: boolean;\n readonly reason?: 'mismatch';\n readonly storedMigrationId?: string;\n readonly computedMigrationId?: string;\n}\n\nfunction sha256Hex(input: string): string {\n return createHash('sha256').update(input).digest('hex');\n}\n\n/**\n * Content-addressed migration identity over (manifest envelope sans\n * contracts/hints, ops). See ADR 199 \"Storage-only migration identity\"\n * for the rationale: contracts are anchored separately by the\n * storage-hash bookends inside the envelope; planner hints are advisory\n * and must not affect identity.\n *\n * The `migrationId` field on the manifest is stripped before hashing so\n * the function can be used both at write time (when no id exists yet)\n * and at verify time (rehashing an already-attested manifest).\n */\nexport function computeMigrationId(\n manifest: Omit<MigrationManifest, 'migrationId'> & { readonly migrationId?: string },\n ops: MigrationOps,\n): string {\n const {\n migrationId: _migrationId,\n signature: _signature,\n fromContract: _fromContract,\n toContract: _toContract,\n hints: _hints,\n ...strippedMeta\n } = manifest;\n\n const canonicalManifest = canonicalizeJson(strippedMeta);\n const canonicalOps = canonicalizeJson(ops);\n\n const partHashes = [canonicalManifest, canonicalOps].map(sha256Hex);\n const hash = sha256Hex(canonicalizeJson(partHashes));\n\n return `sha256:${hash}`;\n}\n\n/**\n * Re-hash an on-disk migration bundle and compare against the stored\n * `migrationId`. Returns `{ ok: true }` when the package is internally\n * consistent (manifest + ops still produce the recorded id), or\n * `{ ok: false, reason: 'mismatch', stored, computed }` when they do\n * not — typically a sign of FS corruption, partial writes, or a\n * post-emit hand edit.\n */\nexport function verifyMigrationBundle(bundle: MigrationBundle): VerifyResult {\n const computed = computeMigrationId(bundle.manifest, bundle.ops);\n\n if (bundle.manifest.migrationId === computed) {\n return {\n ok: true,\n storedMigrationId: bundle.manifest.migrationId,\n computedMigrationId: computed,\n };\n }\n\n return {\n ok: false,\n reason: 'mismatch',\n storedMigrationId: bundle.manifest.migrationId,\n computedMigrationId: computed,\n };\n}\n\n/** Convenience wrapper: read the package from disk then verify it. */\nexport async function verifyMigration(dir: string): Promise<VerifyResult> {\n const pkg = await readMigrationPackage(dir);\n return verifyMigrationBundle(pkg);\n}\n"],"mappings":";;;;AAAA,SAAS,SAAS,OAAyB;AACzC,KAAI,UAAU,QAAQ,OAAO,UAAU,SACrC,QAAO;AAET,KAAI,MAAM,QAAQ,MAAM,CACtB,QAAO,MAAM,IAAI,SAAS;CAE5B,MAAMA,SAAkC,EAAE;AAC1C,MAAK,MAAM,OAAO,OAAO,KAAK,MAAM,CAAC,MAAM,CACzC,QAAO,OAAO,SAAU,MAAkC,KAAK;AAEjE,QAAO;;AAGT,SAAgB,iBAAiB,OAAwB;AACvD,QAAO,KAAK,UAAU,SAAS,MAAM,CAAC;;;;;ACHxC,SAAS,UAAU,OAAuB;AACxC,QAAO,WAAW,SAAS,CAAC,OAAO,MAAM,CAAC,OAAO,MAAM;;;;;;;;;;;;;AAczD,SAAgB,mBACd,UACA,KACQ;CACR,MAAM,EACJ,aAAa,cACb,WAAW,YACX,cAAc,eACd,YAAY,aACZ,OAAO,QACP,GAAG,iBACD;AAQJ,QAAO,UAFM,UAAU,iBADJ,CAHO,iBAAiB,aAAa,EACnC,iBAAiB,IAAI,CAEU,CAAC,IAAI,UAAU,CAChB,CAAC;;;;;;;;;;AAatD,SAAgB,sBAAsB,QAAuC;CAC3E,MAAM,WAAW,mBAAmB,OAAO,UAAU,OAAO,IAAI;AAEhE,KAAI,OAAO,SAAS,gBAAgB,SAClC,QAAO;EACL,IAAI;EACJ,mBAAmB,OAAO,SAAS;EACnC,qBAAqB;EACtB;AAGH,QAAO;EACL,IAAI;EACJ,QAAQ;EACR,mBAAmB,OAAO,SAAS;EACnC,qBAAqB;EACtB;;;AAIH,eAAsB,gBAAgB,KAAoC;AAExE,QAAO,sBADK,MAAM,qBAAqB,IAAI,CACV"}
|