@prisma-next/target-postgres 0.14.0 → 0.15.0-dev.10

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.
Files changed (269) hide show
  1. package/dist/authoring-BtsOWgI3.mjs +674 -0
  2. package/dist/authoring-BtsOWgI3.mjs.map +1 -0
  3. package/dist/canonicalize-D1F4vUnP.d.mts +48 -0
  4. package/dist/canonicalize-D1F4vUnP.d.mts.map +1 -0
  5. package/dist/{codec-ids-BvytN2P8.mjs → codec-ids-B10ZZHzw.mjs} +4 -2
  6. package/dist/codec-ids-B10ZZHzw.mjs.map +1 -0
  7. package/dist/{codec-ids-CnXu9Qy3.d.mts → codec-ids-CI144z7g.d.mts} +4 -2
  8. package/dist/codec-ids-CI144z7g.d.mts.map +1 -0
  9. package/dist/codec-ids.d.mts +2 -2
  10. package/dist/codec-ids.mjs +2 -2
  11. package/dist/{codec-types-DHCkwPKE.d.mts → codec-types-CTfT4U4O.d.mts} +4 -2
  12. package/dist/codec-types-CTfT4U4O.d.mts.map +1 -0
  13. package/dist/codec-types.d.mts +1 -1
  14. package/dist/{codecs--0A5_4Bq.d.mts → codecs-D7W9O0Xx.d.mts} +134 -4
  15. package/dist/codecs-D7W9O0Xx.d.mts.map +1 -0
  16. package/dist/codecs-DRbb26Or.mjs +955 -0
  17. package/dist/codecs-DRbb26Or.mjs.map +1 -0
  18. package/dist/codecs.d.mts +2 -2
  19. package/dist/codecs.mjs +3 -757
  20. package/dist/codecs.mjs.map +1 -1
  21. package/dist/contract-free.d.mts +33 -3
  22. package/dist/contract-free.d.mts.map +1 -1
  23. package/dist/contract-free.mjs +23 -7
  24. package/dist/contract-free.mjs.map +1 -1
  25. package/dist/control-policy-DaLdbcYE.mjs +1254 -0
  26. package/dist/control-policy-DaLdbcYE.mjs.map +1 -0
  27. package/dist/control.d.mts +2 -2
  28. package/dist/control.d.mts.map +1 -1
  29. package/dist/control.mjs +880 -36
  30. package/dist/control.mjs.map +1 -1
  31. package/dist/data-transform-BOWpliq8.mjs.map +1 -1
  32. package/dist/{data-transform-DDgWdB5o.d.mts → data-transform-DPzwBMhW.d.mts} +3 -3
  33. package/dist/{data-transform-DDgWdB5o.d.mts.map → data-transform-DPzwBMhW.d.mts.map} +1 -1
  34. package/dist/data-transform.d.mts +1 -1
  35. package/dist/{ddl-QDyOSeLc.mjs → ddl-GTTOvjx_.mjs} +128 -6
  36. package/dist/ddl-GTTOvjx_.mjs.map +1 -0
  37. package/dist/ddl.d.mts +3 -2
  38. package/dist/ddl.mjs +2 -2
  39. package/dist/default-normalizer.d.mts +1 -1
  40. package/dist/default-normalizer.d.mts.map +1 -1
  41. package/dist/default-normalizer.mjs +247 -1
  42. package/dist/default-normalizer.mjs.map +1 -0
  43. package/dist/descriptor-meta-CFQKLXl_.mjs +23 -0
  44. package/dist/descriptor-meta-CFQKLXl_.mjs.map +1 -0
  45. package/dist/descriptor-meta-runtime-UwYdV8fD.mjs +12 -0
  46. package/dist/descriptor-meta-runtime-UwYdV8fD.mjs.map +1 -0
  47. package/dist/diff-database-schema-oY3brcLj.mjs +368 -0
  48. package/dist/diff-database-schema-oY3brcLj.mjs.map +1 -0
  49. package/dist/diff-database-schema.d.mts +37 -0
  50. package/dist/diff-database-schema.d.mts.map +1 -0
  51. package/dist/diff-database-schema.mjs +2 -0
  52. package/dist/errors-DO-w0EUS.mjs +35 -0
  53. package/dist/errors-DO-w0EUS.mjs.map +1 -0
  54. package/dist/errors.d.mts +11 -9
  55. package/dist/errors.d.mts.map +1 -1
  56. package/dist/errors.mjs +1 -1
  57. package/dist/issue-planner.d.mts +35 -11
  58. package/dist/issue-planner.d.mts.map +1 -1
  59. package/dist/issue-planner.mjs +2 -2
  60. package/dist/migration.d.mts +5 -5
  61. package/dist/migration.d.mts.map +1 -1
  62. package/dist/migration.mjs +4 -4
  63. package/dist/namespace-ids-xtp_ZY86.mjs +7 -0
  64. package/dist/namespace-ids-xtp_ZY86.mjs.map +1 -0
  65. package/dist/native-type-normalizer.mjs +37 -1
  66. package/dist/native-type-normalizer.mjs.map +1 -0
  67. package/dist/nodes-DJC5B8Id.mjs +192 -0
  68. package/dist/nodes-DJC5B8Id.mjs.map +1 -0
  69. package/dist/nodes-Dv5eCSq9.d.mts +170 -0
  70. package/dist/nodes-Dv5eCSq9.d.mts.map +1 -0
  71. package/dist/{op-factory-call-DmQEc3XV.d.mts → op-factory-call-CvH0JeBW.d.mts} +119 -17
  72. package/dist/op-factory-call-CvH0JeBW.d.mts.map +1 -0
  73. package/dist/{op-factory-call-D_p5vxwt.mjs → op-factory-call-tBcLo_B8.mjs} +496 -38
  74. package/dist/op-factory-call-tBcLo_B8.mjs.map +1 -0
  75. package/dist/op-factory-call.d.mts +2 -2
  76. package/dist/op-factory-call.mjs +2 -2
  77. package/dist/pack.d.mts +321 -3
  78. package/dist/pack.d.mts.map +1 -1
  79. package/dist/pack.mjs +1 -1
  80. package/dist/{planner-ddl-builders-B2wOwLqI.mjs → planner-ddl-builders-BKlmGDJl.mjs} +17 -28
  81. package/dist/planner-ddl-builders-BKlmGDJl.mjs.map +1 -0
  82. package/dist/planner-ddl-builders.d.mts +4 -6
  83. package/dist/planner-ddl-builders.d.mts.map +1 -1
  84. package/dist/planner-ddl-builders.mjs +2 -2
  85. package/dist/planner-identity-values-CJPha2Sz.mjs.map +1 -1
  86. package/dist/planner-identity-values.d.mts +8 -2
  87. package/dist/planner-identity-values.d.mts.map +1 -1
  88. package/dist/planner-identity-values.mjs +2 -2
  89. package/dist/planner-ootbxGty.mjs +390 -0
  90. package/dist/planner-ootbxGty.mjs.map +1 -0
  91. package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs → planner-produced-postgres-migration-DVsaobdV.mjs} +3 -3
  92. package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs.map → planner-produced-postgres-migration-DVsaobdV.mjs.map} +1 -1
  93. package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts → planner-produced-postgres-migration-k33bb9Sm.d.mts} +4 -4
  94. package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts.map → planner-produced-postgres-migration-k33bb9Sm.d.mts.map} +1 -1
  95. package/dist/planner-produced-postgres-migration.d.mts +1 -1
  96. package/dist/planner-produced-postgres-migration.mjs +1 -1
  97. package/dist/{planner-sql-checks-jqUUGyQR.mjs → planner-sql-checks-zqNfs2XZ.mjs} +5 -5
  98. package/dist/{planner-sql-checks-jqUUGyQR.mjs.map → planner-sql-checks-zqNfs2XZ.mjs.map} +1 -1
  99. package/dist/planner-sql-checks.d.mts +2 -2
  100. package/dist/planner-sql-checks.d.mts.map +1 -1
  101. package/dist/planner-sql-checks.mjs +1 -1
  102. package/dist/{planner-target-details-CIY6tLeo.d.mts → planner-target-details-HkP4RrRO.d.mts} +2 -2
  103. package/dist/planner-target-details-HkP4RrRO.d.mts.map +1 -0
  104. package/dist/planner-target-details.d.mts +1 -1
  105. package/dist/planner.d.mts +72 -17
  106. package/dist/planner.d.mts.map +1 -1
  107. package/dist/planner.mjs +3 -2
  108. package/dist/postgres-contract-view-C9j5cqP0.d.mts +34 -0
  109. package/dist/postgres-contract-view-C9j5cqP0.d.mts.map +1 -0
  110. package/dist/{postgres-contract-serializer-k3TAcPMY.mjs → postgres-contract-view-DM8sN1Ot.mjs} +45 -41
  111. package/dist/postgres-contract-view-DM8sN1Ot.mjs.map +1 -0
  112. package/dist/postgres-database-schema-node-DJOTywwQ.d.mts +271 -0
  113. package/dist/postgres-database-schema-node-DJOTywwQ.d.mts.map +1 -0
  114. package/dist/postgres-migration-Bt3sXoID.mjs +194 -0
  115. package/dist/postgres-migration-Bt3sXoID.mjs.map +1 -0
  116. package/dist/{postgres-migration-Y4YBJqkS.d.mts → postgres-migration-ulfqRGA1.d.mts} +83 -9
  117. package/dist/postgres-migration-ulfqRGA1.d.mts.map +1 -0
  118. package/dist/postgres-native-enum-BwVNf0Ku.mjs +45 -0
  119. package/dist/postgres-native-enum-BwVNf0Ku.mjs.map +1 -0
  120. package/dist/postgres-rls-policy--CSddvMR.d.mts +52 -0
  121. package/dist/postgres-rls-policy--CSddvMR.d.mts.map +1 -0
  122. package/dist/postgres-role-schema-node-bg32e7I-.mjs +191 -0
  123. package/dist/postgres-role-schema-node-bg32e7I-.mjs.map +1 -0
  124. package/dist/{postgres-schema-COGZ1ark.mjs → postgres-schema-Cm4DtiGA.mjs} +200 -11
  125. package/dist/postgres-schema-Cm4DtiGA.mjs.map +1 -0
  126. package/dist/postgres-schema-DKq6s1P3.d.mts +269 -0
  127. package/dist/postgres-schema-DKq6s1P3.d.mts.map +1 -0
  128. package/dist/postgres-table-schema-node-D6LvInCe.mjs +216 -0
  129. package/dist/postgres-table-schema-node-D6LvInCe.mjs.map +1 -0
  130. package/dist/render-ops.d.mts +1 -1
  131. package/dist/{render-typescript-KMgosran.mjs → render-typescript-7yqILcwr.mjs} +45 -16
  132. package/dist/render-typescript-7yqILcwr.mjs.map +1 -0
  133. package/dist/render-typescript.mjs +1 -1
  134. package/dist/resolve-ddl-schema-XyBaaenM.mjs +22 -0
  135. package/dist/resolve-ddl-schema-XyBaaenM.mjs.map +1 -0
  136. package/dist/rls-canonicalize.d.mts +27 -0
  137. package/dist/rls-canonicalize.d.mts.map +1 -0
  138. package/dist/rls-canonicalize.mjs +2 -0
  139. package/dist/runtime.d.mts +6 -8
  140. package/dist/runtime.d.mts.map +1 -1
  141. package/dist/runtime.mjs +3 -3
  142. package/dist/runtime.mjs.map +1 -1
  143. package/dist/schema-ir-annotations.d.mts +15 -0
  144. package/dist/schema-ir-annotations.d.mts.map +1 -0
  145. package/dist/schema-ir-annotations.mjs +2 -0
  146. package/dist/schema-node-kinds-CrYzCR1i.d.mts +30 -0
  147. package/dist/schema-node-kinds-CrYzCR1i.d.mts.map +1 -0
  148. package/dist/{sql-utils-DcfMz4MQ.mjs → sql-utils-SU4FDvIV.mjs} +12 -3
  149. package/dist/{sql-utils-DcfMz4MQ.mjs.map → sql-utils-SU4FDvIV.mjs.map} +1 -1
  150. package/dist/sql-utils.d.mts +8 -1
  151. package/dist/sql-utils.d.mts.map +1 -1
  152. package/dist/sql-utils.mjs +2 -2
  153. package/dist/types.d.mts +5 -140
  154. package/dist/types.mjs +5 -2
  155. package/dist/wire-name-DbQ9-2RC.mjs +104 -0
  156. package/dist/wire-name-DbQ9-2RC.mjs.map +1 -0
  157. package/package.json +26 -20
  158. package/src/contract-free/checks.ts +153 -0
  159. package/src/contract-free/control-bootstrap.ts +20 -3
  160. package/src/contract-free/ddl.ts +98 -1
  161. package/src/core/authoring.ts +726 -2
  162. package/src/core/codec-helpers.ts +33 -7
  163. package/src/core/codec-ids.ts +2 -0
  164. package/src/core/codec-type-map.ts +4 -0
  165. package/src/core/codecs.ts +236 -12
  166. package/src/core/ddl/nodes.ts +185 -2
  167. package/src/core/default-normalizer.ts +122 -2
  168. package/src/core/descriptor-meta.ts +12 -1
  169. package/src/core/entity-kinds.ts +41 -0
  170. package/src/core/errors.ts +14 -12
  171. package/src/core/migrations/bound-schema.ts +10 -0
  172. package/src/core/migrations/column-ddl-rendering.ts +117 -0
  173. package/src/core/migrations/contract-to-postgres-database-schema-node.ts +269 -0
  174. package/src/core/migrations/control-policy.ts +218 -65
  175. package/src/core/migrations/diff-database-schema.ts +252 -0
  176. package/src/core/migrations/issue-planner.ts +782 -660
  177. package/src/core/migrations/op-factory-call.ts +415 -31
  178. package/src/core/migrations/operations/columns.ts +24 -12
  179. package/src/core/migrations/operations/constraints.ts +1 -1
  180. package/src/core/migrations/operations/data-transform.ts +6 -0
  181. package/src/core/migrations/operations/dependencies.ts +2 -1
  182. package/src/core/migrations/operations/native-enum-types.ts +62 -0
  183. package/src/core/migrations/operations/rls.ts +187 -0
  184. package/src/core/migrations/operations/shared.ts +0 -30
  185. package/src/core/migrations/planner-ddl-builders.ts +31 -59
  186. package/src/core/migrations/planner-identity-values.ts +1 -1
  187. package/src/core/migrations/planner-recipes.ts +31 -13
  188. package/src/core/migrations/planner-sql-checks.ts +1 -1
  189. package/src/core/migrations/planner-strategies.ts +250 -218
  190. package/src/core/migrations/planner-target-details.ts +3 -1
  191. package/src/core/migrations/planner.ts +358 -67
  192. package/src/core/migrations/postgres-migration.ts +178 -86
  193. package/src/core/migrations/render-typescript.ts +42 -15
  194. package/src/core/migrations/resolve-ddl-schema.ts +26 -0
  195. package/src/core/migrations/runner.ts +11 -16
  196. package/src/core/migrations/verify-postgres-namespaces.ts +45 -31
  197. package/src/core/namespace-ids.ts +3 -0
  198. package/src/core/postgres-contract-serializer.ts +46 -71
  199. package/src/core/postgres-contract-view.ts +48 -0
  200. package/src/core/postgres-native-enum.ts +61 -0
  201. package/src/core/postgres-rls-enablement.ts +34 -0
  202. package/src/core/postgres-rls-policy.ts +64 -0
  203. package/src/core/postgres-role.ts +59 -0
  204. package/src/core/postgres-schema-verifier.ts +10 -7
  205. package/src/core/postgres-schema.ts +85 -15
  206. package/src/core/postgres-validators.ts +34 -0
  207. package/src/core/psl-infer/infer-psl-contract.ts +1290 -0
  208. package/src/core/psl-infer/postgres-default-mapping.ts +16 -0
  209. package/src/core/psl-infer/postgres-type-map.ts +129 -0
  210. package/src/core/rls/canonicalize.ts +65 -0
  211. package/src/core/rls/wire-name.ts +33 -0
  212. package/src/core/schema-ir/node-storage-coordinate.ts +26 -0
  213. package/src/core/schema-ir/postgres-database-schema-node.ts +61 -0
  214. package/src/core/schema-ir/postgres-namespace-schema-node.ts +70 -0
  215. package/src/core/schema-ir/postgres-native-enum-schema-node.ts +83 -0
  216. package/src/core/schema-ir/postgres-policy-schema-node.ts +100 -0
  217. package/src/core/schema-ir/postgres-role-schema-node.ts +65 -0
  218. package/src/core/schema-ir/postgres-table-schema-node.ts +140 -0
  219. package/src/core/schema-ir/schema-node-kinds.ts +122 -0
  220. package/src/core/sql-utils.ts +15 -2
  221. package/src/exports/codecs.ts +5 -0
  222. package/src/exports/contract-free.ts +11 -1
  223. package/src/exports/control.ts +27 -33
  224. package/src/exports/ddl.ts +8 -0
  225. package/src/exports/diff-database-schema.ts +7 -0
  226. package/src/exports/issue-planner.ts +1 -1
  227. package/src/exports/migration.ts +1 -0
  228. package/src/exports/op-factory-call.ts +9 -0
  229. package/src/exports/planner-ddl-builders.ts +0 -2
  230. package/src/exports/planner-identity-values.ts +4 -1
  231. package/src/exports/planner.ts +1 -0
  232. package/src/exports/rls-canonicalize.ts +12 -0
  233. package/src/exports/runtime.ts +1 -0
  234. package/src/exports/schema-ir-annotations.ts +1 -0
  235. package/src/exports/sql-utils.ts +1 -0
  236. package/src/exports/types.ts +38 -0
  237. package/dist/codec-ids-BvytN2P8.mjs.map +0 -1
  238. package/dist/codec-ids-CnXu9Qy3.d.mts.map +0 -1
  239. package/dist/codec-types-DHCkwPKE.d.mts.map +0 -1
  240. package/dist/codecs--0A5_4Bq.d.mts.map +0 -1
  241. package/dist/ddl-QDyOSeLc.mjs.map +0 -1
  242. package/dist/default-normalizer-DyyCHQWs.mjs +0 -131
  243. package/dist/default-normalizer-DyyCHQWs.mjs.map +0 -1
  244. package/dist/descriptor-meta-CpGygXpI.mjs +0 -140
  245. package/dist/descriptor-meta-CpGygXpI.mjs.map +0 -1
  246. package/dist/errors-CUk87ByX.mjs +0 -33
  247. package/dist/errors-CUk87ByX.mjs.map +0 -1
  248. package/dist/issue-planner-DL6g3CmE.mjs +0 -824
  249. package/dist/issue-planner-DL6g3CmE.mjs.map +0 -1
  250. package/dist/native-type-normalizer-Bc9XJzWC.mjs +0 -38
  251. package/dist/native-type-normalizer-Bc9XJzWC.mjs.map +0 -1
  252. package/dist/nodes-Bbhs2rwj.mjs +0 -76
  253. package/dist/nodes-Bbhs2rwj.mjs.map +0 -1
  254. package/dist/nodes-pLeLgdis.d.mts +0 -67
  255. package/dist/nodes-pLeLgdis.d.mts.map +0 -1
  256. package/dist/op-factory-call-D_p5vxwt.mjs.map +0 -1
  257. package/dist/op-factory-call-DmQEc3XV.d.mts.map +0 -1
  258. package/dist/planner-Bs_baQax.mjs +0 -324
  259. package/dist/planner-Bs_baQax.mjs.map +0 -1
  260. package/dist/planner-ddl-builders-B2wOwLqI.mjs.map +0 -1
  261. package/dist/planner-target-details-CIY6tLeo.d.mts.map +0 -1
  262. package/dist/postgres-contract-serializer-k3TAcPMY.mjs.map +0 -1
  263. package/dist/postgres-migration-B5jKrXv3.mjs +0 -145
  264. package/dist/postgres-migration-B5jKrXv3.mjs.map +0 -1
  265. package/dist/postgres-migration-Y4YBJqkS.d.mts.map +0 -1
  266. package/dist/postgres-schema-COGZ1ark.mjs.map +0 -1
  267. package/dist/render-typescript-KMgosran.mjs.map +0 -1
  268. package/dist/types.d.mts.map +0 -1
  269. package/src/core/postgres-enum-type-schema.ts +0 -17
