@prisma-next/target-postgres 0.14.0-dev.4 → 0.14.0-dev.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/dist/authoring-BX1WHIdN.mjs +220 -0
  2. package/dist/authoring-BX1WHIdN.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-types-DHCkwPKE.d.mts → codec-types-CueCoGD0.d.mts} +2 -2
  8. package/dist/{codec-types-DHCkwPKE.d.mts.map → codec-types-CueCoGD0.d.mts.map} +1 -1
  9. package/dist/codec-types.d.mts +1 -1
  10. package/dist/{codecs--0A5_4Bq.d.mts → codecs-Ca8upop6.d.mts} +12 -1
  11. package/dist/codecs-Ca8upop6.d.mts.map +1 -0
  12. package/dist/codecs.d.mts +1 -1
  13. package/dist/codecs.mjs +34 -1
  14. package/dist/codecs.mjs.map +1 -1
  15. package/dist/contract-free.d.mts +8 -3
  16. package/dist/contract-free.d.mts.map +1 -1
  17. package/dist/contract-free.mjs +3 -3
  18. package/dist/control.d.mts +1 -1
  19. package/dist/control.d.mts.map +1 -1
  20. package/dist/control.mjs +639 -21
  21. package/dist/control.mjs.map +1 -1
  22. package/dist/data-transform-BOWpliq8.mjs.map +1 -1
  23. package/dist/{data-transform-DDgWdB5o.d.mts → data-transform-qKy1U5V9.d.mts} +2 -2
  24. package/dist/{data-transform-DDgWdB5o.d.mts.map → data-transform-qKy1U5V9.d.mts.map} +1 -1
  25. package/dist/data-transform.d.mts +1 -1
  26. package/dist/{ddl-QDyOSeLc.mjs → ddl-BmLIe1z_.mjs} +62 -5
  27. package/dist/ddl-BmLIe1z_.mjs.map +1 -0
  28. package/dist/ddl.d.mts +3 -2
  29. package/dist/ddl.mjs +2 -2
  30. package/dist/{default-normalizer-DyyCHQWs.mjs → default-normalizer-Dug8Fez9.mjs} +119 -2
  31. package/dist/default-normalizer-Dug8Fez9.mjs.map +1 -0
  32. package/dist/default-normalizer.d.mts +1 -1
  33. package/dist/default-normalizer.d.mts.map +1 -1
  34. package/dist/default-normalizer.mjs +1 -1
  35. package/dist/descriptor-meta-runtime-UwYdV8fD.mjs +12 -0
  36. package/dist/descriptor-meta-runtime-UwYdV8fD.mjs.map +1 -0
  37. package/dist/descriptor-meta-tjatyhtZ.mjs +18 -0
  38. package/dist/descriptor-meta-tjatyhtZ.mjs.map +1 -0
  39. package/dist/{issue-planner-DL6g3CmE.mjs → issue-planner-DbFvaAgu.mjs} +35 -10
  40. package/dist/issue-planner-DbFvaAgu.mjs.map +1 -0
  41. package/dist/issue-planner.d.mts +3 -3
  42. package/dist/issue-planner.d.mts.map +1 -1
  43. package/dist/issue-planner.mjs +1 -1
  44. package/dist/migration.d.mts +5 -5
  45. package/dist/migration.mjs +4 -4
  46. package/dist/nodes-CSkPX3rI.d.mts +120 -0
  47. package/dist/nodes-CSkPX3rI.d.mts.map +1 -0
  48. package/dist/{nodes-Bbhs2rwj.mjs → nodes-Dh__NS58.mjs} +56 -2
  49. package/dist/nodes-Dh__NS58.mjs.map +1 -0
  50. package/dist/{op-factory-call-DmQEc3XV.d.mts → op-factory-call-CJWUxyUW.d.mts} +44 -13
  51. package/dist/op-factory-call-CJWUxyUW.d.mts.map +1 -0
  52. package/dist/{op-factory-call-D_p5vxwt.mjs → op-factory-call-t65c4jyY.mjs} +201 -28
  53. package/dist/op-factory-call-t65c4jyY.mjs.map +1 -0
  54. package/dist/op-factory-call.d.mts +1 -1
  55. package/dist/op-factory-call.mjs +1 -1
  56. package/dist/pack.d.mts +74 -2
  57. package/dist/pack.d.mts.map +1 -1
  58. package/dist/pack.mjs +1 -1
  59. package/dist/planner-CG2pLHk-.mjs +640 -0
  60. package/dist/planner-CG2pLHk-.mjs.map +1 -0
  61. package/dist/{planner-ddl-builders-B2wOwLqI.mjs → planner-ddl-builders-B8Nn6nHN.mjs} +12 -27
  62. package/dist/planner-ddl-builders-B8Nn6nHN.mjs.map +1 -0
  63. package/dist/planner-ddl-builders.d.mts +2 -4
  64. package/dist/planner-ddl-builders.d.mts.map +1 -1
  65. package/dist/planner-ddl-builders.mjs +2 -2
  66. package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs → planner-produced-postgres-migration-BolD0Mvc.mjs} +3 -3
  67. package/dist/{planner-produced-postgres-migration-Cji5vxUf.mjs.map → planner-produced-postgres-migration-BolD0Mvc.mjs.map} +1 -1
  68. package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts → planner-produced-postgres-migration-Cz9--XOT.d.mts} +4 -4
  69. package/dist/{planner-produced-postgres-migration-QqHa2C2l.d.mts.map → planner-produced-postgres-migration-Cz9--XOT.d.mts.map} +1 -1
  70. package/dist/planner-produced-postgres-migration.d.mts +1 -1
  71. package/dist/planner-produced-postgres-migration.mjs +1 -1
  72. package/dist/{planner-sql-checks-jqUUGyQR.mjs → planner-sql-checks-DE9c4519.mjs} +2 -2
  73. package/dist/{planner-sql-checks-jqUUGyQR.mjs.map → planner-sql-checks-DE9c4519.mjs.map} +1 -1
  74. package/dist/planner-sql-checks.mjs +1 -1
  75. package/dist/{planner-target-details-CIY6tLeo.d.mts → planner-target-details-HkP4RrRO.d.mts} +2 -2
  76. package/dist/planner-target-details-HkP4RrRO.d.mts.map +1 -0
  77. package/dist/planner-target-details.d.mts +1 -1
  78. package/dist/planner.d.mts +82 -4
  79. package/dist/planner.d.mts.map +1 -1
  80. package/dist/planner.mjs +2 -2
  81. package/dist/{postgres-contract-serializer-k3TAcPMY.mjs → postgres-contract-view-BegduLr6.mjs} +50 -23
  82. package/dist/postgres-contract-view-BegduLr6.mjs.map +1 -0
  83. package/dist/postgres-contract-view-C9j5cqP0.d.mts +34 -0
  84. package/dist/postgres-contract-view-C9j5cqP0.d.mts.map +1 -0
  85. package/dist/postgres-database-schema-node-os8ogEap.d.mts +183 -0
  86. package/dist/postgres-database-schema-node-os8ogEap.d.mts.map +1 -0
  87. package/dist/{postgres-migration-B5jKrXv3.mjs → postgres-migration-BfMmK6tv.mjs} +21 -2
  88. package/dist/postgres-migration-BfMmK6tv.mjs.map +1 -0
  89. package/dist/{postgres-migration-Y4YBJqkS.d.mts → postgres-migration-D7kNVWVG.d.mts} +19 -6
  90. package/dist/postgres-migration-D7kNVWVG.d.mts.map +1 -0
  91. package/dist/postgres-rls-policy--CSddvMR.d.mts +52 -0
  92. package/dist/postgres-rls-policy--CSddvMR.d.mts.map +1 -0
  93. package/dist/{postgres-schema-COGZ1ark.mjs → postgres-schema-BLIHBEfX.mjs} +108 -5
  94. package/dist/postgres-schema-BLIHBEfX.mjs.map +1 -0
  95. package/dist/postgres-schema-BywoeKlt.d.mts +189 -0
  96. package/dist/postgres-schema-BywoeKlt.d.mts.map +1 -0
  97. package/dist/postgres-table-schema-node-1nNc8XDT.mjs +248 -0
  98. package/dist/postgres-table-schema-node-1nNc8XDT.mjs.map +1 -0
  99. package/dist/render-ops.d.mts +1 -1
  100. package/dist/{render-typescript-KMgosran.mjs → render-typescript-7yqILcwr.mjs} +45 -16
  101. package/dist/render-typescript-7yqILcwr.mjs.map +1 -0
  102. package/dist/render-typescript.mjs +1 -1
  103. package/dist/resolve-ddl-schema-CL1RR1mx.mjs +20 -0
  104. package/dist/resolve-ddl-schema-CL1RR1mx.mjs.map +1 -0
  105. package/dist/rls-canonicalize.d.mts +2 -0
  106. package/dist/rls-canonicalize.mjs +2 -0
  107. package/dist/runtime.d.mts +7 -6
  108. package/dist/runtime.d.mts.map +1 -1
  109. package/dist/runtime.mjs +3 -3
  110. package/dist/runtime.mjs.map +1 -1
  111. package/dist/schema-ir-annotations.d.mts +14 -0
  112. package/dist/schema-ir-annotations.d.mts.map +1 -0
  113. package/dist/schema-ir-annotations.mjs +2 -0
  114. package/dist/types.d.mts +4 -140
  115. package/dist/types.mjs +3 -2
  116. package/package.json +22 -17
  117. package/src/contract-free/checks.ts +73 -0
  118. package/src/contract-free/ddl.ts +45 -1
  119. package/src/core/authoring.ts +115 -1
  120. package/src/core/codecs.ts +35 -1
  121. package/src/core/ddl/nodes.ts +94 -2
  122. package/src/core/default-normalizer.ts +122 -2
  123. package/src/core/descriptor-meta.ts +4 -0
  124. package/src/core/entity-kinds.ts +16 -0
  125. package/src/core/migrations/bound-schema.ts +10 -0
  126. package/src/core/migrations/contract-to-postgres-database-schema-node.ts +125 -0
  127. package/src/core/migrations/control-policy.ts +3 -0
  128. package/src/core/migrations/diff-database-schema.ts +191 -0
  129. package/src/core/migrations/issue-planner.ts +44 -3
  130. package/src/core/migrations/op-factory-call.ts +109 -21
  131. package/src/core/migrations/operations/columns.ts +24 -12
  132. package/src/core/migrations/operations/data-transform.ts +6 -0
  133. package/src/core/migrations/operations/rls.ts +106 -0
  134. package/src/core/migrations/operations/shared.ts +0 -30
  135. package/src/core/migrations/planner-ddl-builders.ts +15 -56
  136. package/src/core/migrations/planner-recipes.ts +31 -13
  137. package/src/core/migrations/planner-strategies.ts +3 -11
  138. package/src/core/migrations/planner-target-details.ts +3 -1
  139. package/src/core/migrations/planner.ts +165 -33
  140. package/src/core/migrations/postgres-migration.ts +34 -4
  141. package/src/core/migrations/render-typescript.ts +42 -15
  142. package/src/core/migrations/resolve-ddl-schema.ts +24 -0
  143. package/src/core/migrations/runner.ts +13 -16
  144. package/src/core/migrations/verify-postgres-namespaces.ts +24 -19
  145. package/src/core/postgres-contract-serializer.ts +50 -33
  146. package/src/core/postgres-contract-view.ts +48 -0
  147. package/src/core/postgres-rls-policy.ts +64 -0
  148. package/src/core/postgres-role.ts +37 -0
  149. package/src/core/postgres-schema.ts +37 -13
  150. package/src/core/postgres-validators.ts +20 -0
  151. package/src/core/psl-infer/infer-psl-contract.ts +834 -0
  152. package/src/core/psl-infer/postgres-default-mapping.ts +16 -0
  153. package/src/core/psl-infer/postgres-type-map.ts +157 -0
  154. package/src/core/rls/canonicalize.ts +48 -0
  155. package/src/core/schema-ir/postgres-database-schema-node.ts +63 -0
  156. package/src/core/schema-ir/postgres-namespace-schema-node.ts +61 -0
  157. package/src/core/schema-ir/postgres-policy-schema-node.ts +93 -0
  158. package/src/core/schema-ir/postgres-role-schema-node.ts +66 -0
  159. package/src/core/schema-ir/postgres-table-schema-node.ts +107 -0
  160. package/src/core/schema-ir/schema-node-kinds.ts +28 -0
  161. package/src/exports/contract-free.ts +7 -1
  162. package/src/exports/control.ts +39 -12
  163. package/src/exports/ddl.ts +4 -0
  164. package/src/exports/migration.ts +1 -0
  165. package/src/exports/planner-ddl-builders.ts +0 -2
  166. package/src/exports/planner.ts +2 -0
  167. package/src/exports/rls-canonicalize.ts +6 -0
  168. package/src/exports/runtime.ts +1 -0
  169. package/src/exports/schema-ir-annotations.ts +1 -0
  170. package/src/exports/types.ts +28 -0
  171. package/dist/codecs--0A5_4Bq.d.mts.map +0 -1
  172. package/dist/ddl-QDyOSeLc.mjs.map +0 -1
  173. package/dist/default-normalizer-DyyCHQWs.mjs.map +0 -1
  174. package/dist/descriptor-meta-CpGygXpI.mjs +0 -140
  175. package/dist/descriptor-meta-CpGygXpI.mjs.map +0 -1
  176. package/dist/issue-planner-DL6g3CmE.mjs.map +0 -1
  177. package/dist/nodes-Bbhs2rwj.mjs.map +0 -1
  178. package/dist/nodes-pLeLgdis.d.mts +0 -67
  179. package/dist/nodes-pLeLgdis.d.mts.map +0 -1
  180. package/dist/op-factory-call-D_p5vxwt.mjs.map +0 -1
  181. package/dist/op-factory-call-DmQEc3XV.d.mts.map +0 -1
  182. package/dist/planner-Bs_baQax.mjs +0 -324
  183. package/dist/planner-Bs_baQax.mjs.map +0 -1
  184. package/dist/planner-ddl-builders-B2wOwLqI.mjs.map +0 -1
  185. package/dist/planner-target-details-CIY6tLeo.d.mts.map +0 -1
  186. package/dist/postgres-contract-serializer-k3TAcPMY.mjs.map +0 -1
  187. package/dist/postgres-migration-B5jKrXv3.mjs.map +0 -1
  188. package/dist/postgres-migration-Y4YBJqkS.d.mts.map +0 -1
  189. package/dist/postgres-schema-COGZ1ark.mjs.map +0 -1
  190. package/dist/render-typescript-KMgosran.mjs.map +0 -1
  191. package/dist/types.d.mts.map +0 -1
