@prisma-next/target-postgres 0.14.0-dev.5 → 0.14.0-dev.51

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 (238) hide show
  1. package/dist/authoring-DzHOdWTl.mjs +364 -0
  2. package/dist/authoring-DzHOdWTl.mjs.map +1 -0
  3. package/dist/canonicalize-Bn3BM1Cn.mjs +46 -0
  4. package/dist/canonicalize-Bn3BM1Cn.mjs.map +1 -0
  5. package/dist/canonicalize-CAbXtVtB.d.mts +37 -0
  6. package/dist/canonicalize-CAbXtVtB.d.mts.map +1 -0
  7. package/dist/{codec-ids-CnXu9Qy3.d.mts → codec-ids-BzrFF-I4.d.mts} +3 -2
  8. package/dist/codec-ids-BzrFF-I4.d.mts.map +1 -0
  9. package/dist/{codec-ids-BvytN2P8.mjs → codec-ids-C_-Hj6bL.mjs} +3 -2
  10. package/dist/codec-ids-C_-Hj6bL.mjs.map +1 -0
  11. package/dist/codec-ids.d.mts +2 -2
  12. package/dist/codec-ids.mjs +2 -2
  13. package/dist/{codec-types-DHCkwPKE.d.mts → codec-types-BDVmvKAW.d.mts} +3 -2
  14. package/dist/codec-types-BDVmvKAW.d.mts.map +1 -0
  15. package/dist/codec-types.d.mts +1 -1
  16. package/dist/{codecs--0A5_4Bq.d.mts → codecs-Dk2sxGLv.d.mts} +77 -4
  17. package/dist/codecs-Dk2sxGLv.d.mts.map +1 -0
  18. package/dist/codecs.d.mts +1 -1
  19. package/dist/codecs.mjs +117 -2
  20. package/dist/codecs.mjs.map +1 -1
  21. package/dist/contract-free.d.mts +8 -3
  22. package/dist/contract-free.d.mts.map +1 -1
  23. package/dist/contract-free.mjs +4 -4
  24. package/dist/control-policy-DUFgy9jO.mjs +1103 -0
  25. package/dist/control-policy-DUFgy9jO.mjs.map +1 -0
  26. package/dist/control.d.mts +2 -2
  27. package/dist/control.d.mts.map +1 -1
  28. package/dist/control.mjs +757 -35
  29. package/dist/control.mjs.map +1 -1
  30. package/dist/data-transform-BOWpliq8.mjs.map +1 -1
  31. package/dist/{data-transform-DDgWdB5o.d.mts → data-transform-DPzwBMhW.d.mts} +3 -3
  32. package/dist/{data-transform-DDgWdB5o.d.mts.map → data-transform-DPzwBMhW.d.mts.map} +1 -1
  33. package/dist/data-transform.d.mts +1 -1
  34. package/dist/{ddl-QDyOSeLc.mjs → ddl-ujxH2A7r.mjs} +63 -6
  35. package/dist/ddl-ujxH2A7r.mjs.map +1 -0
  36. package/dist/ddl.d.mts +3 -2
  37. package/dist/ddl.mjs +2 -2
  38. package/dist/default-normalizer.d.mts +1 -1
  39. package/dist/default-normalizer.d.mts.map +1 -1
  40. package/dist/default-normalizer.mjs +247 -1
  41. package/dist/default-normalizer.mjs.map +1 -0
  42. package/dist/descriptor-meta-CuGDsrG4.mjs +19 -0
  43. package/dist/descriptor-meta-CuGDsrG4.mjs.map +1 -0
  44. package/dist/descriptor-meta-runtime-UwYdV8fD.mjs +12 -0
  45. package/dist/descriptor-meta-runtime-UwYdV8fD.mjs.map +1 -0
  46. package/dist/diff-database-schema-BmSmWcaY.mjs +313 -0
  47. package/dist/diff-database-schema-BmSmWcaY.mjs.map +1 -0
  48. package/dist/diff-database-schema.d.mts +46 -0
  49. package/dist/diff-database-schema.d.mts.map +1 -0
  50. package/dist/diff-database-schema.mjs +2 -0
  51. package/dist/issue-planner.d.mts +35 -11
  52. package/dist/issue-planner.d.mts.map +1 -1
  53. package/dist/issue-planner.mjs +2 -2
  54. package/dist/migration.d.mts +5 -5
  55. package/dist/migration.d.mts.map +1 -1
  56. package/dist/migration.mjs +4 -4
  57. package/dist/namespace-ids-xtp_ZY86.mjs +7 -0
  58. package/dist/namespace-ids-xtp_ZY86.mjs.map +1 -0
  59. package/dist/native-type-normalizer.mjs +37 -1
  60. package/dist/native-type-normalizer.mjs.map +1 -0
  61. package/dist/nodes-CSkPX3rI.d.mts +120 -0
  62. package/dist/nodes-CSkPX3rI.d.mts.map +1 -0
  63. package/dist/{nodes-Bbhs2rwj.mjs → nodes-Dh__NS58.mjs} +56 -2
  64. package/dist/nodes-Dh__NS58.mjs.map +1 -0
  65. package/dist/{op-factory-call-D_p5vxwt.mjs → op-factory-call-BKRnMgrB.mjs} +205 -31
  66. package/dist/op-factory-call-BKRnMgrB.mjs.map +1 -0
  67. package/dist/{op-factory-call-DmQEc3XV.d.mts → op-factory-call-Dt-Jx5JS.d.mts} +46 -15
  68. package/dist/op-factory-call-Dt-Jx5JS.d.mts.map +1 -0
  69. package/dist/op-factory-call.d.mts +2 -2
  70. package/dist/op-factory-call.mjs +2 -2
  71. package/dist/pack.d.mts +115 -3
  72. package/dist/pack.d.mts.map +1 -1
  73. package/dist/pack.mjs +1 -1
  74. package/dist/planner-BMBRPzsv.mjs +367 -0
  75. package/dist/planner-BMBRPzsv.mjs.map +1 -0
  76. package/dist/{planner-ddl-builders-B2wOwLqI.mjs → planner-ddl-builders-B8Nn6nHN.mjs} +12 -27
  77. package/dist/planner-ddl-builders-B8Nn6nHN.mjs.map +1 -0
  78. package/dist/planner-ddl-builders.d.mts +4 -6
  79. package/dist/planner-ddl-builders.d.mts.map +1 -1
  80. package/dist/planner-ddl-builders.mjs +2 -2
  81. package/dist/planner-identity-values-CJPha2Sz.mjs.map +1 -1
  82. package/dist/planner-identity-values.d.mts +8 -2
  83. package/dist/planner-identity-values.d.mts.map +1 -1
  84. package/dist/planner-identity-values.mjs +2 -2
  85. package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts → planner-produced-postgres-migration-C0a7I1-e.d.mts} +4 -4
  86. package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts.map → planner-produced-postgres-migration-C0a7I1-e.d.mts.map} +1 -1
  87. package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs → planner-produced-postgres-migration-Dp6IO_gj.mjs} +3 -3
  88. package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs.map → planner-produced-postgres-migration-Dp6IO_gj.mjs.map} +1 -1
  89. package/dist/planner-produced-postgres-migration.d.mts +1 -1
  90. package/dist/planner-produced-postgres-migration.mjs +1 -1
  91. package/dist/{planner-sql-checks-jqUUGyQR.mjs → planner-sql-checks-Cze3vMfe.mjs} +2 -2
  92. package/dist/{planner-sql-checks-jqUUGyQR.mjs.map → planner-sql-checks-Cze3vMfe.mjs.map} +1 -1
  93. package/dist/planner-sql-checks.d.mts +2 -2
  94. package/dist/planner-sql-checks.d.mts.map +1 -1
  95. package/dist/planner-sql-checks.mjs +1 -1
  96. package/dist/{planner-target-details-CIY6tLeo.d.mts → planner-target-details-HkP4RrRO.d.mts} +2 -2
  97. package/dist/planner-target-details-HkP4RrRO.d.mts.map +1 -0
  98. package/dist/planner-target-details.d.mts +1 -1
  99. package/dist/planner.d.mts +55 -17
  100. package/dist/planner.d.mts.map +1 -1
  101. package/dist/planner.mjs +3 -2
  102. package/dist/{postgres-contract-serializer-k3TAcPMY.mjs → postgres-contract-view-3xfUgjnC.mjs} +44 -41
  103. package/dist/postgres-contract-view-3xfUgjnC.mjs.map +1 -0
  104. package/dist/postgres-contract-view-C9j5cqP0.d.mts +34 -0
  105. package/dist/postgres-contract-view-C9j5cqP0.d.mts.map +1 -0
  106. package/dist/postgres-database-schema-node-C9bTuMrd.d.mts +190 -0
  107. package/dist/postgres-database-schema-node-C9bTuMrd.d.mts.map +1 -0
  108. package/dist/{postgres-migration-B5jKrXv3.mjs → postgres-migration-D0jwzYDT.mjs} +21 -2
  109. package/dist/postgres-migration-D0jwzYDT.mjs.map +1 -0
  110. package/dist/{postgres-migration-Y4YBJqkS.d.mts → postgres-migration-DFaxuRyI.d.mts} +20 -7
  111. package/dist/postgres-migration-DFaxuRyI.d.mts.map +1 -0
  112. package/dist/postgres-native-enum-xdiqmBf_.mjs +41 -0
  113. package/dist/postgres-native-enum-xdiqmBf_.mjs.map +1 -0
  114. package/dist/postgres-rls-policy--CSddvMR.d.mts +52 -0
  115. package/dist/postgres-rls-policy--CSddvMR.d.mts.map +1 -0
  116. package/dist/{postgres-schema-COGZ1ark.mjs → postgres-schema-CajYAwny.mjs} +135 -7
  117. package/dist/postgres-schema-CajYAwny.mjs.map +1 -0
  118. package/dist/postgres-schema-DqnhQRvR.d.mts +226 -0
  119. package/dist/postgres-schema-DqnhQRvR.d.mts.map +1 -0
  120. package/dist/postgres-table-schema-node-BgaSrxYN.mjs +249 -0
  121. package/dist/postgres-table-schema-node-BgaSrxYN.mjs.map +1 -0
  122. package/dist/render-ops.d.mts +1 -1
  123. package/dist/{render-typescript-KMgosran.mjs → render-typescript-7yqILcwr.mjs} +45 -16
  124. package/dist/render-typescript-7yqILcwr.mjs.map +1 -0
  125. package/dist/render-typescript.mjs +1 -1
  126. package/dist/resolve-ddl-schema-Bv1ZvjX8.mjs +22 -0
  127. package/dist/resolve-ddl-schema-Bv1ZvjX8.mjs.map +1 -0
  128. package/dist/rls-canonicalize.d.mts +2 -0
  129. package/dist/rls-canonicalize.mjs +2 -0
  130. package/dist/runtime.d.mts +6 -8
  131. package/dist/runtime.d.mts.map +1 -1
  132. package/dist/runtime.mjs +3 -3
  133. package/dist/runtime.mjs.map +1 -1
  134. package/dist/schema-ir-annotations.d.mts +15 -0
  135. package/dist/schema-ir-annotations.d.mts.map +1 -0
  136. package/dist/schema-ir-annotations.mjs +2 -0
  137. package/dist/schema-node-kinds-ClScchhi.mjs +76 -0
  138. package/dist/schema-node-kinds-ClScchhi.mjs.map +1 -0
  139. package/dist/types.d.mts +4 -140
  140. package/dist/types.mjs +3 -2
  141. package/package.json +23 -17
  142. package/src/contract-free/checks.ts +73 -0
  143. package/src/contract-free/ddl.ts +45 -1
  144. package/src/core/authoring.ts +287 -2
  145. package/src/core/codec-ids.ts +1 -0
  146. package/src/core/codec-type-map.ts +2 -0
  147. package/src/core/codecs.ts +141 -1
  148. package/src/core/ddl/nodes.ts +94 -2
  149. package/src/core/default-normalizer.ts +122 -2
  150. package/src/core/descriptor-meta.ts +6 -1
  151. package/src/core/entity-kinds.ts +30 -0
  152. package/src/core/migrations/bound-schema.ts +10 -0
  153. package/src/core/migrations/column-ddl-rendering.ts +117 -0
  154. package/src/core/migrations/contract-to-postgres-database-schema-node.ts +149 -0
  155. package/src/core/migrations/control-policy.ts +70 -47
  156. package/src/core/migrations/diff-database-schema.ts +280 -0
  157. package/src/core/migrations/issue-planner.ts +647 -652
  158. package/src/core/migrations/op-factory-call.ts +109 -21
  159. package/src/core/migrations/operations/columns.ts +24 -12
  160. package/src/core/migrations/operations/constraints.ts +1 -1
  161. package/src/core/migrations/operations/data-transform.ts +6 -0
  162. package/src/core/migrations/operations/dependencies.ts +2 -1
  163. package/src/core/migrations/operations/rls.ts +106 -0
  164. package/src/core/migrations/operations/shared.ts +0 -30
  165. package/src/core/migrations/planner-ddl-builders.ts +20 -58
  166. package/src/core/migrations/planner-identity-values.ts +1 -1
  167. package/src/core/migrations/planner-recipes.ts +31 -13
  168. package/src/core/migrations/planner-sql-checks.ts +1 -1
  169. package/src/core/migrations/planner-strategies.ts +248 -216
  170. package/src/core/migrations/planner-target-details.ts +3 -1
  171. package/src/core/migrations/planner.ts +297 -59
  172. package/src/core/migrations/postgres-migration.ts +34 -4
  173. package/src/core/migrations/render-typescript.ts +42 -15
  174. package/src/core/migrations/resolve-ddl-schema.ts +26 -0
  175. package/src/core/migrations/runner.ts +10 -16
  176. package/src/core/migrations/verify-postgres-namespaces.ts +47 -30
  177. package/src/core/namespace-ids.ts +3 -0
  178. package/src/core/postgres-contract-serializer.ts +41 -71
  179. package/src/core/postgres-contract-view.ts +48 -0
  180. package/src/core/postgres-native-enum.ts +51 -0
  181. package/src/core/postgres-rls-policy.ts +64 -0
  182. package/src/core/postgres-role.ts +37 -0
  183. package/src/core/postgres-schema-verifier.ts +10 -7
  184. package/src/core/postgres-schema.ts +62 -14
  185. package/src/core/postgres-validators.ts +27 -0
  186. package/src/core/psl-infer/infer-psl-contract.ts +1068 -0
  187. package/src/core/psl-infer/postgres-default-mapping.ts +16 -0
  188. package/src/core/psl-infer/postgres-type-map.ts +157 -0
  189. package/src/core/rls/canonicalize.ts +48 -0
  190. package/src/core/schema-ir/postgres-database-schema-node.ts +60 -0
  191. package/src/core/schema-ir/postgres-namespace-schema-node.ts +60 -0
  192. package/src/core/schema-ir/postgres-policy-schema-node.ts +90 -0
  193. package/src/core/schema-ir/postgres-role-schema-node.ts +63 -0
  194. package/src/core/schema-ir/postgres-table-schema-node.ts +118 -0
  195. package/src/core/schema-ir/schema-node-kinds.ts +106 -0
  196. package/src/exports/contract-free.ts +7 -1
  197. package/src/exports/control.ts +27 -33
  198. package/src/exports/ddl.ts +4 -0
  199. package/src/exports/diff-database-schema.ts +7 -0
  200. package/src/exports/issue-planner.ts +1 -1
  201. package/src/exports/migration.ts +1 -0
  202. package/src/exports/op-factory-call.ts +1 -0
  203. package/src/exports/planner-ddl-builders.ts +0 -2
  204. package/src/exports/planner-identity-values.ts +4 -1
  205. package/src/exports/planner.ts +1 -0
  206. package/src/exports/rls-canonicalize.ts +6 -0
  207. package/src/exports/runtime.ts +1 -0
  208. package/src/exports/schema-ir-annotations.ts +1 -0
  209. package/src/exports/types.ts +28 -0
  210. package/dist/codec-ids-BvytN2P8.mjs.map +0 -1
  211. package/dist/codec-ids-CnXu9Qy3.d.mts.map +0 -1
  212. package/dist/codec-types-DHCkwPKE.d.mts.map +0 -1
  213. package/dist/codecs--0A5_4Bq.d.mts.map +0 -1
  214. package/dist/ddl-QDyOSeLc.mjs.map +0 -1
  215. package/dist/default-normalizer-DyyCHQWs.mjs +0 -131
  216. package/dist/default-normalizer-DyyCHQWs.mjs.map +0 -1
  217. package/dist/descriptor-meta-CpGygXpI.mjs +0 -140
  218. package/dist/descriptor-meta-CpGygXpI.mjs.map +0 -1
  219. package/dist/issue-planner-DL6g3CmE.mjs +0 -824
  220. package/dist/issue-planner-DL6g3CmE.mjs.map +0 -1
  221. package/dist/native-type-normalizer-Bc9XJzWC.mjs +0 -38
  222. package/dist/native-type-normalizer-Bc9XJzWC.mjs.map +0 -1
  223. package/dist/nodes-Bbhs2rwj.mjs.map +0 -1
  224. package/dist/nodes-pLeLgdis.d.mts +0 -67
  225. package/dist/nodes-pLeLgdis.d.mts.map +0 -1
  226. package/dist/op-factory-call-D_p5vxwt.mjs.map +0 -1
  227. package/dist/op-factory-call-DmQEc3XV.d.mts.map +0 -1
  228. package/dist/planner-Bs_baQax.mjs +0 -324
  229. package/dist/planner-Bs_baQax.mjs.map +0 -1
  230. package/dist/planner-ddl-builders-B2wOwLqI.mjs.map +0 -1
  231. package/dist/planner-target-details-CIY6tLeo.d.mts.map +0 -1
  232. package/dist/postgres-contract-serializer-k3TAcPMY.mjs.map +0 -1
  233. package/dist/postgres-migration-B5jKrXv3.mjs.map +0 -1
  234. package/dist/postgres-migration-Y4YBJqkS.d.mts.map +0 -1
  235. package/dist/postgres-schema-COGZ1ark.mjs.map +0 -1
  236. package/dist/render-typescript-KMgosran.mjs.map +0 -1
  237. package/dist/types.d.mts.map +0 -1
  238. package/src/core/postgres-enum-type-schema.ts +0 -17