@@ -0,0 +1,269 @@
1
+ import type { ContractToSchemaIROptions } from '@prisma-next/family-sql/control';
2
+ import { contractNamespaceToSchemaIR } from '@prisma-next/family-sql/control';
3
+ import type { SchemaNodeRef } from '@prisma-next/framework-components/control';
4
+ import { UNBOUND_NAMESPACE_ID } from '@prisma-next/framework-components/ir';
5
+ import {
6
+ PrimaryKey,
7
+ RelationalSchemaNodeKind,
8
+ SqlForeignKeyIR,
9
+ SqlIndexIR,
10
+ SqlUniqueIR,
11
+ } from '@prisma-next/sql-schema-ir/types';
12
+ import { ifDefined } from '@prisma-next/utils/defined';
13
+ import type { PostgresRlsPolicy } from '../postgres-rls-policy';
14
+ import type { PostgresContract } from '../postgres-schema';
15
+ import { isPostgresSchema } from '../postgres-schema';
16
+ import { PostgresDatabaseSchemaNode } from '../schema-ir/postgres-database-schema-node';
17
+ import { PostgresNamespaceSchemaNode } from '../schema-ir/postgres-namespace-schema-node';
18
+ import { PostgresNativeEnumSchemaNode } from '../schema-ir/postgres-native-enum-schema-node';
19
+ import { PostgresPolicySchemaNode } from '../schema-ir/postgres-policy-schema-node';
20
+ import { PostgresRoleSchemaNode } from '../schema-ir/postgres-role-schema-node';
21
+ import { PostgresTableSchemaNode } from '../schema-ir/postgres-table-schema-node';
22
+ import { PostgresSchemaNodeKind } from '../schema-ir/schema-node-kinds';
23
+ import { resolveDdlSchemaForNamespaceStorage } from './resolve-ddl-schema';
24
+
25
+ /** The database root's fixed sentinel id (`PostgresDatabaseSchemaNode#id`). */
26
+ function databaseStep(): { readonly nodeKind: string; readonly id: string } {
27
+ return { nodeKind: PostgresSchemaNodeKind.database, id: 'database' };
28
+ }
29
+
30
+ function tableDependsOn(namespaceId: string, tableName: string): SchemaNodeRef {
31
+ return [
32
+ databaseStep(),
33
+ { nodeKind: PostgresSchemaNodeKind.namespace, id: namespaceId },
34
+ { nodeKind: PostgresSchemaNodeKind.table, id: tableName },
35
+ ];
36
+ }
37
+
38
+ function roleDependsOn(role: string): SchemaNodeRef {
39
+ return [databaseStep(), { nodeKind: PostgresSchemaNodeKind.role, id: role }];
40
+ }
41
+
42
+ /**
43
+ * The chains from a table-child object (foreign key, index, unique, primary
44
+ * key) to each of the own columns it is built on, in the Postgres tree.
45
+ * Dropping a covered column auto-drops the object, so the object's drop must
46
+ * precede the column's; the graph derives that direction from these edges.
47
+ */
48
+ function columnDependsOn(
49
+ namespaceId: string,
50
+ tableName: string,
51
+ columns: readonly string[],
52
+ ): readonly SchemaNodeRef[] {
53
+ return columns.map((column) => [
54
+ ...tableDependsOn(namespaceId, tableName),
55
+ { nodeKind: RelationalSchemaNodeKind.column, id: `column:${column}` },
56
+ ]);
57
+ }
58
+
59
+ function toPolicyNode(policy: PostgresRlsPolicy, namespaceId: string): PostgresPolicySchemaNode {
60
+ return new PostgresPolicySchemaNode({
61
+ name: policy.name,
62
+ prefix: policy.prefix,
63
+ tableName: policy.tableName,
64
+ namespaceId,
65
+ operation: policy.operation,
66
+ roles: [...policy.roles],
67
+ ...ifDefined('using', policy.using),
68
+ ...ifDefined('withCheck', policy.withCheck),
69
+ permissive: policy.permissive,
70
+ dependsOn: [tableDependsOn(namespaceId, policy.tableName), ...policy.roles.map(roleDependsOn)],
71
+ });
72
+ }
73
+
74
+ /**
75
+ * Projects a Postgres contract into the expected schema-diff tree: a
76
+ * `PostgresDatabaseSchemaNode` root holding one `PostgresNamespaceSchemaNode`
77
+ * per Postgres namespace, each holding its `PostgresTableSchemaNode`s with
78
+ * their `PostgresPolicySchemaNode`s, plus the database roles on the root.
79
+ *
80
+ * Not a duplicate of the family's `contractToSchemaIR`: that builds a flat,
81
+ * single `{ tables }` map (and throws on cross-namespace name collisions, with
82
+ * no RLS/role concept) for SQLite's single-schema world. This is the
83
+ * Postgres-specific *tree* shape — multi-schema, RLS-policy-aware, role-aware.
84
+ * It reuses the family's per-namespace table conversion (`contractNamespaceToSchemaIR`)
85
+ * for column/FK/index building and only adds the Postgres tree/policy/role shape.
86
+ *
87
+ * Tables are grouped by their owning namespace (resolved DDL schema name) so
88
+ * the tree mirrors Postgres's object hierarchy. The DDL schema name is
89
+ * resolved once per namespace.
90
+ *
91
+ * A policy that references a table absent from its namespace is a malformed
92
+ * contract — the loop throws rather than fabricating a stub table.
93
+ */
94
+ export function contractToPostgresDatabaseSchemaNode(
95
+ contract: PostgresContract | null,
96
+ options: ContractToSchemaIROptions,
97
+ ): PostgresDatabaseSchemaNode {
98
+ if (contract === null) {
99
+ return new PostgresDatabaseSchemaNode({
100
+ namespaces: {},
101
+ roles: [],
102
+ existingSchemas: [],
103
+ pgVersion: '',
104
+ });
105
+ }
106
+
107
+ const namespaces: Record<string, PostgresNamespaceSchemaNode> = {};
108
+ const roles: PostgresRoleSchemaNode[] = [];
109
+ const ownedSchemas: string[] = [];
110
+
111
+ for (const ns of Object.values(contract.storage.namespaces)) {
112
+ if (!isPostgresSchema(ns)) continue;
113
+
114
+ // Role entries are root-level diff subjects: they hoist to the database
115
+ // root from every slot and never count toward whether a namespace
116
+ // materializes a schema node.
117
+ for (const role of Object.values(ns.role)) {
118
+ roles.push(new PostgresRoleSchemaNode({ name: role.name, namespaceId: role.namespaceId }));
119
+ }
120
+
121
+ // The unbound slot resolves its DDL schema to 'public', so it
122
+ // materializes a schema node exactly when it has non-role content — a
123
+ // late-binding contract keeps today's behavior (the slot carries the
124
+ // tables), while a roles-only unbound slot alongside named namespaces
125
+ // contributes only root roles and no node (which would otherwise be a
126
+ // spurious empty 'public' node, clobbering a real bound 'public'
127
+ // namespace's node keyed by the same resolved schema name).
128
+ if (ns.id === UNBOUND_NAMESPACE_ID) {
129
+ const hasNonRoleContent = Object.entries(ns.entries).some(
130
+ ([entriesKey, slot]) => entriesKey !== 'role' && Object.keys(slot).length > 0,
131
+ );
132
+ if (!hasNonRoleContent) continue;
133
+ }
134
+
135
+ const ddlSchema = resolveDdlSchemaForNamespaceStorage(contract.storage, ns.id);
136
+ ownedSchemas.push(ddlSchema);
137
+
138
+ // Convert only THIS namespace's tables (passing the full storage for
139
+ // type/value-set/enum resolution that spans namespaces), so the same table
140
+ // name can exist in two schemas without colliding in a bare-keyed record.
141
+ const sqlTables = contractNamespaceToSchemaIR(contract.storage, ns.id, options).tables;
142
+
143
+ const policiesByTable = new Map<string, PostgresPolicySchemaNode[]>();
144
+ for (const policy of Object.values(ns.policy)) {
145
+ const list = policiesByTable.get(policy.tableName) ?? [];
146
+ list.push(toPolicyNode(policy, ddlSchema));
147
+ policiesByTable.set(policy.tableName, list);
148
+ }
149
+
150
+ const tables: Record<string, PostgresTableSchemaNode> = {};
151
+ for (const tableName of Object.keys(ns.table)) {
152
+ const sqlTable = sqlTables[tableName];
153
+ if (sqlTable === undefined) continue;
154
+ // The family conversion stamps `referencedSchema` only for bound FK
155
+ // targets; an absent value means the FK targets the unbound namespace.
156
+ // Postgres restores its own coordinate for that slot (the unbound
157
+ // singleton's id) so the raw coordinate keeps qualifying REFERENCES
158
+ // clauses, and resolves the real live DDL schema — introspected FKs
159
+ // already carry the live schema, so this is what lets an expected FK
160
+ // pair (by diff-node id) with its introspected counterpart.
161
+ const foreignKeys = sqlTable.foreignKeys.map((fk) => {
162
+ const resolvedReferencedNamespace = resolveDdlSchemaForNamespaceStorage(
163
+ contract.storage,
164
+ fk.referencedSchema ?? UNBOUND_NAMESPACE_ID,
165
+ );
166
+ return new SqlForeignKeyIR({
167
+ columns: fk.columns,
168
+ referencedTable: fk.referencedTable,
169
+ referencedColumns: fk.referencedColumns,
170
+ referencedSchema: fk.referencedSchema ?? UNBOUND_NAMESPACE_ID,
171
+ ...ifDefined('name', fk.name),
172
+ ...ifDefined('onDelete', fk.onDelete),
173
+ ...ifDefined('onUpdate', fk.onUpdate),
174
+ ...ifDefined('annotations', fk.annotations),
175
+ resolvedReferencedNamespace,
176
+ dependsOn: [
177
+ tableDependsOn(resolvedReferencedNamespace, fk.referencedTable),
178
+ ...columnDependsOn(ddlSchema, tableName, fk.columns),
179
+ ],
180
+ });
181
+ });
182
+ // The family stamped these children's own-column `dependsOn` with the
183
+ // flat (single-schema) chain; the Postgres tree nests them under a
184
+ // namespace, so re-stamp with the multi-schema chain that matches this
185
+ // tree's paths. Every other field is carried through unchanged.
186
+ const uniques = sqlTable.uniques.map(
187
+ (u) =>
188
+ new SqlUniqueIR({
189
+ columns: u.columns,
190
+ ...ifDefined('name', u.name),
191
+ ...ifDefined('annotations', u.annotations),
192
+ dependsOn: columnDependsOn(ddlSchema, tableName, u.columns),
193
+ }),
194
+ );
195
+ const indexes = sqlTable.indexes.map(
196
+ (i) =>
197
+ new SqlIndexIR({
198
+ columns: i.columns,
199
+ unique: i.unique,
200
+ ...ifDefined('name', i.name),
201
+ ...ifDefined('type', i.type),
202
+ ...ifDefined('options', i.options),
203
+ ...ifDefined('annotations', i.annotations),
204
+ dependsOn: columnDependsOn(ddlSchema, tableName, i.columns),
205
+ }),
206
+ );
207
+ const primaryKey =
208
+ sqlTable.primaryKey !== undefined
209
+ ? new PrimaryKey({
210
+ columns: sqlTable.primaryKey.columns,
211
+ ...ifDefined('name', sqlTable.primaryKey.name),
212
+ dependsOn: columnDependsOn(ddlSchema, tableName, sqlTable.primaryKey.columns),
213
+ })
214
+ : undefined;
215
+ tables[tableName] = new PostgresTableSchemaNode({
216
+ name: sqlTable.name,
217
+ columns: sqlTable.columns,
218
+ foreignKeys,
219
+ uniques,
220
+ indexes,
221
+ ...ifDefined('primaryKey', primaryKey),
222
+ ...ifDefined('annotations', sqlTable.annotations),
223
+ ...ifDefined('checks', sqlTable.checks),
224
+ policies: policiesByTable.get(tableName) ?? [],
225
+ // Marker-driven, never derived from the policy set: the `rls` entry
226
+ // is the single authored source of enablement.
227
+ rlsEnabled: Object.hasOwn(ns.rls, tableName),
228
+ });
229
+ }
230
+
231
+ for (const [tableName, tablePolicies] of policiesByTable) {
232
+ if (!(tableName in tables)) {
233
+ const policyName = tablePolicies[0]?.name ?? '(unknown)';
234
+ throw new Error(
235
+ `contract-to-postgres-database-schema-node: policy "${policyName}" references table "${tableName}" not present in namespace "${ddlSchema}"`,
236
+ );
237
+ }
238
+ if (!Object.hasOwn(ns.rls, tableName)) {
239
+ const policyPrefix = tablePolicies[0]?.prefix ?? '(unknown)';
240
+ throw new Error(
241
+ `contract-to-postgres-database-schema-node: policy "${policyPrefix}" targets table "${tableName}" in namespace "${ddlSchema}", which is not RLS-controlled. Mark the model with @@rls (entries.rls["${tableName}"]) or remove the policy.`,
242
+ );
243
+ }
244
+ }
245
+
246
+ const nativeEnums = Object.values(ns.entries.native_enum ?? {}).map(
247
+ (entity) =>
248
+ new PostgresNativeEnumSchemaNode({
249
+ typeName: entity.typeName,
250
+ namespaceId: ddlSchema,
251
+ members: entity.members,
252
+ ...ifDefined('control', entity.control),
253
+ }),
254
+ );
255
+
256
+ namespaces[ddlSchema] = new PostgresNamespaceSchemaNode({
257
+ schemaName: ddlSchema,
258
+ tables,
259
+ nativeEnums,
260
+ });
261
+ }
262
+
263
+ return new PostgresDatabaseSchemaNode({
264
+ namespaces,
265
+ roles,
266
+ existingSchemas: ownedSchemas,
267
+ pgVersion: '',
268
+ });
269
+ }
@@ -1,10 +1,23 @@
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';
1
+ import type { Contract, ControlPolicy } from '@prisma-next/contract/types';
2
+ import type {
3
+ ControlPolicySubject,
4
+ SqlPlannerConflict,
5
+ SuppressionRecord,
6
+ } from '@prisma-next/family-sql/control';
7
+ import type { SchemaDiffIssue } from '@prisma-next/framework-components/control';
8
+ import { issueOutcome } from '@prisma-next/framework-components/control';
4
9
  import { entityAt, UNBOUND_NAMESPACE_ID } from '@prisma-next/framework-components/ir';