@@ -1,4 +1,6 @@
1
1
  import type { ExecuteRequestLowerer } from '@prisma-next/family-sql/control-adapter';
2
+ import type { DdlColumn } from '@prisma-next/sql-relational-core/ast';
3
+ import { ifDefined } from '@prisma-next/utils/defined';
2
4
  import {
3
5
  columnDefaultAst,
4
6
  columnExistsAst,
@@ -7,7 +9,9 @@ import {
7
9
  noNullValuesAst,
8
10
  tableIsEmptyAst,
9
11
  } from '../../../contract-free/checks';
12
+ import * as contractFreeDdl from '../../../contract-free/ddl';
10
13
  import { quoteIdentifier } from '../../sql-utils';
14
+ import { boundSchema } from '../bound-schema';
11
15
  import { qualifyTableName } from '../planner-sql-checks';
12
16
  import { type Op, step, targetDetails } from './shared';
13
17
 
@@ -242,12 +246,18 @@ export async function dropDefault(
242
246
  columnName: string,
243
247
  lowerer: ExecuteRequestLowerer,
244
248
  ): Promise<Op> {
245
- const qualified = qualifyTableName(schemaName, tableName);
246
249
  const { present } = await columnExistsSteps(lowerer, {
247
250
  schema: schemaName,
248
251
  table: tableName,
249
252
  column: columnName,
250
253
  });
254
+ const dropDefaultExec = await lowerer.lowerToExecuteRequest(
255
+ contractFreeDdl.alterTable({
256
+ ...ifDefined('schema', boundSchema(schemaName)),
257
+ table: tableName,
258
+ actions: [contractFreeDdl.dropDefaultAction(columnName)],
259
+ }),
260
+ );
251
261
  const noDefault = await lowerer.lowerToExecuteRequest(
252
262
  columnDefaultAst({ schema: schemaName, table: tableName, column: columnName }).defaultAbsent(),
253
263
  );
@@ -257,12 +267,7 @@ export async function dropDefault(
257
267
  operationClass: 'destructive',
258
268
  target: targetDetails('column', columnName, schemaName, tableName),
259
269
  precheck: [step(`ensure column "${columnName}" exists`, present.sql, present.params)],
260
- execute: [
261
- step(
262
- `drop default on "${columnName}"`,
263
- `ALTER TABLE ${qualified} ALTER COLUMN ${quoteIdentifier(columnName)} DROP DEFAULT`,
264
- ),
265
- ],
270
+ execute: [step(`drop default on "${columnName}"`, dropDefaultExec.sql)],
266
271
  postcheck: [
267
272
  step(`verify column "${columnName}" has no default`, noDefault.sql, noDefault.params),
268
273
  ],
@@ -272,16 +277,23 @@ export async function dropDefault(
272
277
  /**
273
278
  * Builds the op for adding a NOT NULL column (no contract default) to a
274
279
  * non-empty table. Prechecks assert the column is absent and the table is
275
- * empty; the execute step is the raw ADD COLUMN SQL passed by the caller
276
- * (slice-7 deferred); postchecks assert the column exists and is NOT NULL.
280
+ * empty; the execute step lowers the typed `AddColumn` DDL node through the
281
+ * adapter; postchecks assert the column exists and is NOT NULL.
277
282
  */
278
283
  export async function addNotNullColumnDirect(
279
284
  schemaName: string,
280
285
  tableName: string,
281
- columnName: string,
282
- executeStepSql: string,
286
+ column: DdlColumn,
283
287
  lowerer: ExecuteRequestLowerer,
284
288
  ): Promise<Op> {
289
+ const columnName = column.name;
290
+ const addColumn = await lowerer.lowerToExecuteRequest(
291
+ contractFreeDdl.alterTable({
292
+ ...ifDefined('schema', boundSchema(schemaName)),
293
+ table: tableName,
294
+ actions: [contractFreeDdl.addColumnAction(column)],
295
+ }),
296
+ );
285
297
  const absent = await lowerer.lowerToExecuteRequest(
286
298
  columnExistsAst({ schema: schemaName, table: tableName, column: columnName }).columnAbsent(),
287
299
  );
@@ -311,7 +323,7 @@ export async function addNotNullColumnDirect(
311
323
  tableEmpty.params,
312
324
  ),
313
325
  ],
314
- execute: [step(`add column "${columnName}"`, executeStepSql)],
326
+ execute: [step(`add column "${columnName}"`, addColumn.sql)],
315
327
  postcheck: [
316
328
  step(`verify column "${columnName}" exists`, present.sql, present.params),
317
329
  step(`verify column "${columnName}" is NOT NULL`, notNullable.sql, notNullable.params),
@@ -113,6 +113,12 @@ export async function dataTransform<TContract extends Contract<SqlStorage>>(
113
113
  runClosures.map((closure) => invokeAndLower(closure, contract, adapter, name)),
114
114
  );
115
115
 
116
+ // Raw remnant: the factory lowered the user's `check` plan above before
117
+ // we get a chance to wrap it, so the EXISTS lifting only has text by the
118
+ // time it runs. The fix is to rewrite the factory to defer lowering until
119
+ // after wrapping — so `cfExpr.exists(checkPlan.ast)` can produce the
120
+ // EXISTS query, then lower once at the boundary — not to grow new AST
121
+ // substrate. Out of scope for the slice that introduced this comment.
116
122
  const precheck: readonly SqlMigrationPlanOperationStep[] = checkPlan
117
123
  ? [
118
124
  {
@@ -0,0 +1,106 @@
1
+ import type { ExecuteRequestLowerer } from '@prisma-next/family-sql/control-adapter';
2
+ import { ifDefined } from '@prisma-next/utils/defined';
3
+ import { rlsEnabledAst, rlsPolicyExistsAst } from '../../../contract-free/checks';
4
+ import { createPolicy, dropPolicy } from '../../../contract-free/ddl';
5
+ import type { PostgresRlsPolicy } from '../../postgres-rls-policy';
6
+ import { qualifyTableName } from '../planner-sql-checks';
7
+ import { type Op, step, targetDetails } from './shared';
8
+
9
+ const PLAIN_IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_$]*$/;
10
+
11
+ function validateRoleName(role: string): string {
12
+ if (!PLAIN_IDENTIFIER.test(role)) {
13
+ throw new Error(
14
+ `Invalid role name ${JSON.stringify(role)}: role names must be plain SQL identifiers matching ^[A-Za-z_][A-Za-z0-9_$]*$`,
15
+ );
16
+ }
17
+ return role;
18
+ }
19
+
20
+ export async function createRlsPolicy(
21
+ schemaName: string,
22
+ tableName: string,
23
+ policy: PostgresRlsPolicy,
24
+ lowerer: ExecuteRequestLowerer,
25
+ ): Promise<Op> {
26
+ const validatedRoles = policy.roles.map(validateRoleName);
27
+ const ddlNode = createPolicy({
28
+ schema: schemaName,
29
+ table: tableName,
30
+ name: policy.name,
31
+ permissive: policy.permissive,
32
+ operation: policy.operation,
33
+ roles: validatedRoles,
34
+ ...ifDefined('using', policy.using),
35
+ ...ifDefined('withCheck', policy.withCheck),
36
+ });
37
+ const checks = rlsPolicyExistsAst({
38
+ schema: schemaName,
39
+ table: tableName,
40
+ policyName: policy.name,
41
+ });
42
+ const absent = await lowerer.lowerToExecuteRequest(checks.policyAbsent());
43
+ const execute = await lowerer.lowerToExecuteRequest(ddlNode);
44
+ const present = await lowerer.lowerToExecuteRequest(checks.policyPresent());
45
+ return {
46
+ id: `rlsPolicy.${schemaName}.${tableName}.${policy.name}`,
47
+ label: `Create RLS policy "${policy.name}" on "${tableName}"`,
48
+ operationClass: 'additive',
49
+ target: targetDetails('rlsPolicy', policy.name, schemaName, tableName),
50
+ precheck: [
51
+ step(`ensure RLS policy "${policy.name}" does not exist`, absent.sql, absent.params),
52
+ ],
53
+ execute: [step(`create RLS policy "${policy.name}"`, execute.sql, execute.params)],
54
+ postcheck: [step(`verify RLS policy "${policy.name}" exists`, present.sql, present.params)],
55
+ };
56
+ }
57
+
58
+ export async function dropRlsPolicy(
59
+ schemaName: string,
60
+ tableName: string,
61
+ policyName: string,
62
+ lowerer: ExecuteRequestLowerer,
63
+ ): Promise<Op> {
64
+ const ddlNode = dropPolicy({ schema: schemaName, table: tableName, name: policyName });
65
+ const checks = rlsPolicyExistsAst({ schema: schemaName, table: tableName, policyName });
66
+ const present = await lowerer.lowerToExecuteRequest(checks.policyPresent());
67
+ const execute = await lowerer.lowerToExecuteRequest(ddlNode);
68
+ const absent = await lowerer.lowerToExecuteRequest(checks.policyAbsent());
69
+ return {
70
+ id: `rlsPolicy.${schemaName}.${tableName}.${policyName}.drop`,
71
+ label: `Drop RLS policy "${policyName}" on "${tableName}"`,
72
+ operationClass: 'destructive',
73
+ target: targetDetails('rlsPolicy', policyName, schemaName, tableName),
74
+ precheck: [step(`ensure RLS policy "${policyName}" exists`, present.sql, present.params)],
75
+ execute: [step(`drop RLS policy "${policyName}"`, execute.sql, execute.params)],
76
+ postcheck: [step(`verify RLS policy "${policyName}" is absent`, absent.sql, absent.params)],
77
+ };
78
+ }
79
+
80
+ export async function enableRowLevelSecurity(
81
+ schemaName: string,
82
+ tableName: string,
83
+ lowerer: ExecuteRequestLowerer,
84
+ ): Promise<Op> {
85
+ const checks = rlsEnabledAst(schemaName, tableName);
86
+ const disabled = await lowerer.lowerToExecuteRequest(checks.rlsDisabled());
87
+ const enabled = await lowerer.lowerToExecuteRequest(checks.rlsEnabled());
88
+ return {
89
+ id: `rowLevelSecurity.${schemaName}.${tableName}`,
90
+ label: `Enable row-level security on "${tableName}"`,
91
+ operationClass: 'additive',
92
+ target: targetDetails('rowLevelSecurity', tableName, schemaName),
93
+ precheck: [
94
+ step(`check RLS is not already enabled on "${tableName}"`, disabled.sql, disabled.params),
95
+ ],
96
+ execute: [
97
+ step(
98
+ `enable row-level security on "${tableName}"`,
99
+ `ALTER TABLE ${qualifyTableName(schemaName, tableName)} ENABLE ROW LEVEL SECURITY`,
100
+ ),
101
+ ],
102
+ postcheck: [
103
+ step(`verify row-level security is enabled on "${tableName}"`, enabled.sql, enabled.params),
104
+ ],
105
+ };
106
+ }
@@ -1,30 +1,10 @@
1
1
  import type { SqlMigrationPlanOperation } from '@prisma-next/family-sql/control';
2
2
  import type { ReferentialAction } from '@prisma-next/sql-contract/types';
3
3
  import { ifDefined } from '@prisma-next/utils/defined';
4
- import { quoteIdentifier } from '../../sql-utils';
5
4
  import type { OperationClass, PostgresPlanTargetDetails } from '../planner-target-details';
6
5
 
7
6
  export type Op = SqlMigrationPlanOperation<PostgresPlanTargetDetails>;
8
7
 
9
- /**
10
- * Literal-args shape for a column definition consumed by `addColumn` and
11
- * similar operations. Fully materialized: codec expansion and default
12
- * rendering have already happened in the wrapper.
13
- *
14
- * - `typeSql` is the column's DDL type string (e.g. `"integer"`, `"SERIAL"`,
15
- * `"varchar(100)"`), already produced by `buildColumnTypeSql` in the
16
- * call-factory wrapper.
17
- * - `defaultSql` is the full `DEFAULT …` clause (e.g. `"DEFAULT 42"`) or an
18
- * empty string when the column has no default, matching
19
- * `buildColumnDefaultSql`'s output.
20
- */
21
- export interface ColumnSpec {
22
- readonly name: string;
23
- readonly typeSql: string;
24
- readonly defaultSql: string;
25
- readonly nullable: boolean;
26
- }
27
-
28
8
  /**
29
9
  * Literal-args shape for a foreign key definition. `references.schema`
30
10
  * carries the target table's namespace (schema) coordinate so the rendered
@@ -57,13 +37,3 @@ export function targetDetails(
57
37
  details: { schema, objectType, name, ...ifDefined('table', table) },
58
38
  };
59
39
  }
60
-
61
- export function renderColumnDefinition(column: ColumnSpec): string {
62
- const parts = [
63
- quoteIdentifier(column.name),
64
- column.typeSql,
65
- column.defaultSql,
66
- column.nullable ? '' : 'NOT NULL',
67
- ].filter(Boolean);
68
- return parts.join(' ');
69
- }
@@ -1,43 +1,10 @@
1
1
  import type { CodecControlHooks } from '@prisma-next/family-sql/control';
2
- import type {
3
- StorageColumn,
4
- StorageTable,
5
- StorageTypeInstance,
6
- } from '@prisma-next/sql-contract/types';
2
+ import type { StorageColumn, StorageTypeInstance } from '@prisma-next/sql-contract/types';
7
3
  import { ifDefined } from '@prisma-next/utils/defined';
8
4
  import { escapeLiteral, quoteIdentifier } from '../sql-utils';
9
5
  import type { PostgresColumnDefault } from '../types';
10
6
  import { resolveColumnTypeMetadata } from './planner-type-resolution';
11
7
 
12
- export function buildCreateTableSql(
13
- qualifiedTableName: string,
14
- table: StorageTable,
15
- codecHooks: ReadonlyMap<string, CodecControlHooks>,
16
- storageTypes: Record<string, StorageTypeInstance> = {},
17
- ): string {
18
- const columnDefinitions = Object.entries(table.columns).map(
19
- ([columnName, column]: [string, StorageColumn]) => {
20
- const parts = [
21
- quoteIdentifier(columnName),
22
- buildColumnTypeSql(column, codecHooks, storageTypes),
23
- buildColumnDefaultSql(column.default, column),
24
- column.nullable ? '' : 'NOT NULL',
25
- ].filter(Boolean);
26
- return parts.join(' ');
27
- },
28
- );
29
-
30
- const constraintDefinitions: string[] = [];
31
- if (table.primaryKey) {
32
- constraintDefinitions.push(
33
- `PRIMARY KEY (${table.primaryKey.columns.map(quoteIdentifier).join(', ')})`,
34
- );
35
- }
36
-
37
- const allDefinitions = [...columnDefinitions, ...constraintDefinitions];
38
- return `CREATE TABLE ${qualifiedTableName} (\n ${allDefinitions.join(',\n ')}\n)`;
39
- }
40
-
41
8
  /**
42
9
  * Pattern for safe PostgreSQL type names.
43
10
  * Allows letters, digits, underscores, spaces (for "double precision", "character varying"),
@@ -100,15 +67,16 @@ export function buildColumnTypeSql(
100
67
 
101
68
  const expanded = expandParameterizedTypeSql(resolved, codecHooks);
102
69
  if (expanded !== null) {
103
- return expanded;
70
+ return column.many ? `${expanded}[]` : expanded;
104
71
  }
105
72
 
106
73
  if (column.typeRef) {
107
- return quoteIdentifier(resolved.nativeType);
74
+ const base = quoteIdentifier(resolved.nativeType);
75
+ return column.many ? `${base}[]` : base;
108
76
  }
109
77
 
110
78
  assertSafeNativeType(resolved.nativeType);
111
- return resolved.nativeType;
79
+ return column.many ? `${resolved.nativeType}[]` : resolved.nativeType;
112
80
  }
113
81
 
114
82
  function expandParameterizedTypeSql(
@@ -174,6 +142,10 @@ export function buildColumnDefaultSql(
174
142
  export function renderDefaultLiteral(value: unknown, column?: StorageColumn): string {
175
143
  const isJsonColumn = column?.nativeType === 'json' || column?.nativeType === 'jsonb';
176
144
 
145
+ if (column?.many && Array.isArray(value)) {
146
+ return renderArrayLiteralDefault(value);
147
+ }
148
+
177
149
  if (value instanceof Date) {
178
150
  return `'${escapeLiteral(value.toISOString())}'`;
179
151
  }
@@ -193,23 +165,10 @@ export function renderDefaultLiteral(value: unknown, column?: StorageColumn): st
193
165
  return `'${escapeLiteral(json)}'`;
194
166
  }
195
167
 
196
- export function buildAddColumnSql(
197
- qualifiedTableName: string,
198
- columnName: string,
199
- column: StorageColumn,
200
- codecHooks: ReadonlyMap<string, CodecControlHooks>,
201
- temporaryDefault?: string | null,
202
- storageTypes: Record<string, StorageTypeInstance> = {},
203
- ): string {
204
- const typeSql = buildColumnTypeSql(column, codecHooks, storageTypes);
205
- const defaultSql =
206
- buildColumnDefaultSql(column.default, column) ||
207
- (temporaryDefault ? `DEFAULT ${temporaryDefault}` : '');
208
- const parts = [
209
- `ALTER TABLE ${qualifiedTableName}`,
210
- `ADD COLUMN ${quoteIdentifier(columnName)} ${typeSql}`,
211
- defaultSql,
212
- column.nullable ? '' : 'NOT NULL',
213
- ].filter(Boolean);
214
- return parts.join(' ');
168
+ function renderArrayLiteralDefault(elements: unknown[]): string {
169
+ if (elements.length === 0) {
170
+ return "'{}'";
171
+ }
172
+ const rendered = elements.map((el) => renderDefaultLiteral(el)).join(', ');
173
+ return `ARRAY[${rendered}]`;
215
174
  }
@@ -1,15 +1,18 @@
1
1
  import type { CodecControlHooks, SqlMigrationPlanOperation } from '@prisma-next/family-sql/control';
2
2
  import type { ExecuteRequestLowerer } from '@prisma-next/family-sql/control-adapter';
3
3
  import type { StorageColumn, StorageTypeInstance } from '@prisma-next/sql-contract/types';
4
+ import { FunctionColumnDefault } from '@prisma-next/sql-relational-core/ast';
5
+ import { col } from '@prisma-next/sql-relational-core/contract-free';
6
+ import { ifDefined } from '@prisma-next/utils/defined';
4
7
  import {
5
8
  columnDefaultAst,
6
9
  columnExistsAst,
7
10
  columnNullabilityAst,
8
11
  } from '../../contract-free/checks';
9
- import { quoteIdentifier } from '../sql-utils';
12
+ import * as contractFreeDdl from '../../contract-free/ddl';
13
+ import { boundSchema } from './bound-schema';
10
14
  import { step } from './operations/shared';
11
- import { buildAddColumnSql } from './planner-ddl-builders';
12
- import { qualifyTableName } from './planner-sql-checks';
15
+ import { buildColumnTypeSql } from './planner-ddl-builders';
13
16
  import { buildTargetDetails, type PostgresPlanTargetDetails } from './planner-target-details';
14
17
 
15
18
  export function buildAddColumnOperationIdentity(
@@ -51,7 +54,29 @@ export async function buildAddNotNullColumnWithTemporaryDefaultOperation(options
51
54
  temporaryDefault,
52
55
  lowerer,
53
56
  } = options;
54
- const qualified = qualifyTableName(schema, tableName);
57
+
58
+ // The recipe handles NOT NULL columns that carry no contract default, so the
59
+ // temporary backfill value is the only default. It is a pre-rendered SQL
60
+ // fragment (e.g. `''`, `0`, `'{}'::jsonb`), carried verbatim as a
61
+ // `FunctionColumnDefault` so the adapter emits it as a `DEFAULT (...)` clause.
62
+ const ddlColumn = col(columnName, buildColumnTypeSql(column, codecHooks, storageTypes), {
63
+ notNull: true,
64
+ default: new FunctionColumnDefault(temporaryDefault),
65
+ });
66
+ const addColumn = await lowerer.lowerToExecuteRequest(
67
+ contractFreeDdl.alterTable({
68
+ ...ifDefined('schema', boundSchema(schema)),
69
+ table: tableName,
70
+ actions: [contractFreeDdl.addColumnAction(ddlColumn)],
71
+ }),
72
+ );
73
+ const dropTempDefault = await lowerer.lowerToExecuteRequest(
74
+ contractFreeDdl.alterTable({
75
+ ...ifDefined('schema', boundSchema(schema)),
76
+ table: tableName,
77
+ actions: [contractFreeDdl.dropDefaultAction(columnName)],
78
+ }),
79
+ );
55
80
 
56
81
  const absent = await lowerer.lowerToExecuteRequest(
57
82
  columnExistsAst({ schema, table: tableName, column: columnName }).columnAbsent(),
@@ -73,18 +98,11 @@ export async function buildAddNotNullColumnWithTemporaryDefaultOperation(options
73
98
  execute: [
74
99
  {
75
100
  description: `add column "${columnName}"`,
76
- sql: buildAddColumnSql(
77
- qualified,
78
- columnName,
79
- column,
80
- codecHooks,
81
- temporaryDefault,
82
- storageTypes,
83
- ),
101
+ sql: addColumn.sql,
84
102
  },
85
103
  {
86
104
  description: `drop temporary default from column "${columnName}"`,
87
- sql: `ALTER TABLE ${qualified} ALTER COLUMN ${quoteIdentifier(columnName)} DROP DEFAULT`,
105
+ sql: dropTempDefault.sql,
88
106
  },
89
107
  ],
90
108
  postcheck: [
@@ -53,10 +53,10 @@ import {
53
53
  RawSqlCall,
54
54
  SetNotNullCall,
55
55
  } from './op-factory-call';
56
- import { buildAddColumnSql, buildColumnTypeSql } from './planner-ddl-builders';
56
+ import { buildColumnTypeSql } from './planner-ddl-builders';
57
57
  import { resolveIdentityValue } from './planner-identity-values';
58
58
  import { buildSchemaLookupMap, hasForeignKey, hasUniqueConstraint } from './planner-schema-lookup';
59
- import { buildExpectedFormatType, qualifyTableName } from './planner-sql-checks';
59
+ import { buildExpectedFormatType } from './planner-sql-checks';
60
60
  import { buildTargetDetails, type PostgresPlanTargetDetails } from './planner-target-details';
61
61
  import { resolveColumnTypeMetadata } from './planner-type-resolution';
62
62
 
@@ -600,20 +600,12 @@ export const notNullAddColumnCallStrategy: CallMigrationStrategy = (issues, ctx)
600
600
  continue;
601
601
  }
602
602
 
603
- const qualified = qualifyTableName(schemaForTable, issue.table);
604
603
  calls.push(
605
604
  new AddNotNullColumnDirectCall(
606
605
  schemaForTable,
607
606
  issue.table,
608
607
  issue.column,
609
- buildAddColumnSql(
610
- qualified,
611
- issue.column,
612
- column,
613
- mutableCodecHooks,
614
- undefined,
615
- mutableStorageTypes,
616
- ),
608
+ buildColumnSpec(namespaceId, issue.table, issue.column, ctx),
617
609
  ),
618
610
  );
619
611
  }
@@ -9,7 +9,9 @@ export type OperationClass =
9
9
  | 'unique'
10
10
  | 'index'
11
11
  | 'foreignKey'
12
- | 'checkConstraint';
12
+ | 'checkConstraint'
13
+ | 'rlsPolicy'
14
+ | 'rowLevelSecurity';
13
15
 
14
16
  export interface PostgresPlanTargetDetails {
15
17
  readonly schema: string;