@@ -0,0 +1,16 @@
1
+ import type { DefaultMappingOptions } from '@prisma-next/family-sql/psl-infer';
2
+
3
+ const POSTGRES_FUNCTION_ATTRIBUTES: Readonly<Record<string, string>> = {
4
+ 'gen_random_uuid()': '@default(dbgenerated("gen_random_uuid()"))',
5
+ };
6
+
7
+ function formatDbGeneratedAttribute(expression: string): string {
8
+ return `@default(dbgenerated(${JSON.stringify(expression)}))`;
9
+ }
10
+
11
+ export function createPostgresDefaultMapping(): DefaultMappingOptions {
12
+ return {
13
+ functionAttributes: POSTGRES_FUNCTION_ATTRIBUTES,
14
+ fallbackFunctionAttribute: formatDbGeneratedAttribute,
15
+ };
16
+ }
@@ -0,0 +1,157 @@
1
+ import type {
2
+ EnumInfo,
3
+ PslNativeTypeAttribute,
4
+ PslTypeMap,
5
+ PslTypeResolution,
6
+ } from '@prisma-next/family-sql/psl-infer';
7
+
8
+ const POSTGRES_TO_PSL: Record<string, string> = {
9
+ text: 'String',
10
+ bool: 'Boolean',
11
+ boolean: 'Boolean',
12
+ int4: 'Int',
13
+ integer: 'Int',
14
+ int8: 'BigInt',
15
+ bigint: 'BigInt',
16
+ float8: 'Float',
17
+ 'double precision': 'Float',
18
+ numeric: 'Decimal',
19
+ decimal: 'Decimal',
20
+ timestamptz: 'DateTime',
21
+ 'timestamp with time zone': 'DateTime',
22
+ jsonb: 'Json',
23
+ bytea: 'Bytes',
24
+ };
25
+
26
+ const PRESERVED_NATIVE_TYPES: Record<
27
+ string,
28
+ { readonly pslType: string; readonly attributeName: string }
29
+ > = {
30
+ 'character varying': { pslType: 'String', attributeName: 'db.VarChar' },
31
+ character: { pslType: 'String', attributeName: 'db.Char' },
32
+ char: { pslType: 'String', attributeName: 'db.Char' },
33
+ varchar: { pslType: 'String', attributeName: 'db.VarChar' },
34
+ uuid: { pslType: 'String', attributeName: 'db.Uuid' },
35
+ int2: { pslType: 'Int', attributeName: 'db.SmallInt' },
36
+ smallint: { pslType: 'Int', attributeName: 'db.SmallInt' },
37
+ float4: { pslType: 'Float', attributeName: 'db.Real' },
38
+ real: { pslType: 'Float', attributeName: 'db.Real' },
39
+ timestamp: { pslType: 'DateTime', attributeName: 'db.Timestamp' },
40
+ 'timestamp without time zone': { pslType: 'DateTime', attributeName: 'db.Timestamp' },
41
+ date: { pslType: 'DateTime', attributeName: 'db.Date' },
42
+ time: { pslType: 'DateTime', attributeName: 'db.Time' },
43
+ 'time without time zone': { pslType: 'DateTime', attributeName: 'db.Time' },
44
+ timetz: { pslType: 'DateTime', attributeName: 'db.Timetz' },
45
+ 'time with time zone': { pslType: 'DateTime', attributeName: 'db.Timetz' },
46
+ json: { pslType: 'Json', attributeName: 'db.Json' },
47
+ };
48
+
49
+ const PARAMETERIZED_NATIVE_TYPES: Record<
50
+ string,
51
+ { readonly pslType: string; readonly attributeName: string }
52
+ > = {
53
+ 'character varying': { pslType: 'String', attributeName: 'db.VarChar' },
54
+ character: { pslType: 'String', attributeName: 'db.Char' },
55
+ char: { pslType: 'String', attributeName: 'db.Char' },
56
+ varchar: { pslType: 'String', attributeName: 'db.VarChar' },
57
+ numeric: { pslType: 'Decimal', attributeName: 'db.Numeric' },
58
+ timestamp: { pslType: 'DateTime', attributeName: 'db.Timestamp' },
59
+ timestamptz: { pslType: 'DateTime', attributeName: 'db.Timestamptz' },
60
+ time: { pslType: 'DateTime', attributeName: 'db.Time' },
61
+ timetz: { pslType: 'DateTime', attributeName: 'db.Timetz' },
62
+ };
63
+
64
+ const PARAMETERIZED_TYPE_PATTERN = /^(.+?)\((.+)\)$/;
65
+
66
+ function getOwnMappingValue(map: Record<string, string>, key: string): string | undefined {
67
+ return Object.hasOwn(map, key) ? map[key] : undefined;
68
+ }
69
+
70
+ function getOwnRecordValue<T>(map: Record<string, T>, key: string): T | undefined {
71
+ return Object.hasOwn(map, key) ? map[key] : undefined;
72
+ }
73
+
74
+ function createNativeTypeAttribute(name: string, args?: readonly string[]): PslNativeTypeAttribute {
75
+ return args && args.length > 0 ? { name, args } : { name };
76
+ }
77
+
78
+ function splitTypeParameterList(params: string): readonly string[] {
79
+ return params
80
+ .split(',')
81
+ .map((part) => part.trim())
82
+ .filter((part) => part.length > 0);
83
+ }
84
+
85
+ export function createPostgresTypeMap(enumTypeNames?: ReadonlySet<string>): PslTypeMap {
86
+ return {
87
+ resolve(nativeType: string): PslTypeResolution {
88
+ if (enumTypeNames?.has(nativeType)) {
89
+ return { pslType: nativeType, nativeType };
90
+ }
91
+
92
+ const paramMatch = nativeType.match(PARAMETERIZED_TYPE_PATTERN);
93
+ if (paramMatch) {
94
+ const [, baseType = nativeType, params = ''] = paramMatch;
95
+ const template = getOwnRecordValue(PARAMETERIZED_NATIVE_TYPES, baseType);
96
+ if (template) {
97
+ return {
98
+ pslType: template.pslType,
99
+ nativeType,
100
+ typeParams: { baseType, params },
101
+ nativeTypeAttribute: createNativeTypeAttribute(
102
+ template.attributeName,
103
+ splitTypeParameterList(params),
104
+ ),
105
+ };
106
+ }
107
+ }
108
+
109
+ const preservedType = getOwnRecordValue(PRESERVED_NATIVE_TYPES, nativeType);
110
+ if (preservedType) {
111
+ return {
112
+ pslType: preservedType.pslType,
113
+ nativeType,
114
+ nativeTypeAttribute: createNativeTypeAttribute(preservedType.attributeName),
115
+ };
116
+ }
117
+
118
+ const pslType = getOwnMappingValue(POSTGRES_TO_PSL, nativeType);
119
+ if (pslType) {
120
+ return {
121
+ pslType,
122
+ nativeType,
123
+ };
124
+ }
125
+
126
+ return { unsupported: true, nativeType };
127
+ },
128
+ };
129
+ }
130
+
131
+ export function extractEnumInfo(annotations?: Record<string, unknown>): EnumInfo {
132
+ const pgAnnotations = annotations?.['pg'] as Record<string, unknown> | undefined;
133
+ const nativeEnumTypeNames = pgAnnotations?.['nativeEnumTypeNames'];
134
+
135
+ const typeNames = new Set<string>();
136
+ const definitions = new Map<string, readonly string[]>();
137
+
138
+ if (Array.isArray(nativeEnumTypeNames)) {
139
+ for (const name of nativeEnumTypeNames) {
140
+ if (typeof name === 'string') {
141
+ typeNames.add(name);
142
+ }
143
+ }
144
+ }
145
+
146
+ return { typeNames, definitions };
147
+ }
148
+
149
+ export function extractEnumTypeNames(annotations?: Record<string, unknown>): ReadonlySet<string> {
150
+ return extractEnumInfo(annotations).typeNames;
151
+ }
152
+
153
+ export function extractEnumDefinitions(
154
+ annotations?: Record<string, unknown>,
155
+ ): ReadonlyMap<string, readonly string[]> {
156
+ return extractEnumInfo(annotations).definitions;
157
+ }
@@ -0,0 +1,48 @@
1
+ import { createHash } from 'node:crypto';
2
+
3
+ export type RlsPolicyOperation = 'select' | 'insert' | 'update' | 'delete' | 'all';
4
+
5
+ export interface ContentHashParts {
6
+ readonly using?: string;
7
+ readonly withCheck?: string;
8
+ readonly roles: readonly string[];
9
+ readonly operation: RlsPolicyOperation;
10
+ readonly permissive: boolean;
11
+ }
12
+
13
+ /**
14
+ * Stabilizes an authored predicate for hashing: trim, and collapse runs of
15
+ * internal whitespace to a single space.
16
+ *
17
+ * This is deliberately minimal. The content hash is the equivalence relation
18
+ * for a policy, and the wire name (prefix + hash) is the only thing ever
19
+ * compared — we never recompute the hash from an introspected policy body, so
20
+ * there is no need to match Postgres's reprinted form. Minimal normalization
21
+ * also protects the no-collision property: aggressive rewriting (lowercasing,
22
+ * paren-stripping, cast-alias folding) risks collapsing two distinct predicates
23
+ * onto one hash. Out-of-band alteration of a hashed policy is unsupported.
24
+ *
25
+ * The normalizer is a stability commitment: any change re-suffixes all wire names.
26
+ */
27
+ export function normalizePredicate(sql: string): string {
28
+ return sql.replace(/\s+/g, ' ').trim();
29
+ }
30
+
31
+ /**
32
+ * Returns the first 8 lowercase hex characters of the SHA-256 digest over the
33
+ * canonical content tuple for an RLS policy:
34
+ *
35
+ * [normalize(using), normalize(withCheck), sortedRoles, operation, permissive]
36
+ *
37
+ * Schema and table are excluded (they are orthogonal to policy equivalence).
38
+ * Uses `JSON.stringify` for a deterministic encoding.
39
+ */
40
+ export function computeContentHash(parts: ContentHashParts): string {
41
+ const using = normalizePredicate(parts.using ?? '');
42
+ const withCheck = normalizePredicate(parts.withCheck ?? '');
43
+ const roles = [...new Set(parts.roles)].sort();
44
+ const permissive = parts.permissive ? 'permissive' : 'restrictive';
45
+
46
+ const tuple = JSON.stringify([using, withCheck, roles, parts.operation, permissive]);
47
+ return createHash('sha256').update(tuple).digest('hex').slice(0, 8);
48
+ }
@@ -0,0 +1,60 @@
1
+ import type { DiffableNode } from '@prisma-next/framework-components/control';
2
+ import { freezeNode } from '@prisma-next/framework-components/ir';
3
+ import { assertNode, SqlSchemaIRNode } from '@prisma-next/sql-schema-ir/types';
4
+ import type { PostgresNamespaceSchemaNode } from './postgres-namespace-schema-node';
5
+ import type { PostgresRoleSchemaNode } from './postgres-role-schema-node';
6
+ import { PostgresSchemaNodeKind } from './schema-node-kinds';
7
+
8
+ export interface PostgresDatabaseSchemaNodeInput {
9
+ readonly namespaces: Readonly<Record<string, PostgresNamespaceSchemaNode>>;
10
+ readonly roles: readonly PostgresRoleSchemaNode[];
11
+ readonly existingSchemas: readonly string[];
12
+ readonly pgVersion: string;
13
+ }
14
+
15
+ /**
16
+ * The root of the Postgres schema-diff tree: one node per database.
17
+ *
18
+ * `id` is the fixed sentinel `'database'` — the root has no siblings and
19
+ * the value is never emitted into migration paths. `isEqualTo` is identity
20
+ * (roots always share the `'database'` id). `children()` returns namespace
21
+ * nodes only; roles are held on the root but not yielded (role diffing is a
22
+ * later slice).
23
+ */
24
+ export class PostgresDatabaseSchemaNode extends SqlSchemaIRNode implements DiffableNode {
25
+ override readonly nodeKind = PostgresSchemaNodeKind.database;
26
+
27
+ readonly namespaces: Readonly<Record<string, PostgresNamespaceSchemaNode>>;
28
+ readonly roles: readonly PostgresRoleSchemaNode[];
29
+ readonly existingSchemas: readonly string[];
30
+ readonly pgVersion: string;
31
+
32
+ constructor(input: PostgresDatabaseSchemaNodeInput) {
33
+ super();
34
+ this.namespaces = Object.freeze({ ...input.namespaces });
35
+ this.roles = Object.freeze([...input.roles]);
36
+ this.existingSchemas = Object.freeze([...input.existingSchemas]);
37
+ this.pgVersion = input.pgVersion;
38
+ freezeNode(this);
39
+ }
40
+
41
+ get id(): string {
42
+ return 'database';
43
+ }
44
+
45
+ isEqualTo(other: DiffableNode): boolean {
46
+ return this.id === other.id;
47
+ }
48
+
49
+ children(): readonly DiffableNode[] {
50
+ return Object.values(this.namespaces);
51
+ }
52
+
53
+ static is(node: SqlSchemaIRNode): node is PostgresDatabaseSchemaNode {
54
+ return node.nodeKind === PostgresSchemaNodeKind.database;
55
+ }
56
+
57
+ static assert(node: SqlSchemaIRNode): asserts node is PostgresDatabaseSchemaNode {
58
+ assertNode(node, 'PostgresDatabaseSchemaNode', PostgresDatabaseSchemaNode.is);
59
+ }
60
+ }
@@ -0,0 +1,60 @@
1
+ import type { DiffableNode } from '@prisma-next/framework-components/control';
2
+ import { freezeNode } from '@prisma-next/framework-components/ir';
3
+ import { assertNode, SqlSchemaIRNode } from '@prisma-next/sql-schema-ir/types';
4
+ import type { PostgresTableSchemaNode } from './postgres-table-schema-node';
5
+ import { PostgresSchemaNodeKind } from './schema-node-kinds';
6
+
7
+ export interface PostgresNamespaceSchemaNodeInput {
8
+ readonly schemaName: string;
9
+ readonly tables: Readonly<Record<string, PostgresTableSchemaNode>>;
10
+ readonly nativeEnumTypeNames: readonly string[];
11
+ }
12
+
13
+ /**
14
+ * One-per-Postgres-schema diff-tree node. Groups the tables belonging to a
15
+ * single namespace. Per-schema consumers (the relational planner,
16
+ * toSchemaView) read this node's `tables` field structurally via
17
+ * `blindCast`/`SqlSchemaIRNode` — not through a static `SqlSchemaIR`
18
+ * assignment — because `nodeKind` now carries this node's own literal
19
+ * (`postgres-namespace`), distinct from `SqlSchemaIR`'s own (`sql-schema`).
20
+ *
21
+ * `id` is the schema name. `isEqualTo` is identity — two namespace nodes are
22
+ * equal iff their ids (schema names) match. `children()` returns the table
23
+ * nodes. Per-schema metadata is carried on the typed `nativeEnumTypeNames`
24
+ * field, not an annotations bag.
25
+ */
26
+ export class PostgresNamespaceSchemaNode extends SqlSchemaIRNode implements DiffableNode {
27
+ override readonly nodeKind = PostgresSchemaNodeKind.namespace;
28
+
29
+ readonly schemaName: string;
30
+ readonly tables: Readonly<Record<string, PostgresTableSchemaNode>>;
31
+ readonly nativeEnumTypeNames: readonly string[];
32
+
33
+ constructor(input: PostgresNamespaceSchemaNodeInput) {
34
+ super();
35
+ this.schemaName = input.schemaName;
36
+ this.tables = Object.freeze({ ...input.tables });
37
+ this.nativeEnumTypeNames = Object.freeze([...input.nativeEnumTypeNames]);
38
+ freezeNode(this);
39
+ }
40
+
41
+ get id(): string {
42
+ return this.schemaName;
43
+ }
44
+
45
+ isEqualTo(other: DiffableNode): boolean {
46
+ return this.id === other.id;
47
+ }
48
+
49
+ children(): readonly DiffableNode[] {
50
+ return Object.values(this.tables);
51
+ }
52
+
53
+ static is(node: SqlSchemaIRNode): node is PostgresNamespaceSchemaNode {
54
+ return node.nodeKind === PostgresSchemaNodeKind.namespace;
55
+ }
56
+
57
+ static assert(node: SqlSchemaIRNode): asserts node is PostgresNamespaceSchemaNode {
58
+ assertNode(node, 'PostgresNamespaceSchemaNode', PostgresNamespaceSchemaNode.is);
59
+ }
60
+ }
@@ -0,0 +1,90 @@
1
+ import type { DiffableNode } from '@prisma-next/framework-components/control';
2
+ import { freezeNode } from '@prisma-next/framework-components/ir';
3
+ import { assertNode, SqlSchemaIRNode } from '@prisma-next/sql-schema-ir/types';
4
+ import { blindCast } from '@prisma-next/utils/casts';
5
+ import type { RlsPolicyOperation } from '../postgres-rls-policy';
6
+ import { PostgresSchemaNodeKind } from './schema-node-kinds';
7
+
8
+ export interface PostgresPolicySchemaNodeInput {
9
+ /** Full wire name: `<prefix>_<8hex>`. */
10
+ readonly name: string;
11
+ /** User-supplied prefix (the part before the `_<8hex>` suffix). */
12
+ readonly prefix: string;
13
+ /** Name of the table this policy attaches to, by name within the same schema. */
14
+ readonly tableName: string;
15
+ /** Namespace coordinate (schema name). */
16
+ readonly namespaceId: string;
17
+ readonly operation: RlsPolicyOperation;
18
+ /** Sorted role names rendered in `TO <roles>`. */
19
+ readonly roles: readonly string[];
20
+ /** USING predicate SQL string, if present. */
21
+ readonly using?: string;
22
+ /** WITH CHECK predicate SQL string, if present. */
23
+ readonly withCheck?: string;
24
+ /** `true` = `AS PERMISSIVE`, `false` = `AS RESTRICTIVE`. */
25
+ readonly permissive: boolean;
26
+ }
27
+
28
+ /**
29
+ * Schema-diff leaf node for a Postgres row-level security policy.
30
+ *
31
+ * This is a derived, transient node walked by the differ — it is NEVER serialized.
32
+ * Built by project-from-contract and project-from-database from their respective
33
+ * `PostgresRlsPolicy` contract entities / introspected rows.
34
+ *
35
+ * `id` is the wire name (`<prefix>_<sha256(body)[0..8]>`), so name-equality is
36
+ * body-equality. `isEqualTo` compares names only — never byte-compare predicate
37
+ * bodies, because Postgres reprints them.
38
+ */
39
+ export class PostgresPolicySchemaNode extends SqlSchemaIRNode implements DiffableNode {
40
+ override readonly nodeKind = PostgresSchemaNodeKind.policy;
41
+
42
+ readonly name: string;
43
+ readonly prefix: string;
44
+ readonly tableName: string;
45
+ readonly namespaceId: string;
46
+ readonly operation: RlsPolicyOperation;
47
+ readonly roles: readonly string[];
48
+ declare readonly using?: string;
49
+ declare readonly withCheck?: string;
50
+ readonly permissive: boolean;
51
+
52
+ constructor(input: PostgresPolicySchemaNodeInput) {
53
+ super();
54
+ this.name = input.name;
55
+ this.prefix = input.prefix;
56
+ this.tableName = input.tableName;
57
+ this.namespaceId = input.namespaceId;
58
+ this.operation = input.operation;
59
+ this.roles = Object.freeze([...input.roles]);
60
+ if (input.using !== undefined) this.using = input.using;
61
+ if (input.withCheck !== undefined) this.withCheck = input.withCheck;
62
+ this.permissive = input.permissive;
63
+ freezeNode(this);
64
+ }
65
+
66
+ get id(): string {
67
+ return this.name;
68
+ }
69
+
70
+ children(): readonly DiffableNode[] {
71
+ return [];
72
+ }
73
+
74
+ isEqualTo(other: DiffableNode): boolean {
75
+ const node = blindCast<
76
+ SqlSchemaIRNode,
77
+ 'every diff-tree node the differ pairs is a SqlSchemaIRNode; the guard rejects non-policy kinds'
78
+ >(other);
79
+ PostgresPolicySchemaNode.assert(node);
80
+ return this.id === node.id;
81
+ }
82
+
83
+ static is(node: SqlSchemaIRNode): node is PostgresPolicySchemaNode {
84
+ return node.nodeKind === PostgresSchemaNodeKind.policy;
85
+ }
86
+
87
+ static assert(node: SqlSchemaIRNode | undefined): asserts node is PostgresPolicySchemaNode {
88
+ assertNode(node, 'PostgresPolicySchemaNode', PostgresPolicySchemaNode.is);
89
+ }
90
+ }
@@ -0,0 +1,63 @@
1
+ import type { DiffableNode } from '@prisma-next/framework-components/control';
2
+ import { freezeNode } from '@prisma-next/framework-components/ir';
3
+ import { assertNode, SqlSchemaIRNode } from '@prisma-next/sql-schema-ir/types';
4
+ import { blindCast } from '@prisma-next/utils/casts';
5
+ import { PostgresSchemaNodeKind } from './schema-node-kinds';
6
+
7
+ export interface PostgresRoleSchemaNodeInput {
8
+ readonly name: string;
9
+ /**
10
+ * Namespace coordinate. Roles are cluster-scoped; callers pass
11
+ * `UNBOUND_NAMESPACE_ID` from `@prisma-next/framework-components/ir`.
12
+ */
13
+ readonly namespaceId: string;
14
+ }
15
+
16
+ /**
17
+ * Schema-diff leaf node for a Postgres database role.
18
+ *
19
+ * This is a derived, transient node walked by the differ — it is NEVER serialized.
20
+ * Built by project-from-contract and project-from-database from their respective
21
+ * `PostgresRole` contract entities / introspected rows.
22
+ *
23
+ * Roles are cluster-scoped, so `id` is the role name alone. `isEqualTo` compares
24
+ * names — name-equality is role-equality for cluster-scoped objects.
25
+ */
26
+ export class PostgresRoleSchemaNode extends SqlSchemaIRNode implements DiffableNode {
27
+ override readonly nodeKind = PostgresSchemaNodeKind.role;
28
+
29
+ readonly name: string;
30
+ readonly namespaceId: string;
31
+
32
+ constructor(input: PostgresRoleSchemaNodeInput) {
33
+ super();
34
+ this.name = input.name;
35
+ this.namespaceId = input.namespaceId;
36
+ freezeNode(this);
37
+ }
38
+
39
+ get id(): string {
40
+ return this.name;
41
+ }
42
+
43
+ children(): readonly DiffableNode[] {
44
+ return [];
45
+ }
46
+
47
+ isEqualTo(other: DiffableNode): boolean {
48
+ const node = blindCast<
49
+ SqlSchemaIRNode,
50
+ 'every diff-tree node the differ pairs is a SqlSchemaIRNode; the guard rejects non-role kinds'
51
+ >(other);
52
+ PostgresRoleSchemaNode.assert(node);
53
+ return this.id === node.id;
54
+ }
55
+
56
+ static is(node: SqlSchemaIRNode): node is PostgresRoleSchemaNode {
57
+ return node.nodeKind === PostgresSchemaNodeKind.role;
58
+ }
59
+
60
+ static assert(node: SqlSchemaIRNode): asserts node is PostgresRoleSchemaNode {
61
+ assertNode(node, 'PostgresRoleSchemaNode', PostgresRoleSchemaNode.is);
62
+ }
63
+ }
@@ -0,0 +1,118 @@
1
+ import type { DiffableNode } from '@prisma-next/framework-components/control';
2
+ import { freezeNode } from '@prisma-next/framework-components/ir';
3
+ import {
4
+ assertNode,
5
+ PrimaryKey,
6
+ type SqlAnnotations,
7
+ SqlCheckConstraintIR,
8
+ SqlColumnIR,
9
+ SqlForeignKeyIR,
10
+ SqlIndexIR,
11
+ SqlSchemaIRNode,
12
+ type SqlTableIRInput,
13
+ SqlUniqueIR,
14
+ } from '@prisma-next/sql-schema-ir/types';
15
+ import type { PostgresPolicySchemaNode } from './postgres-policy-schema-node';
16
+ import { PostgresSchemaNodeKind } from './schema-node-kinds';
17
+
18
+ export interface PostgresTableSchemaNodeInput extends SqlTableIRInput {
19
+ readonly policies?: readonly PostgresPolicySchemaNode[];
20
+ }
21
+
22
+ /**
23
+ * Postgres-specific table schema-diff node. Carries all `SqlTableIR` fields
24
+ * plus `policies`, and implements `DiffableNode` so the table instance is
25
+ * directly the diff-tree node — no separate wrapper needed.
26
+ *
27
+ * Extends `SqlSchemaIRNode` directly rather than `SqlTableIR` because
28
+ * `SqlTableIR` calls `freezeNode` in its own constructor, which prevents
29
+ * subclass field initialisation.
30
+ *
31
+ * `id` is the table name. `isEqualTo` is identity — two table nodes are equal
32
+ * iff their ids (names) match; the table's own structural drift is entirely
33
+ * expressed by its children. `children()` returns every column, the primary
34
+ * key (when present), every foreign key, unique, index, and check constraint,
35
+ * plus the policy nodes — one flat list, so a drifted column and a drifted
36
+ * policy are both reported by the same walk. Order is deterministic (object
37
+ * key order for columns, array order for the rest) so two structurally equal
38
+ * tables always produce the same child list.
39
+ */
40
+ export class PostgresTableSchemaNode extends SqlSchemaIRNode implements DiffableNode {
41
+ override readonly nodeKind = PostgresSchemaNodeKind.table;
42
+
43
+ readonly name: string;
44
+ readonly columns: Readonly<Record<string, SqlColumnIR>>;
45
+ readonly foreignKeys: ReadonlyArray<SqlForeignKeyIR>;
46
+ readonly uniques: ReadonlyArray<SqlUniqueIR>;
47
+ readonly indexes: ReadonlyArray<SqlIndexIR>;
48
+ declare readonly primaryKey?: PrimaryKey;
49
+ declare readonly annotations?: SqlAnnotations;
50
+ declare readonly checks?: ReadonlyArray<SqlCheckConstraintIR>;
51
+ readonly policies: readonly PostgresPolicySchemaNode[];
52
+
53
+ constructor(input: PostgresTableSchemaNodeInput) {
54
+ super();
55
+ this.name = input.name;
56
+ this.columns = Object.freeze(
57
+ Object.fromEntries(
58
+ Object.entries(input.columns).map(([key, col]) => [
59
+ key,
60
+ col instanceof SqlColumnIR ? col : new SqlColumnIR(col),
61
+ ]),
62
+ ),
63
+ );
64
+ this.foreignKeys = Object.freeze(
65
+ input.foreignKeys.map((fk) => (fk instanceof SqlForeignKeyIR ? fk : new SqlForeignKeyIR(fk))),
66
+ );
67
+ this.uniques = Object.freeze(
68
+ input.uniques.map((u) => (u instanceof SqlUniqueIR ? u : new SqlUniqueIR(u))),
69
+ );
70
+ this.indexes = Object.freeze(
71
+ input.indexes.map((i) => (i instanceof SqlIndexIR ? i : new SqlIndexIR(i))),
72
+ );
73
+ if (input.primaryKey !== undefined) {
74
+ this.primaryKey =
75
+ input.primaryKey instanceof PrimaryKey
76
+ ? input.primaryKey
77
+ : new PrimaryKey(input.primaryKey);
78
+ }
79
+ if (input.annotations !== undefined) this.annotations = input.annotations;
80
+ if (input.checks !== undefined && input.checks.length > 0) {
81
+ this.checks = Object.freeze(
82
+ input.checks.map((c) =>
83
+ c instanceof SqlCheckConstraintIR ? c : new SqlCheckConstraintIR(c),
84
+ ),
85
+ );
86
+ }
87
+ this.policies = Object.freeze([...(input.policies ?? [])]);
88
+ freezeNode(this);
89
+ }
90
+
91
+ get id(): string {
92
+ return this.name;
93
+ }
94
+
95
+ isEqualTo(other: DiffableNode): boolean {
96
+ return this.id === other.id;
97
+ }
98
+
99
+ children(): readonly DiffableNode[] {
100
+ return [
101
+ ...Object.values(this.columns),
102
+ ...(this.primaryKey ? [this.primaryKey] : []),
103
+ ...this.foreignKeys,
104
+ ...this.uniques,
105
+ ...this.indexes,
106
+ ...(this.checks ?? []),
107
+ ...this.policies,
108
+ ];
109
+ }
110
+
111
+ static is(node: SqlSchemaIRNode): node is PostgresTableSchemaNode {
112
+ return node.nodeKind === PostgresSchemaNodeKind.table;
113
+ }
114
+
115
+ static assert(node: SqlSchemaIRNode): asserts node is PostgresTableSchemaNode {
116
+ assertNode(node, 'PostgresTableSchemaNode', PostgresTableSchemaNode.is);
117
+ }
118
+ }