5
10
  import type { SqlStorage, StorageTable } from '@prisma-next/sql-contract/types';
11
+ import { blindCast } from '@prisma-next/utils/casts';
6
12
  import { ifDefined } from '@prisma-next/utils/defined';
13
+ import { type PostgresRole, ROLE_DEFAULT_CONTROL_POLICY } from '../postgres-role';
7
14
  import { isPostgresSchema } from '../postgres-schema';
15
+ import { postgresNodeStorageCoordinate } from '../schema-ir/node-storage-coordinate';
16
+ import type { PostgresNamespaceSchemaNode } from '../schema-ir/postgres-namespace-schema-node';
17
+ import { PostgresTableSchemaNode } from '../schema-ir/postgres-table-schema-node';
18
+ import type { SqlSchemaDiffNode } from '../schema-ir/schema-node-kinds';
19
+ import { PostgresSchemaNodeKind } from '../schema-ir/schema-node-kinds';
20
+ import { issueColumnName } from './issue-planner';
8
21
  import type { PostgresOpFactoryCall } from './op-factory-call';
9
22
 
10
23
  /**
@@ -17,11 +30,16 @@ import type { PostgresOpFactoryCall } from './op-factory-call';
17
30
  * and is fail-closed. Any call not listed here — including future or
18
31
  * extension-contributed factories — is treated as NOT object-creation, so it
19
32
  * is suppressed under `tolerated` rather than permissively emitted.
33
+ *
34
+ * Lists the creation factories that actually reach the call-side resolver
35
+ * (`resolvePostgresCallControlPolicySubject`) — today only RLS policy
36
+ * creation, from `planPostgresSchemaDiff`. Every other whole-object create
37
+ * (table, schema, native enum, and RLS enablement) is constructed inside
38
+ * `planIssues` and graded on the node side by
39
+ * `resolvePostgresNodeIssueCreationFactoryName`, so none of them are listed
40
+ * here.
20
41
  */
