@prisma-next/family-sql 0.5.0-dev.9 → 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.
- package/README.md +2 -3
- package/dist/{authoring-type-constructors-BAR65pSK.mjs → authoring-type-constructors-F4JpCJl7.mjs} +14 -15
- package/dist/authoring-type-constructors-F4JpCJl7.mjs.map +1 -0
- package/dist/control-adapter.d.mts +26 -2
- package/dist/control-adapter.d.mts.map +1 -1
- package/dist/control-adapter.mjs +1 -1
- package/dist/control.d.mts +125 -45
- package/dist/control.d.mts.map +1 -1
- package/dist/control.mjs +1174 -54
- package/dist/control.mjs.map +1 -1
- package/dist/migration.d.mts +22 -24
- package/dist/migration.d.mts.map +1 -1
- package/dist/migration.mjs +25 -24
- package/dist/migration.mjs.map +1 -1
- package/dist/pack.d.mts +35 -23
- package/dist/pack.d.mts.map +1 -1
- package/dist/pack.mjs +3 -5
- package/dist/pack.mjs.map +1 -1
- package/dist/runtime.d.mts +19 -2
- package/dist/runtime.d.mts.map +1 -1
- package/dist/runtime.mjs +26 -4
- package/dist/runtime.mjs.map +1 -1
- package/dist/schema-verify.d.mts +4 -15
- package/dist/schema-verify.d.mts.map +1 -1
- package/dist/schema-verify.mjs +2 -3
- package/dist/test-utils.d.mts +2 -2
- package/dist/test-utils.mjs +2 -3
- package/dist/timestamp-now-generator-BWp8S2sa.mjs +86 -0
- package/dist/timestamp-now-generator-BWp8S2sa.mjs.map +1 -0
- package/dist/types-mhjAPuMn.d.mts +470 -0
- package/dist/types-mhjAPuMn.d.mts.map +1 -0
- package/dist/verify-pRYxnpiG.mjs +81 -0
- package/dist/verify-pRYxnpiG.mjs.map +1 -0
- package/dist/{verify-sql-schema-Ovz7RXR5.mjs → verify-sql-schema-1tDh3x5x.mjs} +18 -72
- package/dist/verify-sql-schema-1tDh3x5x.mjs.map +1 -0
- package/dist/{verify-sql-schema-BBhkqEDo.d.mts → verify-sql-schema-CPHiuYHR.d.mts} +2 -3
- package/dist/verify-sql-schema-CPHiuYHR.d.mts.map +1 -0
- package/dist/verify.d.mts +16 -21
- package/dist/verify.d.mts.map +1 -1
- package/dist/verify.mjs +2 -3
- package/package.json +23 -21
- package/src/core/authoring-field-presets.ts +35 -23
- package/src/core/control-adapter.ts +32 -0
- package/src/core/control-descriptor.ts +2 -1
- package/src/core/control-instance.ts +117 -30
- package/src/core/migrations/contract-to-schema-ir.ts +4 -29
- package/src/core/migrations/field-event-planner.ts +194 -0
- package/src/core/migrations/plan-helpers.ts +4 -0
- package/src/core/migrations/types.ts +207 -59
- package/src/core/operation-preview.ts +62 -0
- package/src/core/psl-contract-infer/default-mapping.ts +56 -0
- package/src/core/psl-contract-infer/name-transforms.ts +178 -0
- package/src/core/psl-contract-infer/postgres-default-mapping.ts +16 -0
- package/src/core/psl-contract-infer/postgres-type-map.ts +165 -0
- package/src/core/psl-contract-infer/printer-config.ts +55 -0
- package/src/core/psl-contract-infer/raw-default-parser.ts +91 -0
- package/src/core/psl-contract-infer/relation-inference.ts +196 -0
- package/src/core/psl-contract-infer/sql-schema-ir-to-psl-ast.ts +832 -0
- package/src/core/schema-verify/verify-helpers.ts +47 -70
- package/src/core/schema-verify/verify-sql-schema.ts +1 -6
- package/src/core/sql-migration.ts +25 -23
- package/src/core/timestamp-now-generator.ts +74 -0
- package/src/core/timestamp-now-runtime-generator.ts +24 -0
- package/src/core/verify.ts +46 -108
- package/src/exports/control.ts +11 -4
- package/src/exports/runtime.ts +2 -0
- package/src/exports/schema-verify.ts +0 -1
- package/src/exports/test-utils.ts +0 -1
- package/src/exports/verify.ts +1 -1
- package/dist/authoring-type-constructors-BAR65pSK.mjs.map +0 -1
- package/dist/types-C6K4mxDM.d.mts +0 -301
- package/dist/types-C6K4mxDM.d.mts.map +0 -1
- package/dist/verify-4GshvY4p.mjs +0 -122
- package/dist/verify-4GshvY4p.mjs.map +0 -1
- package/dist/verify-sql-schema-BBhkqEDo.d.mts.map +0 -1
- package/dist/verify-sql-schema-Ovz7RXR5.mjs.map +0 -1
|
@@ -13,13 +13,40 @@ import type {
|
|
|
13
13
|
PrimaryKey,
|
|
14
14
|
UniqueConstraint,
|
|
15
15
|
} from '@prisma-next/sql-contract/types';
|
|
16
|
-
import type {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
16
|
+
import type { SqlForeignKeyIR, SqlIndexIR, SqlUniqueIR } from '@prisma-next/sql-schema-ir/types';
|
|
17
|
+
|
|
18
|
+
function indexOptionsLooselyEqual(
|
|
19
|
+
a: Record<string, unknown> | undefined,
|
|
20
|
+
b: Record<string, unknown> | undefined,
|
|
21
|
+
): boolean {
|
|
22
|
+
const aKeys = a ? Object.keys(a).sort() : [];
|
|
23
|
+
const bKeys = b ? Object.keys(b).sort() : [];
|
|
24
|
+
if (aKeys.length !== bKeys.length) return false;
|
|
25
|
+
for (let i = 0; i < aKeys.length; i += 1) {
|
|
26
|
+
if (aKeys[i] !== bKeys[i]) return false;
|
|
27
|
+
}
|
|
28
|
+
if (aKeys.length === 0) return true;
|
|
29
|
+
for (const key of aKeys) {
|
|
30
|
+
// Postgres introspection returns reloptions values as raw strings (e.g.
|
|
31
|
+
// `'70'`, `'false'`), while contract option leaves are typed (number,
|
|
32
|
+
// boolean, string). Compare via String() so a contract `fillfactor: 70`
|
|
33
|
+
// matches an introspected `fillfactor: '70'` without a spurious mismatch.
|
|
34
|
+
if (
|
|
35
|
+
String((a as Record<string, unknown>)[key]) !== String((b as Record<string, unknown>)[key])
|
|
36
|
+
) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function indexExtrasMatch(
|
|
44
|
+
contractIndex: Index,
|
|
45
|
+
schemaIndex: { readonly type?: string; readonly options?: Record<string, unknown> },
|
|
46
|
+
): boolean {
|
|
47
|
+
if ((contractIndex.type ?? null) !== (schemaIndex.type ?? null)) return false;
|
|
48
|
+
return indexOptionsLooselyEqual(contractIndex.options, schemaIndex.options);
|
|
49
|
+
}
|
|
23
50
|
|
|
24
51
|
/**
|
|
25
52
|
* Compares two arrays of strings for equality (order-sensitive).
|
|
@@ -391,13 +418,19 @@ export function verifyIndexes(
|
|
|
391
418
|
|
|
392
419
|
// Check for any matching index (unique or non-unique)
|
|
393
420
|
// A unique index can satisfy a non-unique index requirement (stronger satisfies weaker)
|
|
394
|
-
const matchingIndex = schemaIndexes.find(
|
|
395
|
-
|
|
421
|
+
const matchingIndex = schemaIndexes.find(
|
|
422
|
+
(idx) =>
|
|
423
|
+
arraysEqual(idx.columns, contractIndex.columns) && indexExtrasMatch(contractIndex, idx),
|
|
396
424
|
);
|
|
397
425
|
|
|
398
|
-
// Also check if a unique constraint satisfies the index requirement
|
|
426
|
+
// Also check if a unique constraint satisfies the index requirement.
|
|
427
|
+
// Unique constraints carry no type/options of their own, so they can only
|
|
428
|
+
// satisfy a contract index that doesn't request a specific type/options.
|
|
399
429
|
const matchingUniqueConstraint =
|
|
400
|
-
!matchingIndex &&
|
|
430
|
+
!matchingIndex &&
|
|
431
|
+
contractIndex.type === undefined &&
|
|
432
|
+
contractIndex.options === undefined &&
|
|
433
|
+
schemaUniques.find((u) => arraysEqual(u.columns, contractIndex.columns));
|
|
401
434
|
|
|
402
435
|
if (!matchingIndex && !matchingUniqueConstraint) {
|
|
403
436
|
issues.push({
|
|
@@ -442,8 +475,9 @@ export function verifyIndexes(
|
|
|
442
475
|
continue;
|
|
443
476
|
}
|
|
444
477
|
|
|
445
|
-
const matchingIndex = contractIndexes.find(
|
|
446
|
-
|
|
478
|
+
const matchingIndex = contractIndexes.find(
|
|
479
|
+
(idx) =>
|
|
480
|
+
arraysEqual(idx.columns, schemaIndex.columns) && indexExtrasMatch(idx, schemaIndex),
|
|
447
481
|
);
|
|
448
482
|
|
|
449
483
|
if (!matchingIndex) {
|
|
@@ -471,63 +505,6 @@ export function verifyIndexes(
|
|
|
471
505
|
return nodes;
|
|
472
506
|
}
|
|
473
507
|
|
|
474
|
-
/**
|
|
475
|
-
* Verifies database dependencies are installed using component-owned verification hooks.
|
|
476
|
-
* Checks whether each dependency is satisfied by verifying its id is present in
|
|
477
|
-
* schema.dependencies (populated from introspection).
|
|
478
|
-
*
|
|
479
|
-
* Returns verification nodes for the tree.
|
|
480
|
-
*/
|
|
481
|
-
export function verifyDatabaseDependencies(
|
|
482
|
-
dependencies: ReadonlyArray<ComponentDatabaseDependency<unknown>>,
|
|
483
|
-
schema: SqlSchemaIR,
|
|
484
|
-
issues: SchemaIssue[],
|
|
485
|
-
): SchemaVerificationNode[] {
|
|
486
|
-
const nodes: SchemaVerificationNode[] = [];
|
|
487
|
-
const installedIds = new Set(schema.dependencies.map((d) => d.id));
|
|
488
|
-
|
|
489
|
-
for (const dependency of dependencies) {
|
|
490
|
-
const isSatisfied = installedIds.has(dependency.id);
|
|
491
|
-
const depPath = `dependencies.${dependency.id}`;
|
|
492
|
-
|
|
493
|
-
if (!isSatisfied) {
|
|
494
|
-
const depIssue: SchemaIssue = {
|
|
495
|
-
kind: 'dependency_missing',
|
|
496
|
-
dependencyId: dependency.id,
|
|
497
|
-
message: `Dependency "${dependency.id}" is missing from database`,
|
|
498
|
-
};
|
|
499
|
-
issues.push(depIssue);
|
|
500
|
-
const nodeMessage = depIssue.message;
|
|
501
|
-
nodes.push({
|
|
502
|
-
status: 'fail',
|
|
503
|
-
kind: 'databaseDependency',
|
|
504
|
-
name: dependency.label,
|
|
505
|
-
contractPath: depPath,
|
|
506
|
-
code: 'dependency_missing',
|
|
507
|
-
message: nodeMessage,
|
|
508
|
-
expected: undefined,
|
|
509
|
-
actual: undefined,
|
|
510
|
-
children: [],
|
|
511
|
-
});
|
|
512
|
-
} else {
|
|
513
|
-
// Dependency is satisfied
|
|
514
|
-
nodes.push({
|
|
515
|
-
status: 'pass',
|
|
516
|
-
kind: 'databaseDependency',
|
|
517
|
-
name: dependency.label,
|
|
518
|
-
contractPath: depPath,
|
|
519
|
-
code: '',
|
|
520
|
-
message: '',
|
|
521
|
-
expected: undefined,
|
|
522
|
-
actual: undefined,
|
|
523
|
-
children: [],
|
|
524
|
-
});
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
return nodes;
|
|
529
|
-
}
|
|
530
|
-
|
|
531
508
|
/**
|
|
532
509
|
* Computes counts of pass/warn/fail nodes by traversing the tree.
|
|
533
510
|
*/
|
|
@@ -22,11 +22,10 @@ import type {
|
|
|
22
22
|
import type { SqlSchemaIR } from '@prisma-next/sql-schema-ir/types';
|
|
23
23
|
import { ifDefined } from '@prisma-next/utils/defined';
|
|
24
24
|
import { extractCodecControlHooks } from '../assembly';
|
|
25
|
-
import {
|
|
25
|
+
import type { CodecControlHooks } from '../migrations/types';
|
|
26
26
|
import {
|
|
27
27
|
arraysEqual,
|
|
28
28
|
computeCounts,
|
|
29
|
-
verifyDatabaseDependencies,
|
|
30
29
|
verifyForeignKeys,
|
|
31
30
|
verifyIndexes,
|
|
32
31
|
verifyPrimaryKey,
|
|
@@ -166,10 +165,6 @@ export function verifySqlSchema(options: VerifySqlSchemaOptions): VerifyDatabase
|
|
|
166
165
|
});
|
|
167
166
|
}
|
|
168
167
|
|
|
169
|
-
const databaseDependencies = collectInitDependencies(options.frameworkComponents);
|
|
170
|
-
const dependencyStatuses = verifyDatabaseDependencies(databaseDependencies, schema, issues);
|
|
171
|
-
rootChildren.push(...dependencyStatuses);
|
|
172
|
-
|
|
173
168
|
const root = buildRootNode(rootChildren);
|
|
174
169
|
|
|
175
170
|
// Compute counts
|
|
@@ -1,34 +1,36 @@
|
|
|
1
|
+
import { deriveProvidedInvariants } from '@prisma-next/migration-tools/invariants';
|
|
1
2
|
import { Migration } from '@prisma-next/migration-tools/migration';
|
|
2
|
-
import type {
|
|
3
|
+
import type { SqlMigrationPlanOperation, SqlPlanTargetDetails } from './migrations/types';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Family-owned base class for SQL migrations.
|
|
6
7
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* on `SqlMigrationPlanOperation`. The type parameter is narrowed to
|
|
10
|
-
* `SqlPlanTargetDetails` so every target-specific shape must at minimum
|
|
11
|
-
* identify the object being targeted (schema + name); concrete targets
|
|
12
|
-
* extend the shape with their own fields.
|
|
8
|
+
* Generic in `TDetails` (family plan target details, e.g. Postgres vs SQLite)
|
|
9
|
+
* and in `TTargetId` (the literal target identifier, e.g. `'postgres'`).
|
|
13
10
|
*
|
|
14
|
-
*
|
|
15
|
-
* `
|
|
16
|
-
*
|
|
17
|
-
* `
|
|
18
|
-
* belongs on the subclass.
|
|
11
|
+
* Adapters (Postgres, SQLite, …) extend this with a concrete `TDetails` and
|
|
12
|
+
* a fixed `TTargetId` literal, so the public `Migration<TOp>` base sees the
|
|
13
|
+
* fully concrete operation shape. Target-free code in SQL family / tooling
|
|
14
|
+
* parameterises over `TDetails` (and usually `TTargetId = string`).
|
|
19
15
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* target descriptors rather than by family-id lookup, so adding one would
|
|
23
|
-
* be purely decorative. Introducing it later is a non-breaking superset.
|
|
24
|
-
*
|
|
25
|
-
* The operation type parameter is `AnySqlMigrationOperation<TDetails>` — the
|
|
26
|
-
* union of DDL-shaped `SqlMigrationPlanOperation` and `DataTransformOperation`
|
|
27
|
-
* — so subclasses can return a mix of schema operations (e.g. `setNotNull`,
|
|
28
|
-
* `addColumn`) and data-transform operations (e.g. `dataTransform`). Mirrors
|
|
29
|
-
* `MongoMigration`'s parameterization on `AnyMongoMigrationOperation`.
|
|
16
|
+
* Keeps target-free contract/runtime features in the family layer while
|
|
17
|
+
* letting adapters own target shape.
|
|
30
18
|
*/
|
|
31
19
|
export abstract class SqlMigration<
|
|
32
20
|
TDetails extends SqlPlanTargetDetails,
|
|
33
21
|
TTargetId extends string = string,
|
|
34
|
-
> extends Migration<
|
|
22
|
+
> extends Migration<SqlMigrationPlanOperation<TDetails>, 'sql', TTargetId> {
|
|
23
|
+
/**
|
|
24
|
+
* Sorted, deduplicated invariant ids declared by this migration's
|
|
25
|
+
* data-transform ops. Derived from `this.operations` so the field remains
|
|
26
|
+
* consistent with the operation list — planner-built plans (`db init`,
|
|
27
|
+
* `db update`) yield `[]` because they emit no data-transform ops.
|
|
28
|
+
*
|
|
29
|
+
* Required by `SqlMigrationPlan.providedInvariants` (tightened from
|
|
30
|
+
* optional at the SQL-family layer); the framework-level
|
|
31
|
+
* `MigrationPlan.providedInvariants?` stays optional.
|
|
32
|
+
*/
|
|
33
|
+
get providedInvariants(): readonly string[] {
|
|
34
|
+
return deriveProvidedInvariants(this.operations);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { AuthoringFieldPresetDescriptor } from '@prisma-next/framework-components/authoring';
|
|
2
|
+
import type { MutationDefaultGeneratorDescriptor } from '@prisma-next/framework-components/control';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Canonical id for the wall-clock-now mutation default generator.
|
|
6
|
+
*
|
|
7
|
+
* Owned by `family-sql` because that's where the generator lives. The
|
|
8
|
+
* id flows out from here to (1) the control-plane descriptor and the
|
|
9
|
+
* temporal field-preset pair below, (2) the runtime-plane sibling
|
|
10
|
+
* `timestamp-now-runtime-generator.ts`, and (3) authoring surfaces
|
|
11
|
+
* (PSL `temporal.updatedAt()`, TS `field.temporal.updatedAt()`) via
|
|
12
|
+
* the descriptor flow. Co-locating the constant with its only owner
|
|
13
|
+
* keeps the framework layer free of concrete generator ids.
|
|
14
|
+
*/
|
|
15
|
+
export const TIMESTAMP_NOW_GENERATOR_ID = 'timestampNow' as const;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Builds the canonical control-plane descriptor for the wall-clock-now
|
|
19
|
+
* mutation default generator. The descriptor's `id` and `buildPhases`
|
|
20
|
+
* are target-agnostic so PSL `temporal.updatedAt()` and TS
|
|
21
|
+
* `field.temporal.updatedAt()` lower to byte-identical contracts.
|
|
22
|
+
*
|
|
23
|
+
* `applicableCodecIds` is omitted: `timestampNow` is preset-only (not
|
|
24
|
+
* reachable via `@default(timestampNow())` lowering), and the codec is
|
|
25
|
+
* co-registered by the preset descriptor itself, so the
|
|
26
|
+
* `@default(...)` compatibility check has no role to play here.
|
|
27
|
+
*/
|
|
28
|
+
export function timestampNowControlDescriptor(): MutationDefaultGeneratorDescriptor {
|
|
29
|
+
return {
|
|
30
|
+
id: TIMESTAMP_NOW_GENERATOR_ID,
|
|
31
|
+
buildPhases: () => ({
|
|
32
|
+
onCreate: { kind: 'generator', id: TIMESTAMP_NOW_GENERATOR_ID },
|
|
33
|
+
onUpdate: { kind: 'generator', id: TIMESTAMP_NOW_GENERATOR_ID },
|
|
34
|
+
}),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Builds the canonical `temporal.{createdAt,updatedAt}` field-preset pair
|
|
40
|
+
* for a SQL target. `createdAt` lowers to a `now()` storage default;
|
|
41
|
+
* `updatedAt` lowers to the `timestampNow` execution generator on both
|
|
42
|
+
* `onCreate` and `onUpdate` (RD: "last modified time", non-null). Targets
|
|
43
|
+
* supply the codec/native-type pair that matches their timestamp column;
|
|
44
|
+
* everything else is shared so PSL `temporal.updatedAt()` and TS
|
|
45
|
+
* `field.temporal.updatedAt()` lower to byte-identical contracts across
|
|
46
|
+
* targets by construction.
|
|
47
|
+
*/
|
|
48
|
+
export function temporalAuthoringPresets<
|
|
49
|
+
const CodecId extends string,
|
|
50
|
+
const NativeType extends string,
|
|
51
|
+
>(input: { readonly codecId: CodecId; readonly nativeType: NativeType }) {
|
|
52
|
+
const { codecId, nativeType } = input;
|
|
53
|
+
return {
|
|
54
|
+
createdAt: {
|
|
55
|
+
kind: 'fieldPreset',
|
|
56
|
+
output: {
|
|
57
|
+
codecId,
|
|
58
|
+
nativeType,
|
|
59
|
+
default: { kind: 'function', expression: 'now()' },
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
updatedAt: {
|
|
63
|
+
kind: 'fieldPreset',
|
|
64
|
+
output: {
|
|
65
|
+
codecId,
|
|
66
|
+
nativeType,
|
|
67
|
+
executionDefaults: {
|
|
68
|
+
onCreate: { kind: 'generator', id: TIMESTAMP_NOW_GENERATOR_ID },
|
|
69
|
+
onUpdate: { kind: 'generator', id: TIMESTAMP_NOW_GENERATOR_ID },
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
} as const satisfies Record<string, AuthoringFieldPresetDescriptor>;
|
|
74
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { RuntimeMutationDefaultGenerator } from '@prisma-next/sql-runtime';
|
|
2
|
+
import { TIMESTAMP_NOW_GENERATOR_ID } from './timestamp-now-generator';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Builds the canonical runtime-plane generator for the wall-clock-now
|
|
6
|
+
* mutation default. Returns `new Date()`; semantics are target-agnostic
|
|
7
|
+
* so all SQL targets share this single implementation.
|
|
8
|
+
*
|
|
9
|
+
* Declares `stability: 'query'` so a single ORM bulk operation
|
|
10
|
+
* (e.g. `createAll([...])`) shares one timestamp across every row and
|
|
11
|
+
* every timestamp-defaulted column. Matches Prisma 6's `@updatedAt`
|
|
12
|
+
* semantics: one `new Date()` per lowered mutation, not per row.
|
|
13
|
+
*
|
|
14
|
+
* Lives in a runtime-plane-only module so the control-plane
|
|
15
|
+
* `timestamp-now-generator.ts` (descriptor + authoring presets) stays
|
|
16
|
+
* free of `@prisma-next/sql-runtime` imports.
|
|
17
|
+
*/
|
|
18
|
+
export function timestampNowRuntimeGenerator(): RuntimeMutationDefaultGenerator {
|
|
19
|
+
return {
|
|
20
|
+
id: TIMESTAMP_NOW_GENERATOR_ID,
|
|
21
|
+
generate: () => new Date(),
|
|
22
|
+
stability: 'query',
|
|
23
|
+
};
|
|
24
|
+
}
|
package/src/core/verify.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { ContractMarkerRecord } from '@prisma-next/contract/types';
|
|
2
|
-
import type { ControlDriverInstance } from '@prisma-next/framework-components/control';
|
|
3
2
|
import { type } from 'arktype';
|
|
4
3
|
|
|
5
4
|
const MetaSchema = type({ '[string]': 'unknown' });
|
|
@@ -28,6 +27,40 @@ function parseMeta(meta: unknown): Record<string, unknown> {
|
|
|
28
27
|
return result as Record<string, unknown>;
|
|
29
28
|
}
|
|
30
29
|
|
|
30
|
+
/**
|
|
31
|
+
* SQLite stores `contract_json` as TEXT, so the wire shape is a JSON string;
|
|
32
|
+
* Postgres uses `jsonb` and returns an already-parsed value. Normalize both
|
|
33
|
+
* here so `ContractMarkerRecord.contractJson` is always the structured form.
|
|
34
|
+
*/
|
|
35
|
+
function parseContractJson(value: unknown): unknown {
|
|
36
|
+
if (value === null || value === undefined) return null;
|
|
37
|
+
if (typeof value !== 'string') return value;
|
|
38
|
+
try {
|
|
39
|
+
return JSON.parse(value);
|
|
40
|
+
} catch {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Wire shape of a `prisma_contract.marker` row as it comes out of a SQL
|
|
47
|
+
* driver. Snake-cased to match the on-disk column names. Shared by every
|
|
48
|
+
* SQL target's `readMarker` so each runner doesn't redeclare it inline.
|
|
49
|
+
*/
|
|
50
|
+
export type ContractMarkerRow = {
|
|
51
|
+
core_hash: string;
|
|
52
|
+
profile_hash: string;
|
|
53
|
+
contract_json: unknown | null;
|
|
54
|
+
canonical_version: number | null;
|
|
55
|
+
updated_at: Date | string;
|
|
56
|
+
app_tag: string | null;
|
|
57
|
+
meta: unknown | null;
|
|
58
|
+
// SQLite stores arrays as JSON-TEXT, so this is `string` on the wire from
|
|
59
|
+
// a SQLite driver and `string[]` from a Postgres driver. Targets normalize
|
|
60
|
+
// before passing to `parseContractMarkerRow`.
|
|
61
|
+
invariants: unknown;
|
|
62
|
+
};
|
|
63
|
+
|
|
31
64
|
const ContractMarkerRowSchema = type({
|
|
32
65
|
core_hash: 'string',
|
|
33
66
|
profile_hash: 'string',
|
|
@@ -36,6 +69,7 @@ const ContractMarkerRowSchema = type({
|
|
|
36
69
|
'updated_at?': 'Date | string',
|
|
37
70
|
'app_tag?': 'string | null',
|
|
38
71
|
'meta?': 'unknown | null',
|
|
72
|
+
invariants: type('string').array(),
|
|
39
73
|
});
|
|
40
74
|
|
|
41
75
|
/**
|
|
@@ -49,120 +83,24 @@ export function parseContractMarkerRow(row: unknown): ContractMarkerRecord {
|
|
|
49
83
|
throw new Error(`Invalid contract marker row: ${messages}`);
|
|
50
84
|
}
|
|
51
85
|
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
canonical_version?: number | null;
|
|
57
|
-
updated_at?: Date | string;
|
|
58
|
-
app_tag?: string | null;
|
|
59
|
-
meta?: unknown | null;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const updatedAt = validatedRow.updated_at
|
|
63
|
-
? validatedRow.updated_at instanceof Date
|
|
64
|
-
? validatedRow.updated_at
|
|
65
|
-
: new Date(validatedRow.updated_at)
|
|
86
|
+
const updatedAt = result.updated_at
|
|
87
|
+
? result.updated_at instanceof Date
|
|
88
|
+
? result.updated_at
|
|
89
|
+
: new Date(result.updated_at)
|
|
66
90
|
: new Date();
|
|
67
91
|
|
|
68
92
|
return {
|
|
69
|
-
storageHash:
|
|
70
|
-
profileHash:
|
|
71
|
-
contractJson:
|
|
72
|
-
canonicalVersion:
|
|
93
|
+
storageHash: result.core_hash,
|
|
94
|
+
profileHash: result.profile_hash,
|
|
95
|
+
contractJson: parseContractJson(result.contract_json),
|
|
96
|
+
canonicalVersion: result.canonical_version ?? null,
|
|
73
97
|
updatedAt,
|
|
74
|
-
appTag:
|
|
75
|
-
meta: parseMeta(
|
|
98
|
+
appTag: result.app_tag ?? null,
|
|
99
|
+
meta: parseMeta(result.meta),
|
|
100
|
+
invariants: result.invariants,
|
|
76
101
|
};
|
|
77
102
|
}
|
|
78
103
|
|
|
79
|
-
/**
|
|
80
|
-
* Returns the SQL statement to read the contract marker.
|
|
81
|
-
* This is a migration-plane helper (no runtime imports).
|
|
82
|
-
* @internal - Used internally by readMarker(). Prefer readMarker() for Control Plane usage.
|
|
83
|
-
*/
|
|
84
|
-
export function readMarkerSql(): { readonly sql: string; readonly params: readonly unknown[] } {
|
|
85
|
-
return {
|
|
86
|
-
sql: `select
|
|
87
|
-
core_hash,
|
|
88
|
-
profile_hash,
|
|
89
|
-
contract_json,
|
|
90
|
-
canonical_version,
|
|
91
|
-
updated_at,
|
|
92
|
-
app_tag,
|
|
93
|
-
meta
|
|
94
|
-
from prisma_contract.marker
|
|
95
|
-
where id = $1`,
|
|
96
|
-
params: [1],
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Returns the SQL statement that probes for the existence of the marker table.
|
|
102
|
-
* Uses the SQL-standard `information_schema.tables` view so the query succeeds
|
|
103
|
-
* (returning zero rows) when the table has not been created yet — avoiding a
|
|
104
|
-
* `relation does not exist` error. Some Postgres wire-protocol implementations
|
|
105
|
-
* (e.g. PGlite's TCP proxy) do not fully recover from an extended-protocol
|
|
106
|
-
* parse error, so we probe first instead of relying on an error signal.
|
|
107
|
-
* @internal - Used internally by readMarker().
|
|
108
|
-
*/
|
|
109
|
-
export function markerTableExistsSql(): {
|
|
110
|
-
readonly sql: string;
|
|
111
|
-
readonly params: readonly unknown[];
|
|
112
|
-
} {
|
|
113
|
-
return {
|
|
114
|
-
sql: `select 1
|
|
115
|
-
from information_schema.tables
|
|
116
|
-
where table_schema = $1 and table_name = $2`,
|
|
117
|
-
params: ['prisma_contract', 'marker'],
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Reads the contract marker from the database using the provided driver.
|
|
123
|
-
* Returns the parsed marker record or null if no marker is found.
|
|
124
|
-
* This abstracts SQL-specific details from the Control Plane.
|
|
125
|
-
*
|
|
126
|
-
* @param driver - ControlDriverInstance instance for executing queries
|
|
127
|
-
* @returns Promise resolving to ContractMarkerRecord or null if marker not found
|
|
128
|
-
*/
|
|
129
|
-
export async function readMarker(
|
|
130
|
-
driver: ControlDriverInstance<'sql', string>,
|
|
131
|
-
): Promise<ContractMarkerRecord | null> {
|
|
132
|
-
// Probe for the marker table first so that a fresh database (no
|
|
133
|
-
// `prisma_contract` schema) returns null cleanly instead of surfacing a
|
|
134
|
-
// `relation does not exist` error. This keeps the control connection in a
|
|
135
|
-
// predictable state for driver implementations that are sensitive to
|
|
136
|
-
// extended-protocol parse errors.
|
|
137
|
-
const existsStatement = markerTableExistsSql();
|
|
138
|
-
const existsResult = await driver.query(existsStatement.sql, existsStatement.params);
|
|
139
|
-
if (existsResult.rows.length === 0) {
|
|
140
|
-
return null;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
const markerStatement = readMarkerSql();
|
|
144
|
-
const queryResult = await driver.query<{
|
|
145
|
-
core_hash: string;
|
|
146
|
-
profile_hash: string;
|
|
147
|
-
contract_json: unknown | null;
|
|
148
|
-
canonical_version: number | null;
|
|
149
|
-
updated_at: Date | string;
|
|
150
|
-
app_tag: string | null;
|
|
151
|
-
meta: unknown | null;
|
|
152
|
-
}>(markerStatement.sql, markerStatement.params);
|
|
153
|
-
|
|
154
|
-
if (queryResult.rows.length === 0) {
|
|
155
|
-
return null;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const markerRow = queryResult.rows[0];
|
|
159
|
-
if (!markerRow) {
|
|
160
|
-
throw new Error('Database query returned unexpected result structure');
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
return parseContractMarkerRow(markerRow);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
104
|
/**
|
|
167
105
|
* Collects supported codec type IDs from adapter and extension manifests.
|
|
168
106
|
* Returns a sorted, unique array of type IDs that are declared in the manifests.
|
package/src/exports/control.ts
CHANGED
|
@@ -24,6 +24,8 @@ export {
|
|
|
24
24
|
contractToSchemaIR,
|
|
25
25
|
detectDestructiveChanges,
|
|
26
26
|
} from '../core/migrations/contract-to-schema-ir';
|
|
27
|
+
export type { PlanFieldEventOperationsOptions } from '../core/migrations/field-event-planner';
|
|
28
|
+
export { planFieldEventOperations } from '../core/migrations/field-event-planner';
|
|
27
29
|
export {
|
|
28
30
|
createMigrationPlan,
|
|
29
31
|
plannerFailure,
|
|
@@ -33,12 +35,14 @@ export {
|
|
|
33
35
|
} from '../core/migrations/plan-helpers';
|
|
34
36
|
export { INIT_ADDITIVE_POLICY } from '../core/migrations/policies';
|
|
35
37
|
export type {
|
|
36
|
-
AnySqlMigrationOperation,
|
|
37
38
|
CodecControlHooks,
|
|
38
|
-
ComponentDatabaseDependencies,
|
|
39
|
-
ComponentDatabaseDependency,
|
|
40
39
|
CreateSqlMigrationPlanOptions,
|
|
41
40
|
ExpandNativeTypeInput,
|
|
41
|
+
FieldEvent,
|
|
42
|
+
FieldEventContext,
|
|
43
|
+
MultiSpaceRunnerFailure,
|
|
44
|
+
MultiSpaceRunnerResult,
|
|
45
|
+
MultiSpaceRunnerSuccessValue,
|
|
42
46
|
ResolveIdentityValueInput,
|
|
43
47
|
SqlControlAdapterDescriptor,
|
|
44
48
|
SqlControlExtensionDescriptor,
|
|
@@ -66,6 +70,9 @@ export type {
|
|
|
66
70
|
SqlPlanTargetDetails,
|
|
67
71
|
StorageTypePlanResult,
|
|
68
72
|
} from '../core/migrations/types';
|
|
69
|
-
export {
|
|
73
|
+
export {
|
|
74
|
+
temporalAuthoringPresets,
|
|
75
|
+
timestampNowControlDescriptor,
|
|
76
|
+
} from '../core/timestamp-now-generator';
|
|
70
77
|
|
|
71
78
|
export default new SqlFamilyDescriptor();
|
package/src/exports/runtime.ts
CHANGED
package/src/exports/verify.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { type ContractMarkerRow, parseContractMarkerRow } from '../core/verify';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"authoring-type-constructors-BAR65pSK.mjs","names":[],"sources":["../src/core/authoring-field-presets.ts","../src/core/authoring-type-constructors.ts"],"sourcesContent":["import type { AuthoringFieldNamespace } from '@prisma-next/framework-components/authoring';\n\n/**\n * Family-level SQL authoring field presets.\n *\n * Only presets whose codec IDs align with the ID generator metadata live here\n * (see `@prisma-next/ids`). These presets are target-agnostic because the\n * generator metadata fixes their codec/native-type to `sql/char@1`\n * (`character`) regardless of target, and the PSL interpreter lets the\n * generator override the scalar descriptor.\n *\n * Scalar presets that map to target-specific codecs (e.g. `text`, `int`,\n * `boolean`, `dateTime`) are contributed by the target pack (see\n * `postgresAuthoringFieldPresets` in `@prisma-next/target-postgres`) so the\n * TS callback surface and the PSL scalar surface lower to byte-identical\n * contracts for the active target.\n */\n\nconst CHARACTER_CODEC_ID = 'sql/char@1';\nconst CHARACTER_NATIVE_TYPE = 'character';\n\nconst nanoidOptionsArgument = {\n kind: 'object',\n optional: true,\n properties: {\n size: {\n kind: 'number',\n optional: true,\n integer: true,\n minimum: 2,\n maximum: 255,\n },\n },\n} as const;\n\nexport const sqlFamilyAuthoringFieldPresets = {\n uuid: {\n kind: 'fieldPreset',\n output: {\n codecId: CHARACTER_CODEC_ID,\n nativeType: CHARACTER_NATIVE_TYPE,\n typeParams: {\n length: 36,\n },\n },\n },\n ulid: {\n kind: 'fieldPreset',\n output: {\n codecId: CHARACTER_CODEC_ID,\n nativeType: CHARACTER_NATIVE_TYPE,\n typeParams: {\n length: 26,\n },\n },\n },\n nanoid: {\n kind: 'fieldPreset',\n args: [nanoidOptionsArgument],\n output: {\n codecId: CHARACTER_CODEC_ID,\n nativeType: CHARACTER_NATIVE_TYPE,\n typeParams: {\n length: {\n kind: 'arg',\n index: 0,\n path: ['size'],\n default: 21,\n },\n },\n },\n },\n cuid2: {\n kind: 'fieldPreset',\n output: {\n codecId: CHARACTER_CODEC_ID,\n nativeType: CHARACTER_NATIVE_TYPE,\n typeParams: {\n length: 24,\n },\n },\n },\n ksuid: {\n kind: 'fieldPreset',\n output: {\n codecId: CHARACTER_CODEC_ID,\n nativeType: CHARACTER_NATIVE_TYPE,\n typeParams: {\n length: 27,\n },\n },\n },\n id: {\n uuidv4: {\n kind: 'fieldPreset',\n output: {\n codecId: CHARACTER_CODEC_ID,\n nativeType: CHARACTER_NATIVE_TYPE,\n typeParams: {\n length: 36,\n },\n executionDefault: {\n kind: 'generator',\n id: 'uuidv4',\n },\n id: true,\n },\n },\n uuidv7: {\n kind: 'fieldPreset',\n output: {\n codecId: CHARACTER_CODEC_ID,\n nativeType: CHARACTER_NATIVE_TYPE,\n typeParams: {\n length: 36,\n },\n executionDefault: {\n kind: 'generator',\n id: 'uuidv7',\n },\n id: true,\n },\n },\n ulid: {\n kind: 'fieldPreset',\n output: {\n codecId: CHARACTER_CODEC_ID,\n nativeType: CHARACTER_NATIVE_TYPE,\n typeParams: {\n length: 26,\n },\n executionDefault: {\n kind: 'generator',\n id: 'ulid',\n },\n id: true,\n },\n },\n nanoid: {\n kind: 'fieldPreset',\n args: [nanoidOptionsArgument],\n output: {\n codecId: CHARACTER_CODEC_ID,\n nativeType: CHARACTER_NATIVE_TYPE,\n typeParams: {\n length: {\n kind: 'arg',\n index: 0,\n path: ['size'],\n default: 21,\n },\n },\n executionDefault: {\n kind: 'generator',\n id: 'nanoid',\n params: {\n size: {\n kind: 'arg',\n index: 0,\n path: ['size'],\n },\n },\n },\n id: true,\n },\n },\n cuid2: {\n kind: 'fieldPreset',\n output: {\n codecId: CHARACTER_CODEC_ID,\n nativeType: CHARACTER_NATIVE_TYPE,\n typeParams: {\n length: 24,\n },\n executionDefault: {\n kind: 'generator',\n id: 'cuid2',\n },\n id: true,\n },\n },\n ksuid: {\n kind: 'fieldPreset',\n output: {\n codecId: CHARACTER_CODEC_ID,\n nativeType: CHARACTER_NATIVE_TYPE,\n typeParams: {\n length: 27,\n },\n executionDefault: {\n kind: 'generator',\n id: 'ksuid',\n },\n id: true,\n },\n },\n },\n} as const satisfies AuthoringFieldNamespace;\n","import type { AuthoringTypeNamespace } from '@prisma-next/framework-components/authoring';\n\nexport const sqlFamilyAuthoringTypes = {\n sql: {\n String: {\n kind: 'typeConstructor',\n args: [{ kind: 'number', name: 'length', integer: true, minimum: 1, maximum: 10485760 }],\n output: {\n codecId: 'sql/varchar@1',\n nativeType: 'character varying',\n typeParams: {\n length: { kind: 'arg', index: 0 },\n },\n },\n },\n },\n} as const satisfies AuthoringTypeNamespace;\n"],"mappings":";;;;;;;;;;;;;;;;AAkBA,MAAM,qBAAqB;AAC3B,MAAM,wBAAwB;AAE9B,MAAM,wBAAwB;CAC5B,MAAM;CACN,UAAU;CACV,YAAY,EACV,MAAM;EACJ,MAAM;EACN,UAAU;EACV,SAAS;EACT,SAAS;EACT,SAAS;EACV,EACF;CACF;AAED,MAAa,iCAAiC;CAC5C,MAAM;EACJ,MAAM;EACN,QAAQ;GACN,SAAS;GACT,YAAY;GACZ,YAAY,EACV,QAAQ,IACT;GACF;EACF;CACD,MAAM;EACJ,MAAM;EACN,QAAQ;GACN,SAAS;GACT,YAAY;GACZ,YAAY,EACV,QAAQ,IACT;GACF;EACF;CACD,QAAQ;EACN,MAAM;EACN,MAAM,CAAC,sBAAsB;EAC7B,QAAQ;GACN,SAAS;GACT,YAAY;GACZ,YAAY,EACV,QAAQ;IACN,MAAM;IACN,OAAO;IACP,MAAM,CAAC,OAAO;IACd,SAAS;IACV,EACF;GACF;EACF;CACD,OAAO;EACL,MAAM;EACN,QAAQ;GACN,SAAS;GACT,YAAY;GACZ,YAAY,EACV,QAAQ,IACT;GACF;EACF;CACD,OAAO;EACL,MAAM;EACN,QAAQ;GACN,SAAS;GACT,YAAY;GACZ,YAAY,EACV,QAAQ,IACT;GACF;EACF;CACD,IAAI;EACF,QAAQ;GACN,MAAM;GACN,QAAQ;IACN,SAAS;IACT,YAAY;IACZ,YAAY,EACV,QAAQ,IACT;IACD,kBAAkB;KAChB,MAAM;KACN,IAAI;KACL;IACD,IAAI;IACL;GACF;EACD,QAAQ;GACN,MAAM;GACN,QAAQ;IACN,SAAS;IACT,YAAY;IACZ,YAAY,EACV,QAAQ,IACT;IACD,kBAAkB;KAChB,MAAM;KACN,IAAI;KACL;IACD,IAAI;IACL;GACF;EACD,MAAM;GACJ,MAAM;GACN,QAAQ;IACN,SAAS;IACT,YAAY;IACZ,YAAY,EACV,QAAQ,IACT;IACD,kBAAkB;KAChB,MAAM;KACN,IAAI;KACL;IACD,IAAI;IACL;GACF;EACD,QAAQ;GACN,MAAM;GACN,MAAM,CAAC,sBAAsB;GAC7B,QAAQ;IACN,SAAS;IACT,YAAY;IACZ,YAAY,EACV,QAAQ;KACN,MAAM;KACN,OAAO;KACP,MAAM,CAAC,OAAO;KACd,SAAS;KACV,EACF;IACD,kBAAkB;KAChB,MAAM;KACN,IAAI;KACJ,QAAQ,EACN,MAAM;MACJ,MAAM;MACN,OAAO;MACP,MAAM,CAAC,OAAO;MACf,EACF;KACF;IACD,IAAI;IACL;GACF;EACD,OAAO;GACL,MAAM;GACN,QAAQ;IACN,SAAS;IACT,YAAY;IACZ,YAAY,EACV,QAAQ,IACT;IACD,kBAAkB;KAChB,MAAM;KACN,IAAI;KACL;IACD,IAAI;IACL;GACF;EACD,OAAO;GACL,MAAM;GACN,QAAQ;IACN,SAAS;IACT,YAAY;IACZ,YAAY,EACV,QAAQ,IACT;IACD,kBAAkB;KAChB,MAAM;KACN,IAAI;KACL;IACD,IAAI;IACL;GACF;EACF;CACF;;;;ACnMD,MAAa,0BAA0B,EACrC,KAAK,EACH,QAAQ;CACN,MAAM;CACN,MAAM,CAAC;EAAE,MAAM;EAAU,MAAM;EAAU,SAAS;EAAM,SAAS;EAAG,SAAS;EAAU,CAAC;CACxF,QAAQ;EACN,SAAS;EACT,YAAY;EACZ,YAAY,EACV,QAAQ;GAAE,MAAM;GAAO,OAAO;GAAG,EAClC;EACF;CACF,EACF,EACF"}
|