@prisma-next/target-postgres 0.14.0-dev.4 → 0.14.0-dev.41
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/dist/authoring-BX1WHIdN.mjs +220 -0
- package/dist/authoring-BX1WHIdN.mjs.map +1 -0
- package/dist/canonicalize-Bn3BM1Cn.mjs +46 -0
- package/dist/canonicalize-Bn3BM1Cn.mjs.map +1 -0
- package/dist/canonicalize-CAbXtVtB.d.mts +37 -0
- package/dist/canonicalize-CAbXtVtB.d.mts.map +1 -0
- package/dist/{codec-types-DHCkwPKE.d.mts → codec-types-CueCoGD0.d.mts} +2 -2
- package/dist/{codec-types-DHCkwPKE.d.mts.map → codec-types-CueCoGD0.d.mts.map} +1 -1
- package/dist/codec-types.d.mts +1 -1
- package/dist/{codecs--0A5_4Bq.d.mts → codecs-Ca8upop6.d.mts} +12 -1
- package/dist/codecs-Ca8upop6.d.mts.map +1 -0
- package/dist/codecs.d.mts +1 -1
- package/dist/codecs.mjs +34 -1
- package/dist/codecs.mjs.map +1 -1
- package/dist/contract-free.d.mts +8 -3
- package/dist/contract-free.d.mts.map +1 -1
- package/dist/contract-free.mjs +3 -3
- package/dist/control.d.mts +1 -1
- package/dist/control.d.mts.map +1 -1
- package/dist/control.mjs +639 -21
- package/dist/control.mjs.map +1 -1
- package/dist/data-transform-BOWpliq8.mjs.map +1 -1
- package/dist/{data-transform-DDgWdB5o.d.mts → data-transform-qKy1U5V9.d.mts} +2 -2
- package/dist/{data-transform-DDgWdB5o.d.mts.map → data-transform-qKy1U5V9.d.mts.map} +1 -1
- package/dist/data-transform.d.mts +1 -1
- package/dist/{ddl-QDyOSeLc.mjs → ddl-BmLIe1z_.mjs} +62 -5
- package/dist/ddl-BmLIe1z_.mjs.map +1 -0
- package/dist/ddl.d.mts +3 -2
- package/dist/ddl.mjs +2 -2
- package/dist/{default-normalizer-DyyCHQWs.mjs → default-normalizer-Dug8Fez9.mjs} +119 -2
- package/dist/default-normalizer-Dug8Fez9.mjs.map +1 -0
- package/dist/default-normalizer.d.mts +1 -1
- package/dist/default-normalizer.d.mts.map +1 -1
- package/dist/default-normalizer.mjs +1 -1
- package/dist/descriptor-meta-runtime-UwYdV8fD.mjs +12 -0
- package/dist/descriptor-meta-runtime-UwYdV8fD.mjs.map +1 -0
- package/dist/descriptor-meta-tjatyhtZ.mjs +18 -0
- package/dist/descriptor-meta-tjatyhtZ.mjs.map +1 -0
- package/dist/{issue-planner-DL6g3CmE.mjs → issue-planner-DbFvaAgu.mjs} +35 -10
- package/dist/issue-planner-DbFvaAgu.mjs.map +1 -0
- package/dist/issue-planner.d.mts +3 -3
- package/dist/issue-planner.d.mts.map +1 -1
- package/dist/issue-planner.mjs +1 -1
- package/dist/migration.d.mts +5 -5
- package/dist/migration.mjs +4 -4
- package/dist/nodes-CSkPX3rI.d.mts +120 -0
- package/dist/nodes-CSkPX3rI.d.mts.map +1 -0
- package/dist/{nodes-Bbhs2rwj.mjs → nodes-Dh__NS58.mjs} +56 -2
- package/dist/nodes-Dh__NS58.mjs.map +1 -0
- package/dist/{op-factory-call-DmQEc3XV.d.mts → op-factory-call-CJWUxyUW.d.mts} +44 -13
- package/dist/op-factory-call-CJWUxyUW.d.mts.map +1 -0
- package/dist/{op-factory-call-D_p5vxwt.mjs → op-factory-call-t65c4jyY.mjs} +201 -28
- package/dist/op-factory-call-t65c4jyY.mjs.map +1 -0
- package/dist/op-factory-call.d.mts +1 -1
- package/dist/op-factory-call.mjs +1 -1
- package/dist/pack.d.mts +74 -2
- package/dist/pack.d.mts.map +1 -1
- package/dist/pack.mjs +1 -1
- package/dist/planner-CG2pLHk-.mjs +640 -0
- package/dist/planner-CG2pLHk-.mjs.map +1 -0
- package/dist/{planner-ddl-builders-B2wOwLqI.mjs → planner-ddl-builders-B8Nn6nHN.mjs} +12 -27
- package/dist/planner-ddl-builders-B8Nn6nHN.mjs.map +1 -0
- package/dist/planner-ddl-builders.d.mts +2 -4
- package/dist/planner-ddl-builders.d.mts.map +1 -1
- package/dist/planner-ddl-builders.mjs +2 -2
- package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs → planner-produced-postgres-migration-BolD0Mvc.mjs} +3 -3
- package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs.map → planner-produced-postgres-migration-BolD0Mvc.mjs.map} +1 -1
- package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts → planner-produced-postgres-migration-Cz9--XOT.d.mts} +4 -4
- package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts.map → planner-produced-postgres-migration-Cz9--XOT.d.mts.map} +1 -1
- package/dist/planner-produced-postgres-migration.d.mts +1 -1
- package/dist/planner-produced-postgres-migration.mjs +1 -1
- package/dist/{planner-sql-checks-jqUUGyQR.mjs → planner-sql-checks-DE9c4519.mjs} +2 -2
- package/dist/{planner-sql-checks-jqUUGyQR.mjs.map → planner-sql-checks-DE9c4519.mjs.map} +1 -1
- package/dist/planner-sql-checks.mjs +1 -1
- package/dist/{planner-target-details-CIY6tLeo.d.mts → planner-target-details-HkP4RrRO.d.mts} +2 -2
- package/dist/planner-target-details-HkP4RrRO.d.mts.map +1 -0
- package/dist/planner-target-details.d.mts +1 -1
- package/dist/planner.d.mts +82 -4
- package/dist/planner.d.mts.map +1 -1
- package/dist/planner.mjs +2 -2
- package/dist/{postgres-contract-serializer-k3TAcPMY.mjs → postgres-contract-view-BegduLr6.mjs} +50 -23
- package/dist/postgres-contract-view-BegduLr6.mjs.map +1 -0
- package/dist/postgres-contract-view-C9j5cqP0.d.mts +34 -0
- package/dist/postgres-contract-view-C9j5cqP0.d.mts.map +1 -0
- package/dist/postgres-database-schema-node-os8ogEap.d.mts +183 -0
- package/dist/postgres-database-schema-node-os8ogEap.d.mts.map +1 -0
- package/dist/{postgres-migration-B5jKrXv3.mjs → postgres-migration-BfMmK6tv.mjs} +21 -2
- package/dist/postgres-migration-BfMmK6tv.mjs.map +1 -0
- package/dist/{postgres-migration-Y4YBJqkS.d.mts → postgres-migration-D7kNVWVG.d.mts} +19 -6
- package/dist/postgres-migration-D7kNVWVG.d.mts.map +1 -0
- package/dist/postgres-rls-policy--CSddvMR.d.mts +52 -0
- package/dist/postgres-rls-policy--CSddvMR.d.mts.map +1 -0
- package/dist/{postgres-schema-COGZ1ark.mjs → postgres-schema-BLIHBEfX.mjs} +108 -5
- package/dist/postgres-schema-BLIHBEfX.mjs.map +1 -0
- package/dist/postgres-schema-BywoeKlt.d.mts +189 -0
- package/dist/postgres-schema-BywoeKlt.d.mts.map +1 -0
- package/dist/postgres-table-schema-node-1nNc8XDT.mjs +248 -0
- package/dist/postgres-table-schema-node-1nNc8XDT.mjs.map +1 -0
- package/dist/render-ops.d.mts +1 -1
- package/dist/{render-typescript-KMgosran.mjs → render-typescript-7yqILcwr.mjs} +45 -16
- package/dist/render-typescript-7yqILcwr.mjs.map +1 -0
- package/dist/render-typescript.mjs +1 -1
- package/dist/resolve-ddl-schema-CL1RR1mx.mjs +20 -0
- package/dist/resolve-ddl-schema-CL1RR1mx.mjs.map +1 -0
- package/dist/rls-canonicalize.d.mts +2 -0
- package/dist/rls-canonicalize.mjs +2 -0
- package/dist/runtime.d.mts +7 -6
- package/dist/runtime.d.mts.map +1 -1
- package/dist/runtime.mjs +3 -3
- package/dist/runtime.mjs.map +1 -1
- package/dist/schema-ir-annotations.d.mts +14 -0
- package/dist/schema-ir-annotations.d.mts.map +1 -0
- package/dist/schema-ir-annotations.mjs +2 -0
- package/dist/types.d.mts +4 -140
- package/dist/types.mjs +3 -2
- package/package.json +22 -17
- package/src/contract-free/checks.ts +73 -0
- package/src/contract-free/ddl.ts +45 -1
- package/src/core/authoring.ts +115 -1
- package/src/core/codecs.ts +35 -1
- package/src/core/ddl/nodes.ts +94 -2
- package/src/core/default-normalizer.ts +122 -2
- package/src/core/descriptor-meta.ts +4 -0
- package/src/core/entity-kinds.ts +16 -0
- package/src/core/migrations/bound-schema.ts +10 -0
- package/src/core/migrations/contract-to-postgres-database-schema-node.ts +125 -0
- package/src/core/migrations/control-policy.ts +3 -0
- package/src/core/migrations/diff-database-schema.ts +191 -0
- package/src/core/migrations/issue-planner.ts +44 -3
- package/src/core/migrations/op-factory-call.ts +109 -21
- package/src/core/migrations/operations/columns.ts +24 -12
- package/src/core/migrations/operations/data-transform.ts +6 -0
- package/src/core/migrations/operations/rls.ts +106 -0
- package/src/core/migrations/operations/shared.ts +0 -30
- package/src/core/migrations/planner-ddl-builders.ts +15 -56
- package/src/core/migrations/planner-recipes.ts +31 -13
- package/src/core/migrations/planner-strategies.ts +3 -11
- package/src/core/migrations/planner-target-details.ts +3 -1
- package/src/core/migrations/planner.ts +165 -33
- package/src/core/migrations/postgres-migration.ts +34 -4
- package/src/core/migrations/render-typescript.ts +42 -15
- package/src/core/migrations/resolve-ddl-schema.ts +24 -0
- package/src/core/migrations/runner.ts +13 -16
- package/src/core/migrations/verify-postgres-namespaces.ts +24 -19
- package/src/core/postgres-contract-serializer.ts +50 -33
- package/src/core/postgres-contract-view.ts +48 -0
- package/src/core/postgres-rls-policy.ts +64 -0
- package/src/core/postgres-role.ts +37 -0
- package/src/core/postgres-schema.ts +37 -13
- package/src/core/postgres-validators.ts +20 -0
- package/src/core/psl-infer/infer-psl-contract.ts +834 -0
- package/src/core/psl-infer/postgres-default-mapping.ts +16 -0
- package/src/core/psl-infer/postgres-type-map.ts +157 -0
- package/src/core/rls/canonicalize.ts +48 -0
- package/src/core/schema-ir/postgres-database-schema-node.ts +63 -0
- package/src/core/schema-ir/postgres-namespace-schema-node.ts +61 -0
- package/src/core/schema-ir/postgres-policy-schema-node.ts +93 -0
- package/src/core/schema-ir/postgres-role-schema-node.ts +66 -0
- package/src/core/schema-ir/postgres-table-schema-node.ts +107 -0
- package/src/core/schema-ir/schema-node-kinds.ts +28 -0
- package/src/exports/contract-free.ts +7 -1
- package/src/exports/control.ts +39 -12
- package/src/exports/ddl.ts +4 -0
- package/src/exports/migration.ts +1 -0
- package/src/exports/planner-ddl-builders.ts +0 -2
- package/src/exports/planner.ts +2 -0
- package/src/exports/rls-canonicalize.ts +6 -0
- package/src/exports/runtime.ts +1 -0
- package/src/exports/schema-ir-annotations.ts +1 -0
- package/src/exports/types.ts +28 -0
- package/dist/codecs--0A5_4Bq.d.mts.map +0 -1
- package/dist/ddl-QDyOSeLc.mjs.map +0 -1
- package/dist/default-normalizer-DyyCHQWs.mjs.map +0 -1
- package/dist/descriptor-meta-CpGygXpI.mjs +0 -140
- package/dist/descriptor-meta-CpGygXpI.mjs.map +0 -1
- package/dist/issue-planner-DL6g3CmE.mjs.map +0 -1
- package/dist/nodes-Bbhs2rwj.mjs.map +0 -1
- package/dist/nodes-pLeLgdis.d.mts +0 -67
- package/dist/nodes-pLeLgdis.d.mts.map +0 -1
- package/dist/op-factory-call-D_p5vxwt.mjs.map +0 -1
- package/dist/op-factory-call-DmQEc3XV.d.mts.map +0 -1
- package/dist/planner-Bs_baQax.mjs +0 -324
- package/dist/planner-Bs_baQax.mjs.map +0 -1
- package/dist/planner-ddl-builders-B2wOwLqI.mjs.map +0 -1
- package/dist/planner-target-details-CIY6tLeo.d.mts.map +0 -1
- package/dist/postgres-contract-serializer-k3TAcPMY.mjs.map +0 -1
- package/dist/postgres-migration-B5jKrXv3.mjs.map +0 -1
- package/dist/postgres-migration-Y4YBJqkS.d.mts.map +0 -1
- package/dist/postgres-schema-COGZ1ark.mjs.map +0 -1
- package/dist/render-typescript-KMgosran.mjs.map +0 -1
- package/dist/types.d.mts.map +0 -1
package/dist/control.mjs
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { t as postgresTargetDescriptorMeta } from "./descriptor-meta-
|
|
2
|
-
import {
|
|
3
|
-
import { t as
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { t as PostgresContractSerializer } from "./postgres-contract-serializer-k3TAcPMY.mjs";
|
|
1
|
+
import { t as postgresTargetDescriptorMeta } from "./descriptor-meta-tjatyhtZ.mjs";
|
|
2
|
+
import { a as PostgresDatabaseSchemaNode } from "./postgres-table-schema-node-1nNc8XDT.mjs";
|
|
3
|
+
import { i as contractToPostgresDatabaseSchemaNode, n as diffPostgresDatabaseSchema, r as verifyPostgresDatabaseSchema, t as createPostgresMigrationPlanner } from "./planner-CG2pLHk-.mjs";
|
|
4
|
+
import { r as renderDefaultLiteral } from "./planner-ddl-builders-B8Nn6nHN.mjs";
|
|
5
|
+
import { n as PostgresContractSerializer } from "./postgres-contract-view-BegduLr6.mjs";
|
|
7
6
|
import { blindCast } from "@prisma-next/utils/casts";
|
|
8
|
-
import {
|
|
7
|
+
import { extractCodecControlHooks, runnerFailure, runnerSuccess } from "@prisma-next/family-sql/control";
|
|
9
8
|
import { ifDefined } from "@prisma-next/utils/defined";
|
|
10
|
-
import { verifySqlSchema } from "@prisma-next/family-sql/schema-verify";
|
|
11
9
|
import { UNBOUND_NAMESPACE_ID } from "@prisma-next/framework-components/ir";
|
|
12
|
-
import { notOk, ok, okVoid } from "@prisma-next/utils/result";
|
|
13
10
|
import { APP_SPACE_ID } from "@prisma-next/framework-components/control";
|
|
14
|
-
import {
|
|
11
|
+
import { notOk, ok, okVoid } from "@prisma-next/utils/result";
|
|
15
12
|
import { SqlSchemaVerifierBase } from "@prisma-next/family-sql/ir";
|
|
13
|
+
import { SqlQueryError } from "@prisma-next/sql-errors";
|
|
14
|
+
import { inferRelations, mapDefault, parseRawDefault, toFieldName, toModelName, toNamedTypeName } from "@prisma-next/family-sql/psl-infer";
|
|
15
|
+
import { UNSPECIFIED_PSL_NAMESPACE_ID, makePslNamespace, makePslNamespaceEntries } from "@prisma-next/framework-components/psl-ast";
|
|
16
16
|
//#region src/core/migrations/runner.ts
|
|
17
17
|
const LOCK_DOMAIN = "prisma_next.contract.marker";
|
|
18
18
|
/**
|
|
@@ -74,23 +74,19 @@ var PostgresMigrationRunner = class {
|
|
|
74
74
|
applyValue = applyResult.value;
|
|
75
75
|
}
|
|
76
76
|
if (space === APP_SPACE_ID) {
|
|
77
|
-
const
|
|
77
|
+
const schemaNode = await this.family.introspect({
|
|
78
78
|
driver,
|
|
79
79
|
contract: options.destinationContract
|
|
80
80
|
});
|
|
81
|
-
const schemaVerifyResult =
|
|
81
|
+
const schemaVerifyResult = this.family.verifySchema({
|
|
82
82
|
contract: options.destinationContract,
|
|
83
|
-
schema:
|
|
83
|
+
schema: schemaNode,
|
|
84
84
|
strict: options.strictVerification ?? true,
|
|
85
|
-
|
|
86
|
-
typeMetadataRegistry: this.family.typeMetadataRegistry,
|
|
87
|
-
frameworkComponents: options.frameworkComponents,
|
|
88
|
-
normalizeDefault: parsePostgresDefault,
|
|
89
|
-
normalizeNativeType: normalizeSchemaNativeType
|
|
85
|
+
frameworkComponents: options.frameworkComponents
|
|
90
86
|
});
|
|
91
87
|
if (!schemaVerifyResult.ok) return runnerFailure("SCHEMA_VERIFY_FAILED", schemaVerifyResult.summary, {
|
|
92
88
|
why: "The resulting database schema does not satisfy the destination contract.",
|
|
93
|
-
meta: { issues: schemaVerifyResult.schema.issues }
|
|
89
|
+
meta: { issues: [...schemaVerifyResult.schema.issues, ...schemaVerifyResult.schema.schemaDiffIssues] }
|
|
94
90
|
});
|
|
95
91
|
}
|
|
96
92
|
const incomingInvariants = options.plan.providedInvariants ?? [];
|
|
@@ -408,6 +404,605 @@ var PostgresSchemaVerifier = class extends SqlSchemaVerifierBase {
|
|
|
408
404
|
}
|
|
409
405
|
};
|
|
410
406
|
//#endregion
|
|
407
|
+
//#region src/core/psl-infer/postgres-default-mapping.ts
|
|
408
|
+
const POSTGRES_FUNCTION_ATTRIBUTES = { "gen_random_uuid()": "@default(dbgenerated(\"gen_random_uuid()\"))" };
|
|
409
|
+
function formatDbGeneratedAttribute(expression) {
|
|
410
|
+
return `@default(dbgenerated(${JSON.stringify(expression)}))`;
|
|
411
|
+
}
|
|
412
|
+
function createPostgresDefaultMapping() {
|
|
413
|
+
return {
|
|
414
|
+
functionAttributes: POSTGRES_FUNCTION_ATTRIBUTES,
|
|
415
|
+
fallbackFunctionAttribute: formatDbGeneratedAttribute
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
//#endregion
|
|
419
|
+
//#region src/core/psl-infer/postgres-type-map.ts
|
|
420
|
+
const POSTGRES_TO_PSL = {
|
|
421
|
+
text: "String",
|
|
422
|
+
bool: "Boolean",
|
|
423
|
+
boolean: "Boolean",
|
|
424
|
+
int4: "Int",
|
|
425
|
+
integer: "Int",
|
|
426
|
+
int8: "BigInt",
|
|
427
|
+
bigint: "BigInt",
|
|
428
|
+
float8: "Float",
|
|
429
|
+
"double precision": "Float",
|
|
430
|
+
numeric: "Decimal",
|
|
431
|
+
decimal: "Decimal",
|
|
432
|
+
timestamptz: "DateTime",
|
|
433
|
+
"timestamp with time zone": "DateTime",
|
|
434
|
+
jsonb: "Json",
|
|
435
|
+
bytea: "Bytes"
|
|
436
|
+
};
|
|
437
|
+
const PRESERVED_NATIVE_TYPES = {
|
|
438
|
+
"character varying": {
|
|
439
|
+
pslType: "String",
|
|
440
|
+
attributeName: "db.VarChar"
|
|
441
|
+
},
|
|
442
|
+
character: {
|
|
443
|
+
pslType: "String",
|
|
444
|
+
attributeName: "db.Char"
|
|
445
|
+
},
|
|
446
|
+
char: {
|
|
447
|
+
pslType: "String",
|
|
448
|
+
attributeName: "db.Char"
|
|
449
|
+
},
|
|
450
|
+
varchar: {
|
|
451
|
+
pslType: "String",
|
|
452
|
+
attributeName: "db.VarChar"
|
|
453
|
+
},
|
|
454
|
+
uuid: {
|
|
455
|
+
pslType: "String",
|
|
456
|
+
attributeName: "db.Uuid"
|
|
457
|
+
},
|
|
458
|
+
int2: {
|
|
459
|
+
pslType: "Int",
|
|
460
|
+
attributeName: "db.SmallInt"
|
|
461
|
+
},
|
|
462
|
+
smallint: {
|
|
463
|
+
pslType: "Int",
|
|
464
|
+
attributeName: "db.SmallInt"
|
|
465
|
+
},
|
|
466
|
+
float4: {
|
|
467
|
+
pslType: "Float",
|
|
468
|
+
attributeName: "db.Real"
|
|
469
|
+
},
|
|
470
|
+
real: {
|
|
471
|
+
pslType: "Float",
|
|
472
|
+
attributeName: "db.Real"
|
|
473
|
+
},
|
|
474
|
+
timestamp: {
|
|
475
|
+
pslType: "DateTime",
|
|
476
|
+
attributeName: "db.Timestamp"
|
|
477
|
+
},
|
|
478
|
+
"timestamp without time zone": {
|
|
479
|
+
pslType: "DateTime",
|
|
480
|
+
attributeName: "db.Timestamp"
|
|
481
|
+
},
|
|
482
|
+
date: {
|
|
483
|
+
pslType: "DateTime",
|
|
484
|
+
attributeName: "db.Date"
|
|
485
|
+
},
|
|
486
|
+
time: {
|
|
487
|
+
pslType: "DateTime",
|
|
488
|
+
attributeName: "db.Time"
|
|
489
|
+
},
|
|
490
|
+
"time without time zone": {
|
|
491
|
+
pslType: "DateTime",
|
|
492
|
+
attributeName: "db.Time"
|
|
493
|
+
},
|
|
494
|
+
timetz: {
|
|
495
|
+
pslType: "DateTime",
|
|
496
|
+
attributeName: "db.Timetz"
|
|
497
|
+
},
|
|
498
|
+
"time with time zone": {
|
|
499
|
+
pslType: "DateTime",
|
|
500
|
+
attributeName: "db.Timetz"
|
|
501
|
+
},
|
|
502
|
+
json: {
|
|
503
|
+
pslType: "Json",
|
|
504
|
+
attributeName: "db.Json"
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
const PARAMETERIZED_NATIVE_TYPES = {
|
|
508
|
+
"character varying": {
|
|
509
|
+
pslType: "String",
|
|
510
|
+
attributeName: "db.VarChar"
|
|
511
|
+
},
|
|
512
|
+
character: {
|
|
513
|
+
pslType: "String",
|
|
514
|
+
attributeName: "db.Char"
|
|
515
|
+
},
|
|
516
|
+
char: {
|
|
517
|
+
pslType: "String",
|
|
518
|
+
attributeName: "db.Char"
|
|
519
|
+
},
|
|
520
|
+
varchar: {
|
|
521
|
+
pslType: "String",
|
|
522
|
+
attributeName: "db.VarChar"
|
|
523
|
+
},
|
|
524
|
+
numeric: {
|
|
525
|
+
pslType: "Decimal",
|
|
526
|
+
attributeName: "db.Numeric"
|
|
527
|
+
},
|
|
528
|
+
timestamp: {
|
|
529
|
+
pslType: "DateTime",
|
|
530
|
+
attributeName: "db.Timestamp"
|
|
531
|
+
},
|
|
532
|
+
timestamptz: {
|
|
533
|
+
pslType: "DateTime",
|
|
534
|
+
attributeName: "db.Timestamptz"
|
|
535
|
+
},
|
|
536
|
+
time: {
|
|
537
|
+
pslType: "DateTime",
|
|
538
|
+
attributeName: "db.Time"
|
|
539
|
+
},
|
|
540
|
+
timetz: {
|
|
541
|
+
pslType: "DateTime",
|
|
542
|
+
attributeName: "db.Timetz"
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
const PARAMETERIZED_TYPE_PATTERN = /^(.+?)\((.+)\)$/;
|
|
546
|
+
function getOwnMappingValue(map, key) {
|
|
547
|
+
return Object.hasOwn(map, key) ? map[key] : void 0;
|
|
548
|
+
}
|
|
549
|
+
function getOwnRecordValue(map, key) {
|
|
550
|
+
return Object.hasOwn(map, key) ? map[key] : void 0;
|
|
551
|
+
}
|
|
552
|
+
function createNativeTypeAttribute(name, args) {
|
|
553
|
+
return args && args.length > 0 ? {
|
|
554
|
+
name,
|
|
555
|
+
args
|
|
556
|
+
} : { name };
|
|
557
|
+
}
|
|
558
|
+
function splitTypeParameterList(params) {
|
|
559
|
+
return params.split(",").map((part) => part.trim()).filter((part) => part.length > 0);
|
|
560
|
+
}
|
|
561
|
+
function createPostgresTypeMap(enumTypeNames) {
|
|
562
|
+
return { resolve(nativeType) {
|
|
563
|
+
if (enumTypeNames?.has(nativeType)) return {
|
|
564
|
+
pslType: nativeType,
|
|
565
|
+
nativeType
|
|
566
|
+
};
|
|
567
|
+
const paramMatch = nativeType.match(PARAMETERIZED_TYPE_PATTERN);
|
|
568
|
+
if (paramMatch) {
|
|
569
|
+
const [, baseType = nativeType, params = ""] = paramMatch;
|
|
570
|
+
const template = getOwnRecordValue(PARAMETERIZED_NATIVE_TYPES, baseType);
|
|
571
|
+
if (template) return {
|
|
572
|
+
pslType: template.pslType,
|
|
573
|
+
nativeType,
|
|
574
|
+
typeParams: {
|
|
575
|
+
baseType,
|
|
576
|
+
params
|
|
577
|
+
},
|
|
578
|
+
nativeTypeAttribute: createNativeTypeAttribute(template.attributeName, splitTypeParameterList(params))
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
const preservedType = getOwnRecordValue(PRESERVED_NATIVE_TYPES, nativeType);
|
|
582
|
+
if (preservedType) return {
|
|
583
|
+
pslType: preservedType.pslType,
|
|
584
|
+
nativeType,
|
|
585
|
+
nativeTypeAttribute: createNativeTypeAttribute(preservedType.attributeName)
|
|
586
|
+
};
|
|
587
|
+
const pslType = getOwnMappingValue(POSTGRES_TO_PSL, nativeType);
|
|
588
|
+
if (pslType) return {
|
|
589
|
+
pslType,
|
|
590
|
+
nativeType
|
|
591
|
+
};
|
|
592
|
+
return {
|
|
593
|
+
unsupported: true,
|
|
594
|
+
nativeType
|
|
595
|
+
};
|
|
596
|
+
} };
|
|
597
|
+
}
|
|
598
|
+
//#endregion
|
|
599
|
+
//#region src/core/psl-infer/infer-psl-contract.ts
|
|
600
|
+
const SYNTHETIC_SPAN = {
|
|
601
|
+
start: {
|
|
602
|
+
offset: 0,
|
|
603
|
+
line: 1,
|
|
604
|
+
column: 1
|
|
605
|
+
},
|
|
606
|
+
end: {
|
|
607
|
+
offset: 0,
|
|
608
|
+
line: 1,
|
|
609
|
+
column: 1
|
|
610
|
+
}
|
|
611
|
+
};
|
|
612
|
+
const PSL_SCALAR_TYPE_NAMES = new Set([
|
|
613
|
+
"String",
|
|
614
|
+
"Boolean",
|
|
615
|
+
"Int",
|
|
616
|
+
"BigInt",
|
|
617
|
+
"Float",
|
|
618
|
+
"Decimal",
|
|
619
|
+
"DateTime",
|
|
620
|
+
"Json",
|
|
621
|
+
"Bytes"
|
|
622
|
+
]);
|
|
623
|
+
/**
|
|
624
|
+
* Infers a PSL AST (for `printPsl`) from an introspected Postgres schema tree.
|
|
625
|
+
*
|
|
626
|
+
* Target-owned inference: it walks the `PostgresDatabaseSchemaNode` tree and
|
|
627
|
+
* owns the Postgres dialect knowledge — the native type map and default map.
|
|
628
|
+
* Relation inference, name transforms, generic default mapping, and raw-default
|
|
629
|
+
* parsing are shape-neutral utilities imported from the SQL family.
|
|
630
|
+
*
|
|
631
|
+
* This slice emits relational-only PSL, byte-identical to the prior flat
|
|
632
|
+
* inference: the tree's tables (across its namespaces — `contract infer`
|
|
633
|
+
* introspects a single live namespace) are gathered into the model set and
|
|
634
|
+
* emitted as one `UNSPECIFIED_PSL_NAMESPACE_ID` bucket. Top-level entities
|
|
635
|
+
* (policies/roles → PSL extension blocks) are a later slice.
|
|
636
|
+
*/
|
|
637
|
+
function inferPostgresPslContract(tree) {
|
|
638
|
+
const namespaces = Object.values(tree.namespaces);
|
|
639
|
+
const enumTypeNames = [...new Set(namespaces.flatMap((ns) => ns.nativeEnumTypeNames))];
|
|
640
|
+
if (enumTypeNames.length > 0) {
|
|
641
|
+
const names = enumTypeNames.join(", ");
|
|
642
|
+
throw new Error(`contract infer: the database contains native Postgres enum type(s): ${names}. Native Postgres enums (CREATE TYPE … AS ENUM) are not adoptable by contract infer. Drop the native type and replace each column with a text column carrying a CHECK constraint, then re-run contract infer. The domain enum (enum Name { @@type("pg/text@1") … }) authoring surface generates the required check automatically.`);
|
|
643
|
+
}
|
|
644
|
+
const tables = {};
|
|
645
|
+
for (const namespace of namespaces) for (const [tableName, table] of Object.entries(namespace.tables)) {
|
|
646
|
+
if (tables[tableName] !== void 0) throw new Error(`contract infer: duplicate table name "${tableName}" across schemas is not yet supported (single-namespace PSL inference emits one flat bucket; multi-namespace \`namespace { … }\` output is a later slice).`);
|
|
647
|
+
tables[tableName] = table;
|
|
648
|
+
}
|
|
649
|
+
return buildPslDocumentAst({ tables }, {
|
|
650
|
+
typeMap: createPostgresTypeMap(/* @__PURE__ */ new Set()),
|
|
651
|
+
defaultMapping: createPostgresDefaultMapping(),
|
|
652
|
+
parseRawDefault
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
function buildPslDocumentAst(schemaIR, options) {
|
|
656
|
+
const { typeMap, defaultMapping, parseRawDefault: rawDefaultParser } = options;
|
|
657
|
+
const modelNames = buildTopLevelNameMap(Object.keys(schemaIR.tables), toModelName, "model", "table");
|
|
658
|
+
const modelNameMap = new Map([...modelNames].map(([tableName, result]) => [tableName, result.name]));
|
|
659
|
+
const reservedNamedTypeNames = createReservedNamedTypeNames(modelNames);
|
|
660
|
+
const fieldNamesByTable = buildFieldNamesByTable(schemaIR.tables);
|
|
661
|
+
const { relationsByTable } = inferRelations(schemaIR.tables, modelNameMap);
|
|
662
|
+
const namedTypes = seedNamedTypeRegistry(schemaIR, typeMap, /* @__PURE__ */ new Map(), reservedNamedTypeNames);
|
|
663
|
+
const models = [];
|
|
664
|
+
for (const table of Object.values(schemaIR.tables)) models.push(buildModel(table, typeMap, /* @__PURE__ */ new Map(), fieldNamesByTable, namedTypes, defaultMapping, rawDefaultParser, relationsByTable.get(table.name) ?? []));
|
|
665
|
+
const sortedModels = topologicalSort(models, schemaIR.tables, modelNameMap);
|
|
666
|
+
const namedTypeEntries = [...namedTypes.entriesByKey.values()].sort((a, b) => a.name.localeCompare(b.name));
|
|
667
|
+
const types = namedTypeEntries.length > 0 ? {
|
|
668
|
+
kind: "types",
|
|
669
|
+
declarations: namedTypeEntries.map(buildNamedTypeDeclaration),
|
|
670
|
+
span: SYNTHETIC_SPAN
|
|
671
|
+
} : void 0;
|
|
672
|
+
return {
|
|
673
|
+
kind: "document",
|
|
674
|
+
sourceId: "<sql-schema-ir>",
|
|
675
|
+
namespaces: [makePslNamespace({
|
|
676
|
+
kind: "namespace",
|
|
677
|
+
name: UNSPECIFIED_PSL_NAMESPACE_ID,
|
|
678
|
+
entries: makePslNamespaceEntries(sortedModels, [], []),
|
|
679
|
+
span: SYNTHETIC_SPAN
|
|
680
|
+
})],
|
|
681
|
+
...types ? { types } : {},
|
|
682
|
+
span: SYNTHETIC_SPAN
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
function buildModel(table, typeMap, enumNameMap, fieldNamesByTable, namedTypes, defaultMapping, rawDefaultParser, relationFields) {
|
|
686
|
+
const { name: modelName, map: mapName } = toModelName(table.name);
|
|
687
|
+
const fieldNameMap = fieldNamesByTable.get(table.name);
|
|
688
|
+
const pkColumns = new Set(table.primaryKey?.columns ?? []);
|
|
689
|
+
const isSinglePk = pkColumns.size === 1;
|
|
690
|
+
const singlePkConstraintName = isSinglePk ? table.primaryKey?.name : void 0;
|
|
691
|
+
const uniqueColumns = /* @__PURE__ */ new Map();
|
|
692
|
+
for (const unique of table.uniques) if (unique.columns.length === 1) {
|
|
693
|
+
const [columnName = ""] = unique.columns;
|
|
694
|
+
const existingConstraintName = uniqueColumns.get(columnName);
|
|
695
|
+
if (!uniqueColumns.has(columnName) || existingConstraintName === void 0 && unique.name) uniqueColumns.set(columnName, unique.name);
|
|
696
|
+
}
|
|
697
|
+
const fields = [];
|
|
698
|
+
for (const column of Object.values(table.columns)) fields.push(buildScalarField(column, table, typeMap, enumNameMap, fieldNameMap, namedTypes, defaultMapping, rawDefaultParser, pkColumns, isSinglePk, singlePkConstraintName, uniqueColumns));
|
|
699
|
+
const usedFieldNames = new Set(fields.map((field) => field.name));
|
|
700
|
+
for (const rel of relationFields) fields.push(buildRelationField(rel, table.name, fieldNamesByTable, usedFieldNames));
|
|
701
|
+
const modelAttributes = [];
|
|
702
|
+
if (table.primaryKey && table.primaryKey.columns.length > 1) {
|
|
703
|
+
const pkFieldNames = table.primaryKey.columns.map((columnName) => resolveColumnFieldName(fieldNamesByTable, table.name, columnName));
|
|
704
|
+
modelAttributes.push(buildModelConstraintAttribute("id", pkFieldNames, table.primaryKey.name));
|
|
705
|
+
}
|
|
706
|
+
for (const unique of table.uniques) if (unique.columns.length > 1) {
|
|
707
|
+
const uniqueFieldNames = unique.columns.map((columnName) => resolveColumnFieldName(fieldNamesByTable, table.name, columnName));
|
|
708
|
+
modelAttributes.push(buildModelConstraintAttribute("unique", uniqueFieldNames, unique.name));
|
|
709
|
+
}
|
|
710
|
+
for (const index of table.indexes) if (!index.unique) {
|
|
711
|
+
const indexFieldNames = index.columns.map((columnName) => resolveColumnFieldName(fieldNamesByTable, table.name, columnName));
|
|
712
|
+
modelAttributes.push(buildModelConstraintAttribute("index", indexFieldNames, index.name));
|
|
713
|
+
}
|
|
714
|
+
if (mapName) modelAttributes.push(buildMapAttribute("model", mapName));
|
|
715
|
+
const comment = table.primaryKey ? void 0 : "// WARNING: This table has no primary key in the database";
|
|
716
|
+
return {
|
|
717
|
+
kind: "model",
|
|
718
|
+
name: modelName,
|
|
719
|
+
fields,
|
|
720
|
+
attributes: modelAttributes,
|
|
721
|
+
span: SYNTHETIC_SPAN,
|
|
722
|
+
...comment !== void 0 ? { comment } : {}
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
function buildScalarField(column, table, typeMap, enumNameMap, fieldNameMap, namedTypes, defaultMapping, rawDefaultParser, pkColumns, isSinglePk, singlePkConstraintName, uniqueColumns) {
|
|
726
|
+
const resolvedField = fieldNameMap?.get(column.name);
|
|
727
|
+
const fieldName = resolvedField?.fieldName ?? toFieldName(column.name).name;
|
|
728
|
+
const fieldMap = resolvedField?.fieldMap;
|
|
729
|
+
const resolution = typeMap.resolve(column.nativeType, table.annotations);
|
|
730
|
+
if ("unsupported" in resolution) {
|
|
731
|
+
const attrs = [];
|
|
732
|
+
if (fieldMap !== void 0) attrs.push(buildMapAttribute("field", fieldMap));
|
|
733
|
+
return {
|
|
734
|
+
kind: "field",
|
|
735
|
+
name: fieldName,
|
|
736
|
+
typeName: `Unsupported("${escapePslString(resolution.nativeType)}")`,
|
|
737
|
+
optional: column.nullable,
|
|
738
|
+
list: column.many === true,
|
|
739
|
+
attributes: attrs,
|
|
740
|
+
span: SYNTHETIC_SPAN
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
let typeName = resolution.pslType;
|
|
744
|
+
const enumPslName = enumNameMap.get(column.nativeType);
|
|
745
|
+
if (enumPslName) typeName = enumPslName;
|
|
746
|
+
if (resolution.nativeTypeAttribute && !enumPslName) typeName = resolveNamedTypeName(namedTypes, resolution);
|
|
747
|
+
const attributes = [];
|
|
748
|
+
const isId = isSinglePk && pkColumns.has(column.name);
|
|
749
|
+
if (isId) attributes.push(buildSimpleConstraintFieldAttribute("id", singlePkConstraintName));
|
|
750
|
+
if (column.default !== void 0) {
|
|
751
|
+
const parsed = parseColumnDefault(column.default, column.nativeType, rawDefaultParser);
|
|
752
|
+
if (parsed) {
|
|
753
|
+
const result = mapDefault(parsed, defaultMapping);
|
|
754
|
+
if ("attribute" in result) attributes.push(parseDefaultAttributeString(result.attribute));
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
if (uniqueColumns.has(column.name) && !isId) {
|
|
758
|
+
const uniqueConstraintName = uniqueColumns.get(column.name);
|
|
759
|
+
attributes.push(buildSimpleConstraintFieldAttribute("unique", uniqueConstraintName));
|
|
760
|
+
}
|
|
761
|
+
if (fieldMap !== void 0) attributes.push(buildMapAttribute("field", fieldMap));
|
|
762
|
+
return {
|
|
763
|
+
kind: "field",
|
|
764
|
+
name: fieldName,
|
|
765
|
+
typeName,
|
|
766
|
+
optional: column.nullable,
|
|
767
|
+
list: column.many === true,
|
|
768
|
+
attributes,
|
|
769
|
+
span: SYNTHETIC_SPAN
|
|
770
|
+
};
|
|
771
|
+
}
|
|
772
|
+
function buildRelationField(rel, hostTableName, fieldNamesByTable, usedFieldNames) {
|
|
773
|
+
const fieldName = createUniqueFieldName(rel.fieldName, usedFieldNames);
|
|
774
|
+
usedFieldNames.add(fieldName);
|
|
775
|
+
const args = [];
|
|
776
|
+
if (rel.fields && rel.references) {
|
|
777
|
+
if (rel.relationName) args.push(namedArg("name", `"${escapePslString(rel.relationName)}"`));
|
|
778
|
+
args.push(namedArg("fields", `[${rel.fields.map((columnName) => resolveColumnFieldName(fieldNamesByTable, hostTableName, columnName)).join(", ")}]`));
|
|
779
|
+
args.push(namedArg("references", `[${rel.references.map((columnName) => resolveColumnFieldName(fieldNamesByTable, rel.referencedTableName ?? "", columnName)).join(", ")}]`));
|
|
780
|
+
if (rel.onDelete) args.push(namedArg("onDelete", rel.onDelete));
|
|
781
|
+
if (rel.onUpdate) args.push(namedArg("onUpdate", rel.onUpdate));
|
|
782
|
+
if (rel.fkName) args.push(namedArg("map", `"${escapePslString(rel.fkName)}"`));
|
|
783
|
+
} else if (rel.relationName) args.push(namedArg("name", `"${escapePslString(rel.relationName)}"`));
|
|
784
|
+
const attrs = args.length > 0 ? [buildAttribute("field", "relation", args)] : [];
|
|
785
|
+
return {
|
|
786
|
+
kind: "field",
|
|
787
|
+
name: fieldName,
|
|
788
|
+
typeName: rel.typeName,
|
|
789
|
+
optional: rel.optional,
|
|
790
|
+
list: rel.list,
|
|
791
|
+
attributes: attrs,
|
|
792
|
+
span: SYNTHETIC_SPAN
|
|
793
|
+
};
|
|
794
|
+
}
|
|
795
|
+
function buildModelConstraintAttribute(name, fields, constraintName) {
|
|
796
|
+
const args = [positionalArg(`[${fields.join(", ")}]`)];
|
|
797
|
+
if (constraintName !== void 0) args.push(namedArg("map", `"${escapePslString(constraintName)}"`));
|
|
798
|
+
return buildAttribute("model", name, args);
|
|
799
|
+
}
|
|
800
|
+
function buildSimpleConstraintFieldAttribute(name, constraintName) {
|
|
801
|
+
if (constraintName === void 0) return buildAttribute("field", name, []);
|
|
802
|
+
return buildAttribute("field", name, [namedArg("map", `"${escapePslString(constraintName)}"`)]);
|
|
803
|
+
}
|
|
804
|
+
function parseDefaultAttributeString(attributeText) {
|
|
805
|
+
return buildAttribute("field", "default", [positionalArg(attributeText.replace(/^@default\(/, "").replace(/\)$/, ""))]);
|
|
806
|
+
}
|
|
807
|
+
function buildMapAttribute(target, mapName) {
|
|
808
|
+
return buildAttribute(target, "map", [positionalArg(`"${escapePslString(mapName)}"`)]);
|
|
809
|
+
}
|
|
810
|
+
function buildAttribute(target, name, args) {
|
|
811
|
+
return {
|
|
812
|
+
kind: "attribute",
|
|
813
|
+
target,
|
|
814
|
+
name,
|
|
815
|
+
args,
|
|
816
|
+
span: SYNTHETIC_SPAN
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
function positionalArg(value) {
|
|
820
|
+
return {
|
|
821
|
+
kind: "positional",
|
|
822
|
+
value,
|
|
823
|
+
span: SYNTHETIC_SPAN
|
|
824
|
+
};
|
|
825
|
+
}
|
|
826
|
+
function namedArg(name, value) {
|
|
827
|
+
return {
|
|
828
|
+
kind: "named",
|
|
829
|
+
name,
|
|
830
|
+
value,
|
|
831
|
+
span: SYNTHETIC_SPAN
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
function buildNamedTypeDeclaration(entry) {
|
|
835
|
+
const attribute = buildAttribute("namedType", entry.nativeTypeAttribute.name, (entry.nativeTypeAttribute.args ?? []).map(positionalArg));
|
|
836
|
+
return {
|
|
837
|
+
kind: "namedType",
|
|
838
|
+
name: entry.name,
|
|
839
|
+
baseType: entry.baseType,
|
|
840
|
+
attributes: [attribute],
|
|
841
|
+
span: SYNTHETIC_SPAN
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
function escapePslString(value) {
|
|
845
|
+
return value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/\n/g, "\\n").replace(/\r/g, "\\r");
|
|
846
|
+
}
|
|
847
|
+
/**
|
|
848
|
+
* Resolves a `SqlColumnIR.default` value into a normalized {@link ColumnDefault}.
|
|
849
|
+
*
|
|
850
|
+
* `SqlSchemaIR` types the column default as `string` (a raw database default
|
|
851
|
+
* expression). Some legacy fixtures and tests still pass already-normalized
|
|
852
|
+
* `ColumnDefault` objects in the same slot, so we accept either shape
|
|
853
|
+
* defensively at runtime.
|
|
854
|
+
*/
|
|
855
|
+
function parseColumnDefault(value, nativeType, rawDefaultParser) {
|
|
856
|
+
if (typeof value === "string") return rawDefaultParser ? rawDefaultParser(value, nativeType) : void 0;
|
|
857
|
+
if (value !== null && typeof value === "object" && "kind" in value) return value;
|
|
858
|
+
}
|
|
859
|
+
function buildFieldNamesByTable(tables) {
|
|
860
|
+
const fieldNamesByTable = /* @__PURE__ */ new Map();
|
|
861
|
+
for (const table of Object.values(tables)) {
|
|
862
|
+
const assignmentOrder = [...Object.values(table.columns).map((column, index) => {
|
|
863
|
+
const { name, map } = toFieldName(column.name);
|
|
864
|
+
return {
|
|
865
|
+
columnName: column.name,
|
|
866
|
+
desiredFieldName: name,
|
|
867
|
+
fieldMap: map,
|
|
868
|
+
index
|
|
869
|
+
};
|
|
870
|
+
})].sort((left, right) => {
|
|
871
|
+
const mapComparison = Number(left.fieldMap !== void 0) - Number(right.fieldMap !== void 0);
|
|
872
|
+
if (mapComparison !== 0) return mapComparison;
|
|
873
|
+
return left.index - right.index;
|
|
874
|
+
});
|
|
875
|
+
const usedFieldNames = /* @__PURE__ */ new Set();
|
|
876
|
+
const tableFieldNames = /* @__PURE__ */ new Map();
|
|
877
|
+
for (const column of assignmentOrder) {
|
|
878
|
+
const fieldName = createUniqueFieldName(column.desiredFieldName, usedFieldNames);
|
|
879
|
+
usedFieldNames.add(fieldName);
|
|
880
|
+
tableFieldNames.set(column.columnName, {
|
|
881
|
+
fieldName,
|
|
882
|
+
fieldMap: column.fieldMap
|
|
883
|
+
});
|
|
884
|
+
}
|
|
885
|
+
fieldNamesByTable.set(table.name, tableFieldNames);
|
|
886
|
+
}
|
|
887
|
+
return fieldNamesByTable;
|
|
888
|
+
}
|
|
889
|
+
function resolveColumnFieldName(fieldNamesByTable, tableName, columnName) {
|
|
890
|
+
return fieldNamesByTable.get(tableName)?.get(columnName)?.fieldName ?? toFieldName(columnName).name;
|
|
891
|
+
}
|
|
892
|
+
function createUniqueFieldName(desiredName, usedFieldNames) {
|
|
893
|
+
if (!usedFieldNames.has(desiredName)) return desiredName;
|
|
894
|
+
let counter = 2;
|
|
895
|
+
while (usedFieldNames.has(`${desiredName}${counter}`)) counter++;
|
|
896
|
+
return `${desiredName}${counter}`;
|
|
897
|
+
}
|
|
898
|
+
function buildTopLevelNameMap(sources, normalize, kind, sourceKind) {
|
|
899
|
+
const results = /* @__PURE__ */ new Map();
|
|
900
|
+
const normalizedToSources = /* @__PURE__ */ new Map();
|
|
901
|
+
for (const source of sources) {
|
|
902
|
+
const normalized = normalize(source);
|
|
903
|
+
results.set(source, normalized);
|
|
904
|
+
normalizedToSources.set(normalized.name, [...normalizedToSources.get(normalized.name) ?? [], source]);
|
|
905
|
+
}
|
|
906
|
+
const duplicates = [...normalizedToSources.entries()].filter(([, conflictingSources]) => conflictingSources.length > 1);
|
|
907
|
+
if (duplicates.length > 0) {
|
|
908
|
+
const details = duplicates.map(([normalizedName, conflictingSources]) => `- ${kind} "${normalizedName}" from ${sourceKind}s ${conflictingSources.map((source) => `"${source}"`).join(", ")}`);
|
|
909
|
+
throw new Error(`PSL ${kind} name collisions detected:\n${details.join("\n")}`);
|
|
910
|
+
}
|
|
911
|
+
return results;
|
|
912
|
+
}
|
|
913
|
+
function createReservedNamedTypeNames(modelNames) {
|
|
914
|
+
const reservedNames = new Set(PSL_SCALAR_TYPE_NAMES);
|
|
915
|
+
for (const result of modelNames.values()) reservedNames.add(result.name);
|
|
916
|
+
return reservedNames;
|
|
917
|
+
}
|
|
918
|
+
function seedNamedTypeRegistry(schemaIR, typeMap, enumNameMap, reservedNames) {
|
|
919
|
+
const seeds = /* @__PURE__ */ new Map();
|
|
920
|
+
for (const tableName of Object.keys(schemaIR.tables).sort()) {
|
|
921
|
+
const table = schemaIR.tables[tableName];
|
|
922
|
+
if (!table) continue;
|
|
923
|
+
for (const columnName of Object.keys(table.columns).sort()) {
|
|
924
|
+
const column = table.columns[columnName];
|
|
925
|
+
if (!column) continue;
|
|
926
|
+
const resolution = typeMap.resolve(column.nativeType, table.annotations);
|
|
927
|
+
if ("unsupported" in resolution || enumNameMap.has(column.nativeType) || !resolution.nativeTypeAttribute) continue;
|
|
928
|
+
const signatureKey = createNamedTypeSignatureKey(resolution);
|
|
929
|
+
if (!seeds.has(signatureKey)) seeds.set(signatureKey, {
|
|
930
|
+
baseType: resolution.pslType,
|
|
931
|
+
desiredName: toNamedTypeName(column.name),
|
|
932
|
+
nativeTypeAttribute: resolution.nativeTypeAttribute
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
const registry = {
|
|
937
|
+
entriesByKey: /* @__PURE__ */ new Map(),
|
|
938
|
+
usedNames: new Set(reservedNames)
|
|
939
|
+
};
|
|
940
|
+
const sortedSeeds = [...seeds.entries()].sort((left, right) => {
|
|
941
|
+
const desiredNameComparison = left[1].desiredName.localeCompare(right[1].desiredName);
|
|
942
|
+
if (desiredNameComparison !== 0) return desiredNameComparison;
|
|
943
|
+
return left[0].localeCompare(right[0]);
|
|
944
|
+
});
|
|
945
|
+
for (const [signatureKey, seed] of sortedSeeds) {
|
|
946
|
+
const name = createUniqueFieldName(seed.desiredName, registry.usedNames);
|
|
947
|
+
registry.entriesByKey.set(signatureKey, {
|
|
948
|
+
name,
|
|
949
|
+
baseType: seed.baseType,
|
|
950
|
+
nativeTypeAttribute: seed.nativeTypeAttribute
|
|
951
|
+
});
|
|
952
|
+
registry.usedNames.add(name);
|
|
953
|
+
}
|
|
954
|
+
return registry;
|
|
955
|
+
}
|
|
956
|
+
function resolveNamedTypeName(registry, resolution) {
|
|
957
|
+
const key = createNamedTypeSignatureKey(resolution);
|
|
958
|
+
const existing = registry.entriesByKey.get(key);
|
|
959
|
+
if (existing) return existing.name;
|
|
960
|
+
throw new Error(`Named type registry was not seeded for native type "${resolution.nativeType}"`);
|
|
961
|
+
}
|
|
962
|
+
function createNamedTypeSignatureKey(resolution) {
|
|
963
|
+
return JSON.stringify({
|
|
964
|
+
baseType: resolution.pslType,
|
|
965
|
+
nativeTypeAttribute: resolution.nativeTypeAttribute ? {
|
|
966
|
+
name: resolution.nativeTypeAttribute.name,
|
|
967
|
+
args: resolution.nativeTypeAttribute.args ?? null
|
|
968
|
+
} : null
|
|
969
|
+
});
|
|
970
|
+
}
|
|
971
|
+
function topologicalSort(models, tables, modelNameMap) {
|
|
972
|
+
const modelByName = /* @__PURE__ */ new Map();
|
|
973
|
+
for (const model of models) modelByName.set(model.name, model);
|
|
974
|
+
const deps = /* @__PURE__ */ new Map();
|
|
975
|
+
const tableToModel = /* @__PURE__ */ new Map();
|
|
976
|
+
for (const tableName of Object.keys(tables)) {
|
|
977
|
+
const modelName = modelNameMap.get(tableName);
|
|
978
|
+
tableToModel.set(tableName, modelName);
|
|
979
|
+
deps.set(modelName, /* @__PURE__ */ new Set());
|
|
980
|
+
}
|
|
981
|
+
for (const [tableName, table] of Object.entries(tables)) {
|
|
982
|
+
const modelName = tableToModel.get(tableName);
|
|
983
|
+
for (const fk of table.foreignKeys) {
|
|
984
|
+
const refModelName = tableToModel.get(fk.referencedTable);
|
|
985
|
+
if (refModelName && refModelName !== modelName) deps.get(modelName).add(refModelName);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
const result = [];
|
|
989
|
+
const visited = /* @__PURE__ */ new Set();
|
|
990
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
991
|
+
const sortedNames = [...deps.keys()].sort();
|
|
992
|
+
function visit(name) {
|
|
993
|
+
if (visited.has(name)) return;
|
|
994
|
+
if (visiting.has(name)) return;
|
|
995
|
+
visiting.add(name);
|
|
996
|
+
const sortedDeps = [...deps.get(name)].sort();
|
|
997
|
+
for (const dep of sortedDeps) visit(dep);
|
|
998
|
+
visiting.delete(name);
|
|
999
|
+
visited.add(name);
|
|
1000
|
+
result.push(modelByName.get(name));
|
|
1001
|
+
}
|
|
1002
|
+
for (const name of sortedNames) visit(name);
|
|
1003
|
+
return result;
|
|
1004
|
+
}
|
|
1005
|
+
//#endregion
|
|
411
1006
|
//#region src/exports/control.ts
|
|
412
1007
|
function buildNativeTypeExpander(frameworkComponents) {
|
|
413
1008
|
if (!frameworkComponents) return;
|
|
@@ -428,6 +1023,28 @@ const postgresTargetDescriptor = {
|
|
|
428
1023
|
...postgresTargetDescriptorMeta,
|
|
429
1024
|
contractSerializer: new PostgresContractSerializer(),
|
|
430
1025
|
schemaVerifier: new PostgresSchemaVerifier(),
|
|
1026
|
+
inferPslContract(schema) {
|
|
1027
|
+
PostgresDatabaseSchemaNode.assert(schema);
|
|
1028
|
+
return inferPostgresPslContract(schema);
|
|
1029
|
+
},
|
|
1030
|
+
diffDatabaseSchema(input) {
|
|
1031
|
+
return diffPostgresDatabaseSchema({
|
|
1032
|
+
contract: input.contract,
|
|
1033
|
+
actualSchema: input.schema,
|
|
1034
|
+
strict: input.strict,
|
|
1035
|
+
typeMetadataRegistry: input.typeMetadataRegistry,
|
|
1036
|
+
frameworkComponents: input.frameworkComponents
|
|
1037
|
+
});
|
|
1038
|
+
},
|
|
1039
|
+
verifyDatabaseSchema(input) {
|
|
1040
|
+
return verifyPostgresDatabaseSchema({
|
|
1041
|
+
contract: input.contract,
|
|
1042
|
+
actualSchema: input.schema,
|
|
1043
|
+
strict: input.strict,
|
|
1044
|
+
typeMetadataRegistry: input.typeMetadataRegistry,
|
|
1045
|
+
frameworkComponents: input.frameworkComponents
|
|
1046
|
+
});
|
|
1047
|
+
},
|
|
431
1048
|
migrations: {
|
|
432
1049
|
createPlanner(adapter) {
|
|
433
1050
|
return createPostgresMigrationPlanner(adapter);
|
|
@@ -436,9 +1053,10 @@ const postgresTargetDescriptor = {
|
|
|
436
1053
|
return createPostgresMigrationRunner(family);
|
|
437
1054
|
},
|
|
438
1055
|
contractToSchema(contract, frameworkComponents) {
|
|
439
|
-
|
|
1056
|
+
const expander = buildNativeTypeExpander(frameworkComponents);
|
|
1057
|
+
return contractToPostgresDatabaseSchemaNode(blindCast(contract), {
|
|
440
1058
|
annotationNamespace: "pg",
|
|
441
|
-
...ifDefined("expandNativeType",
|
|
1059
|
+
...ifDefined("expandNativeType", expander),
|
|
442
1060
|
renderDefault: postgresRenderDefault
|
|
443
1061
|
});
|
|
444
1062
|
}
|