@prisma-next/target-postgres 0.12.0-dev.3 → 0.12.0-dev.31
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/{codec-ids-DliyCWPY.d.mts → codec-ids-BEVQ-gO4.d.mts} +1 -1
- package/dist/{codec-ids-DliyCWPY.d.mts.map → codec-ids-BEVQ-gO4.d.mts.map} +1 -1
- package/dist/codec-ids.d.mts +1 -1
- package/dist/{codec-types-BF1DXTPs.d.mts → codec-types-ClVgENiR.d.mts} +2 -2
- package/dist/{codec-types-BF1DXTPs.d.mts.map → codec-types-ClVgENiR.d.mts.map} +1 -1
- package/dist/codec-types.d.mts +1 -1
- package/dist/{codecs-DuP3d9Au.d.mts → codecs-BHrYhAZc.d.mts} +2 -2
- package/dist/{codecs-DuP3d9Au.d.mts.map → codecs-BHrYhAZc.d.mts.map} +1 -1
- package/dist/codecs.d.mts +1 -1
- package/dist/column-type-compatibility-Dzxz_CkP.mjs +11 -0
- package/dist/column-type-compatibility-Dzxz_CkP.mjs.map +1 -0
- package/dist/column-type-compatibility.d.mts +5 -0
- package/dist/column-type-compatibility.d.mts.map +1 -0
- package/dist/column-type-compatibility.mjs +2 -0
- package/dist/contract-free.d.mts +35 -0
- package/dist/contract-free.d.mts.map +1 -0
- package/dist/contract-free.mjs +97 -0
- package/dist/contract-free.mjs.map +1 -0
- package/dist/control.d.mts +1 -1
- package/dist/control.mjs +45 -25
- package/dist/control.mjs.map +1 -1
- package/dist/{data-transform-CAPmAdxS.d.mts → data-transform-C1ZpoMCt.d.mts} +2 -2
- package/dist/{data-transform-CAPmAdxS.d.mts.map → data-transform-C1ZpoMCt.d.mts.map} +1 -1
- package/dist/{data-transform-CRkv2T_U.mjs → data-transform-DIYwbe4S.mjs} +1 -1
- package/dist/{data-transform-CRkv2T_U.mjs.map → data-transform-DIYwbe4S.mjs.map} +1 -1
- package/dist/data-transform.d.mts +1 -1
- package/dist/data-transform.mjs +1 -1
- package/dist/ddl.d.mts +2 -0
- package/dist/ddl.mjs +2 -0
- package/dist/{default-normalizer-DaAhmzBV.mjs → default-normalizer-DFHtpCPH.mjs} +1 -1
- package/dist/{default-normalizer-DaAhmzBV.mjs.map → default-normalizer-DFHtpCPH.mjs.map} +1 -1
- package/dist/default-normalizer.mjs +1 -1
- package/dist/{descriptor-meta-Uu8QaClr.mjs → descriptor-meta-BqATo5Dz.mjs} +3 -2
- package/dist/descriptor-meta-BqATo5Dz.mjs.map +1 -0
- package/dist/{descriptor-meta-runtime-DMRX39kp.mjs → descriptor-meta-runtime-BBL7VdnR.mjs} +2 -2
- package/dist/{descriptor-meta-runtime-DMRX39kp.mjs.map → descriptor-meta-runtime-BBL7VdnR.mjs.map} +1 -1
- package/dist/{enum-planning-DRA9LaMU.mjs → enum-planning-DHQE3zSH.mjs} +0 -0
- package/dist/{enum-planning-DRA9LaMU.mjs.map → enum-planning-DHQE3zSH.mjs.map} +1 -1
- package/dist/enum-planning.d.mts +1 -1
- package/dist/enum-planning.mjs +1 -1
- package/dist/{errors-BbnITmAD.mjs → errors-De185dnO.mjs} +1 -1
- package/dist/{errors-BbnITmAD.mjs.map → errors-De185dnO.mjs.map} +1 -1
- package/dist/errors.mjs +1 -1
- package/dist/{issue-planner-BtaL6OfW.mjs → issue-planner-ttX4mOiF.mjs} +10 -10
- package/dist/{issue-planner-BtaL6OfW.mjs.map → issue-planner-ttX4mOiF.mjs.map} +1 -1
- package/dist/issue-planner.d.mts +1 -1
- package/dist/issue-planner.mjs +1 -1
- package/dist/migration.d.mts +3 -3
- package/dist/migration.mjs +3 -3
- package/dist/{native-type-normalizer-BNEQ9VDs.mjs → native-type-normalizer-BUUe8USD.mjs} +1 -1
- package/dist/{native-type-normalizer-BNEQ9VDs.mjs.map → native-type-normalizer-BUUe8USD.mjs.map} +1 -1
- package/dist/native-type-normalizer.mjs +1 -1
- package/dist/nodes-BS8vS8gv.d.mts +38 -0
- package/dist/nodes-BS8vS8gv.d.mts.map +1 -0
- package/dist/nodes-BYSqasmC.mjs +42 -0
- package/dist/nodes-BYSqasmC.mjs.map +1 -0
- package/dist/{op-factory-call-CDlImOF_.d.mts → op-factory-call-B9mYdDex.d.mts} +3 -3
- package/dist/{op-factory-call-CDlImOF_.d.mts.map → op-factory-call-B9mYdDex.d.mts.map} +1 -1
- package/dist/{op-factory-call-ewOd5q6L.mjs → op-factory-call-D8XePTfV.mjs} +2 -2
- package/dist/{op-factory-call-ewOd5q6L.mjs.map → op-factory-call-D8XePTfV.mjs.map} +1 -1
- package/dist/op-factory-call.d.mts +1 -1
- package/dist/op-factory-call.mjs +1 -1
- package/dist/pack.d.mts +3 -2
- package/dist/pack.d.mts.map +1 -1
- package/dist/pack.mjs +1 -1
- package/dist/{planner-ddl-builders-BNMfSE9r.mjs → planner-ddl-builders-BcRMeGgx.mjs} +3 -3
- package/dist/{planner-ddl-builders-BNMfSE9r.mjs.map → planner-ddl-builders-BcRMeGgx.mjs.map} +1 -1
- package/dist/planner-ddl-builders.d.mts +1 -1
- package/dist/planner-ddl-builders.mjs +1 -1
- package/dist/{planner-identity-values-nhVj0hp-.mjs → planner-identity-values-DmwlJe_z.mjs} +1 -1
- package/dist/{planner-identity-values-nhVj0hp-.mjs.map → planner-identity-values-DmwlJe_z.mjs.map} +1 -1
- package/dist/planner-identity-values.mjs +1 -1
- package/dist/{planner-produced-postgres-migration-D_nsXbhl.mjs → planner-produced-postgres-migration-D6KXC2DH.mjs} +4 -4
- package/dist/{planner-produced-postgres-migration-D_nsXbhl.mjs.map → planner-produced-postgres-migration-D6KXC2DH.mjs.map} +1 -1
- package/dist/{planner-produced-postgres-migration-D02NOhVQ.d.mts → planner-produced-postgres-migration-RdjC7aUC.d.mts} +3 -3
- package/dist/{planner-produced-postgres-migration-D02NOhVQ.d.mts.map → planner-produced-postgres-migration-RdjC7aUC.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-schema-lookup-CGxxYfnD.mjs → planner-schema-lookup-Bm4S3LvB.mjs} +1 -1
- package/dist/{planner-schema-lookup-CGxxYfnD.mjs.map → planner-schema-lookup-Bm4S3LvB.mjs.map} +1 -1
- package/dist/planner-schema-lookup.mjs +1 -1
- package/dist/{planner-sql-checks-CfEiTXoQ.mjs → planner-sql-checks-lxWZQCFz.mjs} +3 -3
- package/dist/{planner-sql-checks-CfEiTXoQ.mjs.map → planner-sql-checks-lxWZQCFz.mjs.map} +1 -1
- package/dist/planner-sql-checks.mjs +1 -1
- package/dist/{planner-target-details-a_wuOiYf.d.mts → planner-target-details-C1Ye0hb6.d.mts} +1 -1
- package/dist/{planner-target-details-a_wuOiYf.d.mts.map → planner-target-details-C1Ye0hb6.d.mts.map} +1 -1
- package/dist/planner-target-details.d.mts +1 -1
- package/dist/planner-y-bVvDFR.mjs +342 -0
- package/dist/planner-y-bVvDFR.mjs.map +1 -0
- package/dist/planner.d.mts +3 -2
- package/dist/planner.d.mts.map +1 -1
- package/dist/planner.mjs +1 -1
- package/dist/{postgres-contract-serializer-BnOboPWs.mjs → postgres-contract-serializer-BYpK6TkY.mjs} +3 -3
- package/dist/{postgres-contract-serializer-BnOboPWs.mjs.map → postgres-contract-serializer-BYpK6TkY.mjs.map} +1 -1
- package/dist/{postgres-enum-type-CSzsvXrO.d.mts → postgres-enum-type-Bl6THBPg.d.mts} +1 -1
- package/dist/{postgres-enum-type-CSzsvXrO.d.mts.map → postgres-enum-type-Bl6THBPg.d.mts.map} +1 -1
- package/dist/{postgres-enum-type-BMgyxNyy.mjs → postgres-enum-type-CaGZ2mfI.mjs} +1 -1
- package/dist/{postgres-enum-type-BMgyxNyy.mjs.map → postgres-enum-type-CaGZ2mfI.mjs.map} +1 -1
- package/dist/{postgres-migration-BatbEvU6.mjs → postgres-migration-BnDg_Nj0.mjs} +3 -3
- package/dist/{postgres-migration-BatbEvU6.mjs.map → postgres-migration-BnDg_Nj0.mjs.map} +1 -1
- package/dist/{postgres-migration-DRY8V-bQ.d.mts → postgres-migration-DLZtVVDM.d.mts} +3 -3
- package/dist/{postgres-migration-DRY8V-bQ.d.mts.map → postgres-migration-DLZtVVDM.d.mts.map} +1 -1
- package/dist/{postgres-schema-BxAuNFX0.mjs → postgres-schema-Dky_Jff8.mjs} +3 -3
- package/dist/{postgres-schema-BxAuNFX0.mjs.map → postgres-schema-Dky_Jff8.mjs.map} +1 -1
- package/dist/{render-ops-XhICjX_P.mjs → render-ops-BpU-Qv06.mjs} +1 -1
- package/dist/{render-ops-XhICjX_P.mjs.map → render-ops-BpU-Qv06.mjs.map} +1 -1
- package/dist/render-ops.d.mts +1 -1
- package/dist/render-ops.mjs +1 -1
- package/dist/{render-typescript-K125n-RZ.mjs → render-typescript-D9O7aOS_.mjs} +1 -1
- package/dist/{render-typescript-K125n-RZ.mjs.map → render-typescript-D9O7aOS_.mjs.map} +1 -1
- package/dist/render-typescript.mjs +1 -1
- package/dist/runtime.mjs +2 -2
- package/dist/{shared-Do_a5ymU.d.mts → shared-wRIzOjdd.d.mts} +2 -2
- package/dist/{shared-Do_a5ymU.d.mts.map → shared-wRIzOjdd.d.mts.map} +1 -1
- package/dist/{sql-utils-CggjWNij.mjs → sql-utils-DR_F9aHR.mjs} +1 -1
- package/dist/{sql-utils-CggjWNij.mjs.map → sql-utils-DR_F9aHR.mjs.map} +1 -1
- package/dist/sql-utils.mjs +1 -1
- package/dist/{statement-builders-DVI5IVAa.mjs → statement-builders-Cd311EDF.mjs} +16 -4
- package/dist/statement-builders-Cd311EDF.mjs.map +1 -0
- package/dist/statement-builders.d.mts.map +1 -1
- package/dist/statement-builders.mjs +1 -1
- package/dist/{tables-DoA39Yqo.mjs → tables-Cuy4VIgu.mjs} +3 -3
- package/dist/{tables-DoA39Yqo.mjs.map → tables-Cuy4VIgu.mjs.map} +1 -1
- package/dist/{types-O40IcFV9.d.mts → types-DncetAjv.d.mts} +1 -1
- package/dist/types-DncetAjv.d.mts.map +1 -0
- package/dist/types.d.mts +2 -2
- package/dist/types.mjs +2 -2
- package/package.json +20 -17
- package/src/contract-free/control-bootstrap.ts +55 -0
- package/src/contract-free/ddl.ts +32 -0
- package/src/core/column-type-compatibility.ts +13 -0
- package/src/core/ddl/nodes.ts +59 -0
- package/src/core/descriptor-meta.ts +1 -0
- package/src/core/migrations/control-policy.ts +235 -0
- package/src/core/migrations/planner.ts +57 -6
- package/src/core/migrations/runner.ts +66 -27
- package/src/core/migrations/statement-builders.ts +18 -3
- package/src/exports/column-type-compatibility.ts +1 -0
- package/src/exports/contract-free.ts +5 -0
- package/src/exports/ddl.ts +7 -0
- package/dist/descriptor-meta-Uu8QaClr.mjs.map +0 -1
- package/dist/planner-Bjz5pnLa.mjs +0 -177
- package/dist/planner-Bjz5pnLa.mjs.map +0 -1
- package/dist/statement-builders-DVI5IVAa.mjs.map +0 -1
- package/dist/types-O40IcFV9.d.mts.map +0 -1
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import type { Contract } from '@prisma-next/contract/types';
|
|
2
|
+
import type { ControlPolicySubject } from '@prisma-next/family-sql/control';
|
|
3
|
+
import type { SchemaIssue } from '@prisma-next/framework-components/control';
|
|
4
|
+
import { UNBOUND_NAMESPACE_ID } from '@prisma-next/framework-components/ir';
|
|
5
|
+
import {
|
|
6
|
+
isPostgresEnumStorageEntry,
|
|
7
|
+
type SqlStorage,
|
|
8
|
+
storageTableAt,
|
|
9
|
+
} from '@prisma-next/sql-contract/types';
|
|
10
|
+
import { ifDefined } from '@prisma-next/utils/defined';
|
|
11
|
+
import { isPostgresSchema } from '../postgres-schema';
|
|
12
|
+
import type { PostgresOpFactoryCall } from './op-factory-call';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Factory calls that create a whole, previously-absent top-level storage
|
|
16
|
+
* object. Used to decide whether `tolerated` permits a call (it only allows
|
|
17
|
+
* creating absent objects, never modifying existing ones).
|
|
18
|
+
*
|
|
19
|
+
* Deliberately an explicit, closed set rather than a `factoryName`
|
|
20
|
+
* create/alter/drop classification: it answers exactly one yes/no question
|
|
21
|
+
* and is fail-closed. Any call not listed here — including future or
|
|
22
|
+
* extension-contributed factories — is treated as NOT object-creation, so it
|
|
23
|
+
* is suppressed under `tolerated` rather than permissively emitted.
|
|
24
|
+
*/
|
|
25
|
+
const OBJECT_CREATION_FACTORIES: ReadonlySet<string> = new Set<string>([
|
|
26
|
+
'createTable',
|
|
27
|
+
'createEnumType',
|
|
28
|
+
'createSchema',
|
|
29
|
+
]);
|
|
30
|
+
|
|
31
|
+
function createsNewTopLevelObject(call: PostgresOpFactoryCall): boolean {
|
|
32
|
+
return OBJECT_CREATION_FACTORIES.has(call.factoryName);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function ddlSchemaNameForNamespace(contract: Contract<SqlStorage>, namespaceId: string): string {
|
|
36
|
+
const namespace = contract.storage.namespaces[namespaceId];
|
|
37
|
+
return isPostgresSchema(namespace) ? namespace.ddlSchemaName(contract.storage) : namespaceId;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function resolveNamespaceIdForTable(
|
|
41
|
+
contract: Contract<SqlStorage>,
|
|
42
|
+
tableName: string,
|
|
43
|
+
ddlSchemaName: string | undefined,
|
|
44
|
+
): string {
|
|
45
|
+
for (const namespaceId of Object.keys(contract.storage.namespaces)) {
|
|
46
|
+
const table = storageTableAt(contract.storage, namespaceId, tableName);
|
|
47
|
+
if (!table) continue;
|
|
48
|
+
if (
|
|
49
|
+
ddlSchemaName === undefined ||
|
|
50
|
+
ddlSchemaNameForNamespace(contract, namespaceId) === ddlSchemaName
|
|
51
|
+
) {
|
|
52
|
+
return namespaceId;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return UNBOUND_NAMESPACE_ID;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function resolveNamespaceIdForDdlSchema(
|
|
59
|
+
contract: Contract<SqlStorage>,
|
|
60
|
+
ddlSchemaName: string,
|
|
61
|
+
): string {
|
|
62
|
+
for (const namespaceId of Object.keys(contract.storage.namespaces)) {
|
|
63
|
+
const ns = contract.storage.namespaces[namespaceId];
|
|
64
|
+
if (isPostgresSchema(ns) && ns.ddlSchemaName(contract.storage) === ddlSchemaName) {
|
|
65
|
+
return namespaceId;
|
|
66
|
+
}
|
|
67
|
+
if (namespaceId === ddlSchemaName) {
|
|
68
|
+
return namespaceId;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return UNBOUND_NAMESPACE_ID;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface PostgresCallFields {
|
|
75
|
+
readonly schemaName?: string;
|
|
76
|
+
readonly tableName?: string;
|
|
77
|
+
readonly columnName?: string;
|
|
78
|
+
readonly typeName?: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function postgresCallFields(call: PostgresOpFactoryCall): PostgresCallFields {
|
|
82
|
+
return {
|
|
83
|
+
...ifDefined('schemaName', 'schemaName' in call ? call.schemaName : undefined),
|
|
84
|
+
...ifDefined('tableName', 'tableName' in call ? call.tableName : undefined),
|
|
85
|
+
...ifDefined('columnName', 'columnName' in call ? call.columnName : undefined),
|
|
86
|
+
...ifDefined('typeName', 'typeName' in call ? call.typeName : undefined),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function formatPostgresControlPolicySubjectLabel(
|
|
91
|
+
factoryName: string,
|
|
92
|
+
subject: ControlPolicySubject | undefined,
|
|
93
|
+
contract: Contract<SqlStorage>,
|
|
94
|
+
): string {
|
|
95
|
+
if (subject?.table) {
|
|
96
|
+
const ddlSchema = ddlSchemaNameForNamespace(contract, subject.namespaceId);
|
|
97
|
+
return `${factoryName}(${ddlSchema}.${subject.table})`;
|
|
98
|
+
}
|
|
99
|
+
if (subject?.typeName) {
|
|
100
|
+
const ddlSchema = ddlSchemaNameForNamespace(contract, subject.namespaceId);
|
|
101
|
+
return `${factoryName}(${ddlSchema}.${subject.typeName})`;
|
|
102
|
+
}
|
|
103
|
+
return factoryName;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function resolvePostgresCallControlPolicySubject(
|
|
107
|
+
call: PostgresOpFactoryCall,
|
|
108
|
+
contract: Contract<SqlStorage>,
|
|
109
|
+
): ControlPolicySubject | undefined {
|
|
110
|
+
const callFields = postgresCallFields(call);
|
|
111
|
+
const createsNewObject = createsNewTopLevelObject(call);
|
|
112
|
+
|
|
113
|
+
if (call.factoryName === 'createSchema' && callFields.schemaName) {
|
|
114
|
+
return {
|
|
115
|
+
namespaceId: resolveNamespaceIdForDdlSchema(contract, callFields.schemaName),
|
|
116
|
+
createsNewObject,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (callFields.typeName && call.factoryName !== 'addColumn') {
|
|
121
|
+
const namespaceId = callFields.schemaName
|
|
122
|
+
? resolveNamespaceIdForDdlSchema(contract, callFields.schemaName)
|
|
123
|
+
: UNBOUND_NAMESPACE_ID;
|
|
124
|
+
const ns = contract.storage.namespaces[namespaceId];
|
|
125
|
+
const rawEnum =
|
|
126
|
+
ns && 'enum' in ns && ns.enum != null ? ns.enum[callFields.typeName] : undefined;
|
|
127
|
+
const controlPolicy = isPostgresEnumStorageEntry(rawEnum) ? rawEnum.control : undefined;
|
|
128
|
+
return {
|
|
129
|
+
namespaceId,
|
|
130
|
+
...ifDefined('explicitNodeControlPolicy', controlPolicy),
|
|
131
|
+
typeName: callFields.typeName,
|
|
132
|
+
createsNewObject,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (callFields.tableName) {
|
|
137
|
+
const namespaceId = resolveNamespaceIdForTable(
|
|
138
|
+
contract,
|
|
139
|
+
callFields.tableName,
|
|
140
|
+
callFields.schemaName,
|
|
141
|
+
);
|
|
142
|
+
const table = storageTableAt(contract.storage, namespaceId, callFields.tableName);
|
|
143
|
+
const tableControlPolicy = table?.control;
|
|
144
|
+
return {
|
|
145
|
+
namespaceId,
|
|
146
|
+
...ifDefined('explicitNodeControlPolicy', tableControlPolicy),
|
|
147
|
+
table: callFields.tableName,
|
|
148
|
+
...ifDefined('column', callFields.columnName),
|
|
149
|
+
createsNewObject,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (callFields.schemaName) {
|
|
154
|
+
return {
|
|
155
|
+
namespaceId: resolveNamespaceIdForDdlSchema(contract, callFields.schemaName),
|
|
156
|
+
createsNewObject,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Issue kinds that describe the absence of a whole, top-level Postgres
|
|
165
|
+
* object — the same kinds `createsNewTopLevelObject` recognises for calls.
|
|
166
|
+
* Used by {@link resolvePostgresIssueCreationFactoryName} to decide whether
|
|
167
|
+
* a `tolerated` subject permits the issue to flow into the planner
|
|
168
|
+
* (create-if-absent) and to seed the suppressed-subject warning's
|
|
169
|
+
* `factoryName` when the planner is skipped.
|
|
170
|
+
*/
|
|
171
|
+
const POSTGRES_ISSUE_CREATION_FACTORY: Readonly<Record<string, string>> = Object.freeze({
|
|
172
|
+
missing_schema: 'createSchema',
|
|
173
|
+
missing_table: 'createTable',
|
|
174
|
+
type_missing: 'createEnumType',
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
export function resolvePostgresIssueCreationFactoryName(issue: SchemaIssue): string | undefined {
|
|
178
|
+
return POSTGRES_ISSUE_CREATION_FACTORY[issue.kind];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Resolve the control-policy subject coordinate for a single
|
|
183
|
+
* {@link SchemaIssue}. Mirrors the resolution `resolvePostgresCallControlPolicySubject`
|
|
184
|
+
* performs for a generated DDL call, but works *off the issue* — so the
|
|
185
|
+
* planner can partition issues by effective policy before the diff engine
|
|
186
|
+
* runs. `createsNewObject` is derived from the issue's kind: schema/table/
|
|
187
|
+
* type-missing issues describe a brand-new top-level object; everything else
|
|
188
|
+
* touches an existing object.
|
|
189
|
+
*
|
|
190
|
+
* An `extra_table` issue carries no contract namespace coordinate (the table
|
|
191
|
+
* isn't in any contract namespace), so the subject's `namespaceId` falls
|
|
192
|
+
* back to {@link UNBOUND_NAMESPACE_ID}; the call-side resolver does the same
|
|
193
|
+
* for the `DropTableCall` it produces.
|
|
194
|
+
*/
|
|
195
|
+
export function resolvePostgresIssueControlPolicySubject(
|
|
196
|
+
issue: SchemaIssue,
|
|
197
|
+
contract: Contract<SqlStorage>,
|
|
198
|
+
): ControlPolicySubject | undefined {
|
|
199
|
+
const createsNewObject = POSTGRES_ISSUE_CREATION_FACTORY[issue.kind] !== undefined;
|
|
200
|
+
|
|
201
|
+
if (issue.kind === 'missing_schema' && issue.namespaceId) {
|
|
202
|
+
return { namespaceId: issue.namespaceId, createsNewObject };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if ('typeName' in issue && issue.typeName) {
|
|
206
|
+
const namespaceId =
|
|
207
|
+
'namespaceId' in issue && issue.namespaceId ? issue.namespaceId : UNBOUND_NAMESPACE_ID;
|
|
208
|
+
const ns = contract.storage.namespaces[namespaceId];
|
|
209
|
+
const rawEnum = ns && 'enum' in ns && ns.enum != null ? ns.enum[issue.typeName] : undefined;
|
|
210
|
+
const controlPolicy = isPostgresEnumStorageEntry(rawEnum) ? rawEnum.control : undefined;
|
|
211
|
+
return {
|
|
212
|
+
namespaceId,
|
|
213
|
+
...ifDefined('explicitNodeControlPolicy', controlPolicy),
|
|
214
|
+
typeName: issue.typeName,
|
|
215
|
+
createsNewObject,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if ('table' in issue && issue.table) {
|
|
220
|
+
const namespaceId =
|
|
221
|
+
'namespaceId' in issue && issue.namespaceId
|
|
222
|
+
? issue.namespaceId
|
|
223
|
+
: resolveNamespaceIdForTable(contract, issue.table, undefined);
|
|
224
|
+
const table = storageTableAt(contract.storage, namespaceId, issue.table);
|
|
225
|
+
return {
|
|
226
|
+
namespaceId,
|
|
227
|
+
...ifDefined('explicitNodeControlPolicy', table?.control),
|
|
228
|
+
table: issue.table,
|
|
229
|
+
...ifDefined('column', 'column' in issue ? issue.column : undefined),
|
|
230
|
+
createsNewObject,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return undefined;
|
|
235
|
+
}
|
|
@@ -2,10 +2,13 @@ import type { Contract } from '@prisma-next/contract/types';
|
|
|
2
2
|
import type {
|
|
3
3
|
MigrationOperationPolicy,
|
|
4
4
|
SqlMigrationPlannerPlanOptions,
|
|
5
|
+
SqlPlannerConflict,
|
|
5
6
|
SqlPlannerFailureResult,
|
|
6
7
|
} from '@prisma-next/family-sql/control';
|
|
7
8
|
import {
|
|
8
9
|
extractCodecControlHooks,
|
|
10
|
+
partitionCallsByControlPolicy,
|
|
11
|
+
partitionIssuesByControlPolicy,
|
|
9
12
|
planFieldEventOperations,
|
|
10
13
|
plannerFailure,
|
|
11
14
|
} from '@prisma-next/family-sql/control';
|
|
@@ -18,10 +21,19 @@ import type {
|
|
|
18
21
|
SchemaIssue,
|
|
19
22
|
} from '@prisma-next/framework-components/control';
|
|
20
23
|
import { UNBOUND_NAMESPACE_ID } from '@prisma-next/framework-components/ir';
|
|
24
|
+
import { blindCast } from '@prisma-next/utils/casts';
|
|
25
|
+
import { postgresColumnsCompatible } from '../column-type-compatibility';
|
|
21
26
|
import { parsePostgresDefault } from '../default-normalizer';
|
|
22
27
|
import { normalizeSchemaNativeType } from '../native-type-normalizer';
|
|
28
|
+
import {
|
|
29
|
+
formatPostgresControlPolicySubjectLabel,
|
|
30
|
+
resolvePostgresCallControlPolicySubject,
|
|
31
|
+
resolvePostgresIssueControlPolicySubject,
|
|
32
|
+
resolvePostgresIssueCreationFactoryName,
|
|
33
|
+
} from './control-policy';
|
|
23
34
|
import { createResolveExistingEnumValues } from './enum-planning';
|
|
24
35
|
import { planIssues } from './issue-planner';
|
|
36
|
+
import type { PostgresOpFactoryCall } from './op-factory-call';
|
|
25
37
|
import { TypeScriptRenderablePostgresMigration } from './planner-produced-postgres-migration';
|
|
26
38
|
import { postgresPlannerStrategies } from './planner-strategies';
|
|
27
39
|
import { verifyPostgresNamespacePresence } from './verify-postgres-namespaces';
|
|
@@ -52,7 +64,11 @@ export function createPostgresMigrationPlanner(): PostgresMigrationPlanner {
|
|
|
52
64
|
* uniformly.
|
|
53
65
|
*/
|
|
54
66
|
export type PostgresPlanResult =
|
|
55
|
-
| {
|
|
67
|
+
| {
|
|
68
|
+
readonly kind: 'success';
|
|
69
|
+
readonly plan: TypeScriptRenderablePostgresMigration;
|
|
70
|
+
readonly warnings?: readonly SqlPlannerConflict[];
|
|
71
|
+
}
|
|
56
72
|
| SqlPlannerFailureResult;
|
|
57
73
|
|
|
58
74
|
/**
|
|
@@ -131,8 +147,25 @@ export class PostgresMigrationPlanner implements MigrationPlanner<'sql', 'postgr
|
|
|
131
147
|
const codecHooks = extractCodecControlHooks(options.frameworkComponents);
|
|
132
148
|
const storageTypes = options.contract.storage.types ?? {};
|
|
133
149
|
|
|
134
|
-
|
|
150
|
+
// Input-side control-policy partition. `external` / `observed` subjects
|
|
151
|
+
// — and non-creation issues for `tolerated` subjects — are dropped from
|
|
152
|
+
// the planner's input entirely; the planner never observes them, never
|
|
153
|
+
// diffs them, never generates DDL for them. Suppression warnings are
|
|
154
|
+
// built directly from the suppressed partition (one per subject), so the
|
|
155
|
+
// user-visible message survives even when the planner would have failed
|
|
156
|
+
// to model the subject's live shape.
|
|
157
|
+
const issuePartition = partitionIssuesByControlPolicy({
|
|
135
158
|
issues: schemaIssues,
|
|
159
|
+
contract: options.contract,
|
|
160
|
+
resolveControlPolicySubject: (issue) =>
|
|
161
|
+
resolvePostgresIssueControlPolicySubject(issue, options.contract),
|
|
162
|
+
resolveCreationFactoryName: resolvePostgresIssueCreationFactoryName,
|
|
163
|
+
formatSubjectLabel: (factoryName, subject) =>
|
|
164
|
+
formatPostgresControlPolicySubjectLabel(factoryName, subject, options.contract),
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const result = planIssues({
|
|
168
|
+
issues: issuePartition.plannable,
|
|
136
169
|
toContract: options.contract,
|
|
137
170
|
// `fromContract` is only supplied by `migration plan`. It is `null` for
|
|
138
171
|
// `db update` / `db init`, which means data-safety strategies needing
|
|
@@ -163,10 +196,26 @@ export class PostgresMigrationPlanner implements MigrationPlanner<'sql', 'postgr
|
|
|
163
196
|
newContract: options.contract,
|
|
164
197
|
codecHooks,
|
|
165
198
|
});
|
|
166
|
-
// Codec
|
|
167
|
-
//
|
|
168
|
-
|
|
169
|
-
|
|
199
|
+
// Codec hook ops are target-agnostic `OpFactoryCall`; Postgres planning
|
|
200
|
+
// lifts them at this integration boundary (see field-event-planner JSDoc).
|
|
201
|
+
const fieldEventPostgresCalls = blindCast<
|
|
202
|
+
readonly PostgresOpFactoryCall[],
|
|
203
|
+
'Codec hook ops conform to PostgresOpFactoryCall at the app emitter boundary'
|
|
204
|
+
>(fieldEventOps);
|
|
205
|
+
const fieldEventPartition = partitionCallsByControlPolicy({
|
|
206
|
+
calls: fieldEventPostgresCalls,
|
|
207
|
+
contract: options.contract,
|
|
208
|
+
resolveControlPolicySubject: (call) =>
|
|
209
|
+
resolvePostgresCallControlPolicySubject(call, options.contract),
|
|
210
|
+
resolveFactoryName: (call) => call.factoryName,
|
|
211
|
+
formatSubjectLabel: (factoryName, subject) =>
|
|
212
|
+
formatPostgresControlPolicySubjectLabel(factoryName, subject, options.contract),
|
|
213
|
+
});
|
|
214
|
+
const calls = [...result.value.calls, ...fieldEventPartition.kept];
|
|
215
|
+
const warnings: SqlPlannerConflict[] = [
|
|
216
|
+
...issuePartition.warnings,
|
|
217
|
+
...fieldEventPartition.warnings,
|
|
218
|
+
];
|
|
170
219
|
|
|
171
220
|
return Object.freeze({
|
|
172
221
|
kind: 'success' as const,
|
|
@@ -178,6 +227,7 @@ export class PostgresMigrationPlanner implements MigrationPlanner<'sql', 'postgr
|
|
|
178
227
|
},
|
|
179
228
|
options.spaceId,
|
|
180
229
|
),
|
|
230
|
+
...(warnings.length > 0 ? { warnings: Object.freeze(warnings) } : {}),
|
|
181
231
|
});
|
|
182
232
|
}
|
|
183
233
|
|
|
@@ -208,6 +258,7 @@ export class PostgresMigrationPlanner implements MigrationPlanner<'sql', 'postgr
|
|
|
208
258
|
frameworkComponents: options.frameworkComponents,
|
|
209
259
|
normalizeDefault: parsePostgresDefault,
|
|
210
260
|
normalizeNativeType: normalizeSchemaNativeType,
|
|
261
|
+
columnsCompatible: postgresColumnsCompatible,
|
|
211
262
|
resolveExistingEnumValues: createResolveExistingEnumValues(options.contract.storage),
|
|
212
263
|
};
|
|
213
264
|
const verifyResult = verifySqlSchema(verifyOptions);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ContractMarkerRecord } from '@prisma-next/contract/types';
|
|
1
|
+
import type { Contract, ContractMarkerRecord } from '@prisma-next/contract/types';
|
|
2
2
|
import type {
|
|
3
3
|
MigrationOperationPolicy,
|
|
4
4
|
SqlControlFamilyInstance,
|
|
@@ -19,10 +19,12 @@ import type {
|
|
|
19
19
|
} from '@prisma-next/framework-components/control';
|
|
20
20
|
import { APP_SPACE_ID } from '@prisma-next/framework-components/control';
|
|
21
21
|
import { UNBOUND_NAMESPACE_ID } from '@prisma-next/framework-components/ir';
|
|
22
|
+
import type { SqlStorage } from '@prisma-next/sql-contract/types';
|
|
22
23
|
import { SqlQueryError } from '@prisma-next/sql-errors';
|
|
23
24
|
import { ifDefined } from '@prisma-next/utils/defined';
|
|
24
25
|
import type { Result } from '@prisma-next/utils/result';
|
|
25
26
|
import { notOk, ok, okVoid } from '@prisma-next/utils/result';
|
|
27
|
+
import { postgresColumnsCompatible } from '../column-type-compatibility';
|
|
26
28
|
import { parsePostgresDefault } from '../default-normalizer';
|
|
27
29
|
import { normalizeSchemaNativeType } from '../native-type-normalizer';
|
|
28
30
|
import { createResolveExistingEnumValues } from './enum-planning';
|
|
@@ -30,9 +32,6 @@ import type { PostgresPlanTargetDetails } from './planner-target-details';
|
|
|
30
32
|
import {
|
|
31
33
|
buildLedgerInsertStatement,
|
|
32
34
|
buildMergeMarkerStatements,
|
|
33
|
-
ensureLedgerTableStatement,
|
|
34
|
-
ensureMarkerTableStatement,
|
|
35
|
-
ensurePrismaContractSchemaStatement,
|
|
36
35
|
type SqlStatement,
|
|
37
36
|
} from './statement-builders';
|
|
38
37
|
|
|
@@ -107,7 +106,7 @@ class PostgresMigrationRunner implements SqlMigrationRunner<PostgresPlanTargetDe
|
|
|
107
106
|
if (!policyCheck.ok) return policyCheck;
|
|
108
107
|
|
|
109
108
|
await this.acquireLock(driver, lockKey);
|
|
110
|
-
const ensureResult = await this.ensureControlTables(driver);
|
|
109
|
+
const ensureResult = await this.ensureControlTables(driver, options.destinationContract);
|
|
111
110
|
if (!ensureResult.ok) return ensureResult;
|
|
112
111
|
const existingMarker = await this.family.readMarker({ driver, space });
|
|
113
112
|
|
|
@@ -146,6 +145,7 @@ class PostgresMigrationRunner implements SqlMigrationRunner<PostgresPlanTargetDe
|
|
|
146
145
|
frameworkComponents: options.frameworkComponents,
|
|
147
146
|
normalizeDefault: parsePostgresDefault,
|
|
148
147
|
normalizeNativeType: normalizeSchemaNativeType,
|
|
148
|
+
columnsCompatible: postgresColumnsCompatible,
|
|
149
149
|
resolveExistingEnumValues: createResolveExistingEnumValues(
|
|
150
150
|
options.destinationContract.storage,
|
|
151
151
|
),
|
|
@@ -166,7 +166,12 @@ class PostgresMigrationRunner implements SqlMigrationRunner<PostgresPlanTargetDe
|
|
|
166
166
|
|
|
167
167
|
if (!isSelfEdgeNoOp) {
|
|
168
168
|
await this.upsertMarker(driver, options, existingMarker, space);
|
|
169
|
-
await this.
|
|
169
|
+
await this.recordLedgerEntries(
|
|
170
|
+
driver,
|
|
171
|
+
options,
|
|
172
|
+
existingMarker,
|
|
173
|
+
applyValue.executedOperations,
|
|
174
|
+
);
|
|
170
175
|
}
|
|
171
176
|
|
|
172
177
|
return runnerSuccess({
|
|
@@ -282,18 +287,22 @@ class PostgresMigrationRunner implements SqlMigrationRunner<PostgresPlanTargetDe
|
|
|
282
287
|
|
|
283
288
|
private async ensureControlTables(
|
|
284
289
|
driver: SqlMigrationRunnerExecuteOptions<PostgresPlanTargetDetails>['driver'],
|
|
290
|
+
contract: Contract<SqlStorage>,
|
|
285
291
|
): Promise<Result<void, SqlMigrationRunnerFailure>> {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
292
|
+
const lowererContext = { contract };
|
|
293
|
+
const bootstrapQueries = this.family.bootstrapControlTableQueries();
|
|
294
|
+
const [schemaQuery, ...tableQueries] = bootstrapQueries;
|
|
295
|
+
if (schemaQuery === undefined) {
|
|
296
|
+
throw new Error('Postgres control-table bootstrap must include CREATE SCHEMA');
|
|
297
|
+
}
|
|
298
|
+
await this.executeStatement(driver, this.family.lowerAst(schemaQuery, lowererContext));
|
|
291
299
|
const legacyDetection = await this.detectLegacyMarkerShape(driver);
|
|
292
300
|
if (!legacyDetection.ok) {
|
|
293
301
|
return legacyDetection;
|
|
294
302
|
}
|
|
295
|
-
|
|
296
|
-
|
|
303
|
+
for (const query of tableQueries) {
|
|
304
|
+
await this.executeStatement(driver, this.family.lowerAst(query, lowererContext));
|
|
305
|
+
}
|
|
297
306
|
return okVoid();
|
|
298
307
|
}
|
|
299
308
|
|
|
@@ -612,25 +621,55 @@ class PostgresMigrationRunner implements SqlMigrationRunner<PostgresPlanTargetDe
|
|
|
612
621
|
await this.executeStatement(driver, statement);
|
|
613
622
|
}
|
|
614
623
|
|
|
615
|
-
private async
|
|
624
|
+
private async recordLedgerEntries(
|
|
616
625
|
driver: SqlMigrationRunnerExecuteOptions<PostgresPlanTargetDetails>['driver'],
|
|
617
626
|
options: SqlMigrationRunnerExecuteOptions<PostgresPlanTargetDetails>,
|
|
618
627
|
existingMarker: ContractMarkerRecord | null,
|
|
619
628
|
executedOperations: readonly SqlMigrationPlanOperation<PostgresPlanTargetDetails>[],
|
|
620
629
|
): Promise<void> {
|
|
621
|
-
const
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
630
|
+
const plan = options.plan;
|
|
631
|
+
const space = plan.spaceId;
|
|
632
|
+
const destinationProfileHash =
|
|
633
|
+
plan.destination.profileHash ??
|
|
634
|
+
options.destinationContract.profileHash ??
|
|
635
|
+
plan.destination.storageHash;
|
|
636
|
+
const edges = options.migrationEdges;
|
|
637
|
+
const totalEdgeOps = edges.reduce((sum, edge) => sum + edge.operationCount, 0);
|
|
638
|
+
if (totalEdgeOps !== plan.operations.length) {
|
|
639
|
+
throw new Error(
|
|
640
|
+
`Ledger write: plan.operations length (${plan.operations.length}) does not match sum of migrationEdges operationCount (${totalEdgeOps})`,
|
|
641
|
+
);
|
|
642
|
+
}
|
|
643
|
+
// The ledger records the operations as executed — idempotency-skipped ops
|
|
644
|
+
// are substituted with skip records (empty `execute`) by `applyPlan`, so the
|
|
645
|
+
// journal reflects what actually ran rather than the raw plan.
|
|
646
|
+
let offset = 0;
|
|
647
|
+
const lastIndex = edges.length - 1;
|
|
648
|
+
for (const [i, edge] of edges.entries()) {
|
|
649
|
+
const edgeOps = executedOperations.slice(offset, offset + edge.operationCount);
|
|
650
|
+
offset += edge.operationCount;
|
|
651
|
+
const isFirst = i === 0;
|
|
652
|
+
const isLast = i === lastIndex;
|
|
653
|
+
await this.executeStatement(
|
|
654
|
+
driver,
|
|
655
|
+
buildLedgerInsertStatement({
|
|
656
|
+
space,
|
|
657
|
+
migrationName: edge.dirName,
|
|
658
|
+
migrationHash: edge.migrationHash,
|
|
659
|
+
originStorageHash: edge.from === '' ? null : edge.from,
|
|
660
|
+
originProfileHash:
|
|
661
|
+
isFirst && existingMarker?.storageHash === edge.from
|
|
662
|
+
? (existingMarker.profileHash ?? null)
|
|
663
|
+
: null,
|
|
664
|
+
destinationStorageHash: edge.to,
|
|
665
|
+
destinationProfileHash:
|
|
666
|
+
isLast && edge.to === plan.destination.storageHash ? destinationProfileHash : null,
|
|
667
|
+
contractJsonBefore: isFirst ? (existingMarker?.contractJson ?? null) : null,
|
|
668
|
+
contractJsonAfter: isLast ? options.destinationContract : null,
|
|
669
|
+
operations: edgeOps,
|
|
670
|
+
}),
|
|
671
|
+
);
|
|
672
|
+
}
|
|
634
673
|
}
|
|
635
674
|
|
|
636
675
|
private async acquireLock(
|
|
@@ -40,6 +40,9 @@ export const ensureLedgerTableStatement: SqlStatement = {
|
|
|
40
40
|
sql: `create table if not exists prisma_contract.ledger (
|
|
41
41
|
id bigserial primary key,
|
|
42
42
|
created_at timestamptz not null default now(),
|
|
43
|
+
space text not null,
|
|
44
|
+
migration_name text not null,
|
|
45
|
+
migration_hash text not null,
|
|
43
46
|
origin_core_hash text,
|
|
44
47
|
origin_profile_hash text,
|
|
45
48
|
destination_core_hash text not null,
|
|
@@ -138,6 +141,9 @@ export function buildMergeMarkerStatements(input: MergeMarkerInput): {
|
|
|
138
141
|
}
|
|
139
142
|
|
|
140
143
|
export interface LedgerInsertInput {
|
|
144
|
+
readonly space: string;
|
|
145
|
+
readonly migrationName: string;
|
|
146
|
+
readonly migrationHash: string;
|
|
141
147
|
readonly originStorageHash?: string | null;
|
|
142
148
|
readonly originProfileHash?: string | null;
|
|
143
149
|
readonly destinationStorageHash: string;
|
|
@@ -150,6 +156,9 @@ export interface LedgerInsertInput {
|
|
|
150
156
|
export function buildLedgerInsertStatement(input: LedgerInsertInput): SqlStatement {
|
|
151
157
|
return {
|
|
152
158
|
sql: `insert into prisma_contract.ledger (
|
|
159
|
+
space,
|
|
160
|
+
migration_name,
|
|
161
|
+
migration_hash,
|
|
153
162
|
origin_core_hash,
|
|
154
163
|
origin_profile_hash,
|
|
155
164
|
destination_core_hash,
|
|
@@ -162,11 +171,17 @@ export function buildLedgerInsertStatement(input: LedgerInsertInput): SqlStateme
|
|
|
162
171
|
$2,
|
|
163
172
|
$3,
|
|
164
173
|
$4,
|
|
165
|
-
$5
|
|
166
|
-
$6
|
|
167
|
-
$7
|
|
174
|
+
$5,
|
|
175
|
+
$6,
|
|
176
|
+
$7,
|
|
177
|
+
$8::jsonb,
|
|
178
|
+
$9::jsonb,
|
|
179
|
+
$10::jsonb
|
|
168
180
|
)`,
|
|
169
181
|
params: [
|
|
182
|
+
input.space,
|
|
183
|
+
input.migrationName,
|
|
184
|
+
input.migrationHash,
|
|
170
185
|
input.originStorageHash ?? null,
|
|
171
186
|
input.originProfileHash ?? null,
|
|
172
187
|
input.destinationStorageHash,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { postgresColumnsCompatible } from '../core/column-type-compatibility';
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"descriptor-meta-Uu8QaClr.mjs","names":[],"sources":["../src/core/descriptor-meta.ts"],"sourcesContent":["import type { CodecTypes } from '../exports/codec-types';\nimport {\n postgresAuthoringEntityTypes,\n postgresAuthoringFieldPresets,\n postgresAuthoringTypes,\n} from './authoring';\nimport { postgresTargetDescriptorMetaRuntime } from './descriptor-meta-runtime';\n\nconst postgresTargetDescriptorMetaBase = {\n ...postgresTargetDescriptorMetaRuntime,\n authoring: {\n type: postgresAuthoringTypes,\n field: postgresAuthoringFieldPresets,\n entityTypes: postgresAuthoringEntityTypes,\n },\n} as const;\n\nexport const postgresTargetDescriptorMeta: typeof postgresTargetDescriptorMetaBase & {\n readonly __codecTypes?: CodecTypes;\n} = postgresTargetDescriptorMetaBase;\n"],"mappings":";AAiBA,MAAa,+BAET;CAVF,GAAG;CACH,WAAW;EACT,MAAM;EACN,OAAO;EACP,aAAa;CACf;AAKE"}
|