21
- const OBJECT_CREATION_FACTORIES: ReadonlySet<string> = new Set<string>([
22
- 'createTable',
23
- 'createSchema',
24
- ]);
42
+ const OBJECT_CREATION_FACTORIES: ReadonlySet<string> = new Set<string>(['createRlsPolicy']);
25
43
 
26
44
  function createsNewTopLevelObject(call: PostgresOpFactoryCall): boolean {
27
45
  return OBJECT_CREATION_FACTORIES.has(call.factoryName);
@@ -32,18 +50,25 @@ function ddlSchemaNameForNamespace(contract: Contract<SqlStorage>, namespaceId:
32
50
  return isPostgresSchema(namespace) ? namespace.ddlSchemaName(contract.storage) : namespaceId;
33
51
  }
34
52
 
35
- function resolveNamespaceIdForTable(
53
+ /**
54
+ * Resolve the namespace a declared storage entity lives in by walking every
55
+ * namespace for one whose `entries` map actually contains the coordinate —
56
+ * a table by its name, a native enum by its physical type name (see
57
+ * {@link postgresNodeStorageCoordinate}). When `ddlSchemaName` is given, the
58
+ * match must also land in that DDL schema (disambiguates same-named entities
59
+ * declared under different namespaces); when omitted, the first namespace
60
+ * that declares the entity wins. Falls back to {@link UNBOUND_NAMESPACE_ID}
61
+ * when no namespace declares it — e.g. an extra/dropped entity the contract
62
+ * doesn't claim at all.
63
+ */
64
+ function resolveNamespaceIdForEntity(
36
65
  contract: Contract<SqlStorage>,
37
- tableName: string,
66
+ coordinate: { readonly entityKind: string; readonly entityName: string },
38
67
  ddlSchemaName: string | undefined,
39
68
  ): string {
40
69
  for (const namespaceId of Object.keys(contract.storage.namespaces)) {
41
- const table = entityAt<StorageTable>(contract.storage, {
42
- namespaceId,
43
- entityKind: 'table',
44
- entityName: tableName,
45
- });
46
- if (!table) continue;
70
+ const entity = entityAt(contract.storage, { namespaceId, ...coordinate });
71
+ if (!entity) continue;
47
72
  if (
48
73
  ddlSchemaName === undefined ||
49
74
  ddlSchemaNameForNamespace(contract, namespaceId) === ddlSchemaName
@@ -54,7 +79,19 @@ function resolveNamespaceIdForTable(
54
79
  return UNBOUND_NAMESPACE_ID;
55
80
  }
56
81
 
57
- function resolveNamespaceIdForDdlSchema(
82
+ function resolveNamespaceIdForTable(
83
+ contract: Contract<SqlStorage>,
84
+ tableName: string,
85
+ ddlSchemaName: string | undefined,
86
+ ): string {
87
+ return resolveNamespaceIdForEntity(
88
+ contract,
89
+ { entityKind: 'table', entityName: tableName },
90
+ ddlSchemaName,
91
+ );
92
+ }
93
+
94
+ export function resolveNamespaceIdForDdlSchema(
58
95
  contract: Contract<SqlStorage>,
59
96
  ddlSchemaName: string,
60
97
  ): string {
@@ -84,16 +121,60 @@ function postgresCallFields(call: PostgresOpFactoryCall): PostgresCallFields {
84
121
  };
85
122
  }
86
123
 
87
- export function formatPostgresControlPolicySubjectLabel(
88
- factoryName: string,
124
+ function formatSuppressionSubjectLabel(
89
125
  subject: ControlPolicySubject | undefined,
90
126
  contract: Contract<SqlStorage>,
91
127
  ): string {
92
- if (subject?.table) {
93
- const ddlSchema = ddlSchemaNameForNamespace(contract, subject.namespaceId);
94
- return `${factoryName}(${ddlSchema}.${subject.table})`;
128
+ if (subject === undefined) return 'unknown';
129
+ const ddlSchema = ddlSchemaNameForNamespace(contract, subject.namespaceId);
130
+ if (subject.entityKind !== undefined && subject.entityName !== undefined) {
131
+ return `${subject.entityKind} "${ddlSchema}.${subject.entityName}"`;
132
+ }
133
+ return `namespace "${ddlSchema}"`;
134
+ }
135
+
136
+ function postgresSuppressionSummary(
137
+ subjectLabel: string,
138
+ subject: ControlPolicySubject | undefined,
139
+ policy: string,
140
+ ): string {
141
+ const namespace = subject?.namespaceId ?? 'unknown';
142
+ const declared = subject?.explicitNodeControlPolicy;
143
+ if (policy === 'external' && declared === 'managed') {
144
+ return `control policy suppressed: ${subjectLabel} — namespace '${namespace}' has effective control 'external' but declared 'managed'`;
95
145
  }
96
- return factoryName;
146
+ const declaredSuffix = declared ? ` but declared '${declared}'` : '';
147
+ return `control policy suppressed: ${subjectLabel} — namespace '${namespace}' has effective control '${policy}'${declaredSuffix}`;
148
+ }
149
+
150
+ /**
151
+ * Render one family {@link SuppressionRecord} into a target `SqlPlannerConflict`.
152
+ * The family decides *that* a subject is suppressed and hands over the raw
153
+ * coordinate + policy; the label, message, and location are rendered here,
154
+ * driven entirely by the subject's own `(entityKind, entityName)` coordinate
155
+ * — no target-owned table-vs-enum vocabulary.
156
+ */
157
+ export function renderPostgresSuppression(
158
+ record: SuppressionRecord,
159
+ contract: Contract<SqlStorage>,
160
+ ): SqlPlannerConflict {
161
+ const subject = record.subject;
162
+ const subjectLabel = formatSuppressionSubjectLabel(subject, contract);
163
+ return {
164
+ kind: 'controlPolicySuppressedCall',
165
+ summary: postgresSuppressionSummary(subjectLabel, subject, record.policy),
166
+ location: {
167
+ ...ifDefined('namespaceId', subject?.namespaceId),
168
+ ...ifDefined('entityKind', subject?.entityKind),
169
+ ...ifDefined('entityName', subject?.entityName),
170
+ ...ifDefined('column', subject?.column),
171
+ },
172
+ meta: {
173
+ controlPolicy: record.policy,
174
+ ...ifDefined('factoryName', record.factoryName),
175
+ ...ifDefined('declaredControlPolicy', subject?.explicitNodeControlPolicy),
176
+ },
177
+ };
97
178
  }
98
179
 
99
180
  export function resolvePostgresCallControlPolicySubject(
@@ -123,9 +204,10 @@ export function resolvePostgresCallControlPolicySubject(
123
204
  })?.control;
124
205
  return {
125
206
  namespaceId,
126
- ...ifDefined('explicitNodeControlPolicy', tableControlPolicy),
127
- table: callFields.tableName,
207
+ entityKind: 'table',
208
+ entityName: callFields.tableName,
128
209
  ...ifDefined('column', callFields.columnName),
210
+ ...ifDefined('explicitNodeControlPolicy', tableControlPolicy),
129
211
  createsNewObject,
130
212
  };
131
213
  }
@@ -141,64 +223,135 @@ export function resolvePostgresCallControlPolicySubject(
141
223
  }
142
224
 
143
225
  /**
144
- * Issue kinds that describe the absence of a whole, top-level Postgres
145
- * object the same kinds `createsNewTopLevelObject` recognises for calls.
146
- * Used by {@link resolvePostgresIssueCreationFactoryName} to decide whether
147
- * a `tolerated` subject permits the issue to flow into the planner
226
+ * Node kinds whose *absence* is the creation of a whole, top-level Postgres
227
+ * object: a namespace, a table, or a native enum. Used by
228
+ * {@link resolvePostgresNodeIssueCreationFactoryName} to decide whether a
229
+ * `tolerated` subject permits the issue to flow into the planner
148
230
  * (create-if-absent) and to seed the suppressed-subject warning's
149
- * `factoryName` when the planner is skipped.
231
+ * `factoryName` when the planner is skipped. RLS policy creation is not
232
+ * listed here — policy issues never reach this issue-based partition (they
233
+ * are routed to `planPostgresSchemaDiff` and gated via the call-based
234
+ * {@link resolvePostgresCallControlPolicySubject} instead).
150
235
  */
151
- const POSTGRES_ISSUE_CREATION_FACTORY: Readonly<Record<string, string>> = Object.freeze({
152
- missing_schema: 'createSchema',
153
- missing_table: 'createTable',
236
+ const POSTGRES_NODE_CREATION_FACTORY: Readonly<Record<string, string>> = Object.freeze({
237
+ [PostgresSchemaNodeKind.namespace]: 'createSchema',
238
+ [PostgresSchemaNodeKind.table]: 'createTable',
239
+ [PostgresSchemaNodeKind.nativeEnum]: 'createNativeEnumType',
154
240
  });
155
241
 
156
- export function resolvePostgresIssueCreationFactoryName(issue: SchemaIssue): string | undefined {
157
- return POSTGRES_ISSUE_CREATION_FACTORY[issue.kind];
242
+ /**
243
+ * A table `not-equal` issue whose `rlsEnabled` flips OFF→ON (expected on,
244
+ * actual off) is enablement toward `ENABLE ROW LEVEL SECURITY`. It is
245
+ * creation-class on the node side because `isEnablementCreationIssue` and
246
+ * {@link resolvePostgresNodeIssueCreationFactoryName} treat this OFF→ON delta
247
+ * as a creation: enabling RLS establishes the fail-closed guard the declared
248
+ * policy set attaches to, the same grant `tolerated` extends to creating the
249
+ * policies themselves. The opposite direction (`DISABLE`) is a modification
250
+ * and stays managed-only. Keying on the actual delta (not just the expected
251
+ * bit) keeps this correct if a second table attribute ever joins
252
+ * `isEqualTo`: a not-equal with no `rlsEnabled` delta is not enablement, so
253
+ * it is not admitted as creation-class here.
254
+ */
255
+ function isEnablementCreationIssue(issue: SchemaDiffIssue<SqlSchemaDiffNode>): boolean {
256
+ if (issueOutcome(issue) !== 'not-equal') return false;
257
+ const { expected, actual } = issue;
258
+ return (
259
+ expected !== undefined &&
260
+ actual !== undefined &&
261
+ PostgresTableSchemaNode.is(expected) &&
262
+ PostgresTableSchemaNode.is(actual) &&
263
+ expected.rlsEnabled === true &&
264
+ actual.rlsEnabled === false
265
+ );
266
+ }
267
+
268
+ export function resolvePostgresNodeIssueCreationFactoryName(
269
+ issue: SchemaDiffIssue<SqlSchemaDiffNode>,
270
+ ): string | undefined {
271
+ if (isEnablementCreationIssue(issue)) {
272
+ return 'enableRowLevelSecurity';
273
+ }
274
+ if (issueOutcome(issue) !== 'not-found') return undefined;
275
+ const node = issue.expected ?? issue.actual;
276
+ if (node === undefined) return undefined;
277
+ return POSTGRES_NODE_CREATION_FACTORY[node.nodeKind];
158
278
  }
159
279
 
160
280
  /**
161
- * Resolve the control-policy subject coordinate for a single
162
- * {@link SchemaIssue}. Mirrors the resolution `resolvePostgresCallControlPolicySubject`
163
- * performs for a generated DDL call, but works *off the issue* — so the
164
- * planner can partition issues by effective policy before the diff engine
165
- * runs. `createsNewObject` is derived from the issue's kind: schema/table/
166
- * type-missing issues describe a brand-new top-level object; everything else
167
- * touches an existing object.
168
- *
169
- * An `extra_table` issue carries no contract namespace coordinate (the table
170
- * isn't in any contract namespace), so the subject's `namespaceId` falls
171
- * back to {@link UNBOUND_NAMESPACE_ID}; the call-side resolver does the same
172
- * for the `DropTableCall` it produces.
281
+ * Resolves the control-policy subject for a node-typed {@link SchemaDiffIssue}
282
+ * (the issue-side mirror of `resolvePostgresCallControlPolicySubject`).
283
+ * Storage entities resolve off their node coordinate; a sub-entity issue
284
+ * resolves off the enclosing table read from the issue path; an unclaimed
285
+ * extra falls back to the unbound coordinate. A role always resolves
286
+ * `'external'` roles are referenced, never owned.
173
287
  */
174
- export function resolvePostgresIssueControlPolicySubject(
175
- issue: SchemaIssue,
288
+ export function resolvePostgresNodeIssueControlPolicySubject(
289
+ issue: SchemaDiffIssue<SqlSchemaDiffNode>,
176
290
  contract: Contract<SqlStorage>,
177
291
  ): ControlPolicySubject | undefined {
178
- const createsNewObject = POSTGRES_ISSUE_CREATION_FACTORY[issue.kind] !== undefined;
292
+ const node = issue.expected ?? issue.actual;
293
+ if (node === undefined) return undefined;
294
+
295
+ if (node.nodeKind === PostgresSchemaNodeKind.namespace) {
296
+ const namespaceNode = blindCast<
297
+ PostgresNamespaceSchemaNode,
298
+ 'a postgres-namespace diff node is always a PostgresNamespaceSchemaNode'
299
+ >(node);
300
+ return {
301
+ namespaceId: resolveNamespaceIdForDdlSchema(contract, namespaceNode.schemaName),
302
+ createsNewObject: issueOutcome(issue) === 'not-found',
303
+ };
304
+ }
179
305
 
180
- if (issue.kind === 'missing_schema' && issue.namespaceId) {
181
- return { namespaceId: issue.namespaceId, createsNewObject };
306
+ if (node.nodeKind === PostgresSchemaNodeKind.role) {
307
+ const roleName = issue.path[1];
308
+ const roleEntity =
309
+ roleName === undefined
310
+ ? undefined
311
+ : entityAt<PostgresRole>(contract.storage, {
312
+ namespaceId: UNBOUND_NAMESPACE_ID,
313
+ entityKind: 'role',
314
+ entityName: roleName,
315
+ });
316
+ return {
317
+ namespaceId: UNBOUND_NAMESPACE_ID,
318
+ explicitNodeControlPolicy: roleEntity?.control ?? ROLE_DEFAULT_CONTROL_POLICY,
319
+ createsNewObject: false,
320
+ };
182
321
  }
183
322
 
184
- if ('table' in issue && issue.table) {
185
- const namespaceId =
186
- 'namespaceId' in issue && issue.namespaceId
187
- ? issue.namespaceId
188
- : resolveNamespaceIdForTable(contract, issue.table, undefined);
189
- const table = entityAt<StorageTable>(contract.storage, {
323
+ const coordinate = postgresNodeStorageCoordinate(node);
324
+ if (coordinate !== undefined) {
325
+ const namespaceId = resolveNamespaceIdForEntity(contract, coordinate, issue.path[1]);
326
+ const entityControl = entityAt<{ readonly control?: ControlPolicy }>(contract.storage, {
190
327
  namespaceId,
191
- entityKind: 'table',
192
- entityName: issue.table,
193
- });
328
+ ...coordinate,
329
+ })?.control;
194
330
  return {
195
331
  namespaceId,
196
- ...ifDefined('explicitNodeControlPolicy', table?.control),
197
- table: issue.table,
198
- ...ifDefined('column', 'column' in issue ? issue.column : undefined),
199
- createsNewObject,
332
+ ...coordinate,
333
+ ...ifDefined('column', issueColumnName(issue)),
334
+ ...ifDefined('explicitNodeControlPolicy', entityControl),
335
+ createsNewObject: resolvePostgresNodeIssueCreationFactoryName(issue) !== undefined,
200
336
  };
201
337
  }
202
338
 
203
- return undefined;
339
+ const tableName = issue.path[2];
340
+ if (tableName === undefined) return undefined;
341
+ const ddlSchemaName = issue.path[1];
342
+ const namespaceId = resolveNamespaceIdForTable(contract, tableName, ddlSchemaName);
343
+ const table = entityAt<StorageTable>(contract.storage, {
344
+ namespaceId,
345
+ entityKind: 'table',
346
+ entityName: tableName,
347
+ });
348
+
349
+ return {
350
+ namespaceId,
351
+ entityKind: 'table',
352
+ entityName: tableName,
353
+ ...ifDefined('column', issueColumnName(issue)),
354
+ ...ifDefined('explicitNodeControlPolicy', table?.control),
355
+ createsNewObject: resolvePostgresNodeIssueCreationFactoryName(issue) !== undefined,
356
+ };
204
357
  }