@rebasepro/server-postgres 0.24.0 → 0.24.1-canary.gcb918fc

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 (172) hide show
  1. package/README.md +16 -0
  2. package/dist/{BranchService-BRt78gfa.js → BranchService-Bsn2wfaj.js} +3 -2
  3. package/dist/{BranchService-BRt78gfa.js.map → BranchService-Bsn2wfaj.js.map} +1 -1
  4. package/dist/PostgresBackendDriver.d.ts +91 -2
  5. package/dist/PostgresBootstrapper.d.ts +101 -2
  6. package/dist/auth/ensure-tables.d.ts +12 -0
  7. package/dist/auth/services.d.ts +6 -0
  8. package/dist/backup/backup-cli.d.ts +14 -0
  9. package/dist/backup/backup-service.d.ts +2 -2
  10. package/dist/backup/pg-binary.d.ts +5 -0
  11. package/dist/{backup-cli-DW5p9_zv.js → backup-cli-DnpJi6I7.js} +37 -29
  12. package/dist/backup-cli-DnpJi6I7.js.map +1 -0
  13. package/dist/{backup-service-EwcDVG-8.js → backup-service-BTBB7RXn.js} +55 -21
  14. package/dist/backup-service-BTBB7RXn.js.map +1 -0
  15. package/dist/boot-ddl-DVpsFN6r.js +105 -0
  16. package/dist/boot-ddl-DVpsFN6r.js.map +1 -0
  17. package/dist/cli-console-Ca6ZlAVU.js +154 -0
  18. package/dist/cli-console-Ca6ZlAVU.js.map +1 -0
  19. package/dist/cli-console.d.ts +39 -0
  20. package/dist/cli-errors-CaI4ntyD.js +1919 -0
  21. package/dist/cli-errors-CaI4ntyD.js.map +1 -0
  22. package/dist/cli-errors.d.ts +51 -5
  23. package/dist/{cli-flags-BglvpjHv.js → cli-flags-JNI-pwSf.js} +6 -3
  24. package/dist/cli-flags-JNI-pwSf.js.map +1 -0
  25. package/dist/cli-helpers.d.ts +40 -0
  26. package/dist/cli-output.d.ts +16 -28
  27. package/dist/cli-push.d.ts +87 -0
  28. package/dist/cli.js +566 -182
  29. package/dist/cli.js.map +1 -1
  30. package/dist/{column-plan-helpers-1-LQD0yI.js → column-plan-helpers-drZIsSNf.js} +10 -4
  31. package/dist/{column-plan-helpers-1-LQD0yI.js.map → column-plan-helpers-drZIsSNf.js.map} +1 -1
  32. package/dist/{databasePoolManager-Bj5FbeAs.js → connection-C9mT8bgf.js} +24 -112
  33. package/dist/connection-C9mT8bgf.js.map +1 -0
  34. package/dist/connection.d.ts +13 -2
  35. package/dist/databasePoolManager-BIuewidb.js +119 -0
  36. package/dist/databasePoolManager-BIuewidb.js.map +1 -0
  37. package/dist/{ensure-collection-tables-kkkHk8oo.js → deferred-index-build-DP1_SNFo.js} +484 -346
  38. package/dist/deferred-index-build-DP1_SNFo.js.map +1 -0
  39. package/dist/{doctor-C-sYWbmt.js → doctor-C4WiftHm.js} +63 -52
  40. package/dist/doctor-C4WiftHm.js.map +1 -0
  41. package/dist/ensure-collection-policies-CXpbeNag.js +541 -0
  42. package/dist/ensure-collection-policies-CXpbeNag.js.map +1 -0
  43. package/dist/ensure-collection-tables-BhsU6qUE.js +532 -0
  44. package/dist/ensure-collection-tables-BhsU6qUE.js.map +1 -0
  45. package/dist/{ensure-tables-BmI_tRxc.js → ensure-tables-CVm_yvzb.js} +173 -77
  46. package/dist/ensure-tables-CVm_yvzb.js.map +1 -0
  47. package/dist/enum-labels-kept-D8fI1MoX.js +56 -0
  48. package/dist/enum-labels-kept-D8fI1MoX.js.map +1 -0
  49. package/dist/generate-drizzle-schema-Bz52jH2h.js +57 -0
  50. package/dist/generate-drizzle-schema-Bz52jH2h.js.map +1 -0
  51. package/dist/{generate-drizzle-schema-logic-sSFDp6LR.js → generate-drizzle-schema-logic-B74WN37S.js} +5 -5
  52. package/dist/generate-drizzle-schema-logic-B74WN37S.js.map +1 -0
  53. package/dist/generate-files-CdEckX6c.js +199 -0
  54. package/dist/generate-files-CdEckX6c.js.map +1 -0
  55. package/dist/{generate-postgres-ddl-logic-BJsLaVNX.js → generate-postgres-ddl-logic-B64O2oZF.js} +3 -3
  56. package/dist/{generate-postgres-ddl-logic-BJsLaVNX.js.map → generate-postgres-ddl-logic-B64O2oZF.js.map} +1 -1
  57. package/dist/generated-sql.d.ts +8 -2
  58. package/dist/history/HistoryService.d.ts +64 -5
  59. package/dist/history/ensure-history-table.d.ts +63 -4
  60. package/dist/index.d.ts +1 -0
  61. package/dist/index.es.js +139 -19236
  62. package/dist/index.es.js.map +1 -1
  63. package/dist/{introspect-db-logic-kCETE8TY.js → introspect-db-logic-C-Di8dA9.js} +44 -6
  64. package/dist/introspect-db-logic-C-Di8dA9.js.map +1 -0
  65. package/dist/{introspect-db-queries-C_Q5VgQw.js → introspect-db-queries-D6kAqReI.js} +23 -1
  66. package/dist/introspect-db-queries-D6kAqReI.js.map +1 -0
  67. package/dist/open-rebase-CbOur0TU.js +20858 -0
  68. package/dist/open-rebase-CbOur0TU.js.map +1 -0
  69. package/dist/open-rebase.d.ts +86 -0
  70. package/dist/pg-error-utils-vJezlXdI.js +388 -0
  71. package/dist/pg-error-utils-vJezlXdI.js.map +1 -0
  72. package/dist/{plan-schema-DU9exq6C.js → plan-schema-B1YF_tYq.js} +151 -41
  73. package/dist/plan-schema-B1YF_tYq.js.map +1 -0
  74. package/dist/{policy-drift-B-J2hhm0.js → policy-drift-Dm8i-s1J.js} +23 -6
  75. package/dist/policy-drift-Dm8i-s1J.js.map +1 -0
  76. package/dist/push-index-prebuild-Db4AQF4i.js +45 -0
  77. package/dist/push-index-prebuild-Db4AQF4i.js.map +1 -0
  78. package/dist/push-legacy-renames-BK6ilQq2.js +34 -0
  79. package/dist/push-legacy-renames-BK6ilQq2.js.map +1 -0
  80. package/dist/{render-ddl-Ds2t_d9V.js → render-ddl-CtC3y9Ol.js} +8 -4
  81. package/dist/render-ddl-CtC3y9Ol.js.map +1 -0
  82. package/dist/{rls-bootstrap-sql-_KNnjanK.js → rls-bootstrap-sql-RQUljnJ1.js} +125 -1270
  83. package/dist/rls-bootstrap-sql-RQUljnJ1.js.map +1 -0
  84. package/dist/{rls-enforcement-CfXOJJaW.js → rls-enforcement-DvzqlaL1.js} +129 -17
  85. package/dist/rls-enforcement-DvzqlaL1.js.map +1 -0
  86. package/dist/schema/auth-schema.d.ts +2 -2
  87. package/dist/schema/boot-ddl.d.ts +47 -0
  88. package/dist/schema/collection-index.d.ts +45 -0
  89. package/dist/schema/column-plan-helpers.d.ts +5 -0
  90. package/dist/schema/deferred-index-build.d.ts +131 -0
  91. package/dist/schema/destructive-sql.d.ts +7 -0
  92. package/dist/schema/doctor-cli.js +110 -6
  93. package/dist/schema/doctor-cli.js.map +1 -1
  94. package/dist/schema/doctor-search-scale.d.ts +45 -0
  95. package/dist/schema/doctor.d.ts +20 -1
  96. package/dist/schema/ensure-collection-policies.d.ts +73 -35
  97. package/dist/schema/ensure-collection-tables.d.ts +56 -3
  98. package/dist/schema/enum-labels-kept.d.ts +26 -0
  99. package/dist/schema/generate-drizzle-schema.js +1 -1
  100. package/dist/schema/generate-files.d.ts +44 -0
  101. package/dist/schema/generate-next-step.d.ts +0 -13
  102. package/dist/schema/generate-postgres-ddl.js +10 -81
  103. package/dist/schema/generate-postgres-ddl.js.map +1 -1
  104. package/dist/schema/introspect-db-logic.d.ts +12 -0
  105. package/dist/schema/introspect-db-queries.d.ts +11 -1
  106. package/dist/schema/introspect-db-structure.d.ts +10 -0
  107. package/dist/schema/introspect-db.js +4 -5
  108. package/dist/schema/introspect-db.js.map +1 -1
  109. package/dist/schema/migration-post-process.d.ts +34 -0
  110. package/dist/schema/plan/diff-plan.d.ts +126 -0
  111. package/dist/schema/plan/types.d.ts +8 -0
  112. package/dist/schema/push-index-prebuild.d.ts +25 -0
  113. package/dist/schema/push-legacy-renames.d.ts +20 -0
  114. package/dist/schema/rls-bootstrap-sql.d.ts +15 -0
  115. package/dist/security/rls-enforcement.d.ts +32 -4
  116. package/dist/services/FetchService.d.ts +58 -8
  117. package/dist/services/PersistService.d.ts +15 -0
  118. package/dist/services/RelationService.d.ts +88 -3
  119. package/dist/services/cdc/CdcListener.d.ts +17 -6
  120. package/dist/services/cdc/trigger-cdc.d.ts +151 -27
  121. package/dist/services/channel-bus/index.d.ts +1 -1
  122. package/dist/services/channel-history.d.ts +15 -4
  123. package/dist/services/collection-helpers.d.ts +15 -0
  124. package/dist/services/field-viewer.d.ts +15 -1
  125. package/dist/services/pg-notify-listener.d.ts +7 -0
  126. package/dist/services/realtimeService.d.ts +314 -14
  127. package/dist/services/row-pipeline.d.ts +16 -9
  128. package/dist/services/socket-liveness.d.ts +42 -2
  129. package/dist/services/sql-script.d.ts +207 -3
  130. package/dist/services/vector-iterative-scan.d.ts +40 -0
  131. package/dist/services/write-depth.d.ts +6 -0
  132. package/dist/services/write-transaction-scope.d.ts +9 -0
  133. package/dist/socket-opt-ins.d.ts +64 -0
  134. package/dist/source-DBDbBTL4.js +811 -0
  135. package/dist/source-DBDbBTL4.js.map +1 -0
  136. package/dist/tsx-loader.d.ts +14 -0
  137. package/dist/utils/drizzle-conditions.d.ts +21 -1
  138. package/dist/utils/error-cause.d.ts +7 -0
  139. package/dist/utils/pg-error-utils.d.ts +2 -4
  140. package/dist/utils/result-rows.d.ts +10 -0
  141. package/dist/websocket-BLGc5yhs.js +1604 -0
  142. package/dist/websocket-BLGc5yhs.js.map +1 -0
  143. package/dist/websocket.d.ts +13 -0
  144. package/package.json +7 -7
  145. package/dist/backup-cli-DW5p9_zv.js.map +0 -1
  146. package/dist/backup-service-EwcDVG-8.js.map +0 -1
  147. package/dist/cli-errors-DsA-K9uP.js +0 -893
  148. package/dist/cli-errors-DsA-K9uP.js.map +0 -1
  149. package/dist/cli-flags-BglvpjHv.js.map +0 -1
  150. package/dist/cli-output-CNdMql-L.js +0 -47
  151. package/dist/cli-output-CNdMql-L.js.map +0 -1
  152. package/dist/collection-index-DdnsxiJ_.js +0 -286
  153. package/dist/collection-index-DdnsxiJ_.js.map +0 -1
  154. package/dist/databasePoolManager-Bj5FbeAs.js.map +0 -1
  155. package/dist/doctor-C-sYWbmt.js.map +0 -1
  156. package/dist/ensure-collection-policies-B1ureSIV.js +0 -160
  157. package/dist/ensure-collection-policies-B1ureSIV.js.map +0 -1
  158. package/dist/ensure-collection-tables-kkkHk8oo.js.map +0 -1
  159. package/dist/ensure-tables-BmI_tRxc.js.map +0 -1
  160. package/dist/generate-drizzle-schema-93M0lUxK.js +0 -123
  161. package/dist/generate-drizzle-schema-93M0lUxK.js.map +0 -1
  162. package/dist/generate-drizzle-schema-logic-sSFDp6LR.js.map +0 -1
  163. package/dist/introspect-db-logic-kCETE8TY.js.map +0 -1
  164. package/dist/introspect-db-queries-C_Q5VgQw.js.map +0 -1
  165. package/dist/plan-schema-DU9exq6C.js.map +0 -1
  166. package/dist/policy-drift-B-J2hhm0.js.map +0 -1
  167. package/dist/render-ddl-Ds2t_d9V.js.map +0 -1
  168. package/dist/rls-bootstrap-sql-_KNnjanK.js.map +0 -1
  169. package/dist/rls-enforcement-CfXOJJaW.js.map +0 -1
  170. package/dist/source-Br7L7GOI.js +0 -410
  171. package/dist/source-Br7L7GOI.js.map +0 -1
  172. package/dist/utils/sql-redaction.d.ts +0 -21
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli-errors-CaI4ntyD.js","names":[],"sources":["../src/utils/error-cause.ts","../src/schema/destructive-sql.ts","../src/module-dir.ts","../src/generated-sql.ts","../src/cli-helpers.ts","../src/cli-errors.ts"],"sourcesContent":["/**\n * Walk the error cause chain and return the deepest meaningful message.\n *\n * A leaf module, imported by the driver CLI: `pg-error-utils.ts`, where it used\n * to live, imports `@rebasepro/server`, and the CLI must start without it.\n */\nexport function extractCauseMessage(error: unknown): string | null {\n if (!error || typeof error !== \"object\") return null;\n if (!(error instanceof Error)) return null;\n\n if (error.cause && typeof error.cause === \"object\") {\n const deeper = extractCauseMessage(error.cause);\n if (deeper) return deeper;\n // The cause itself has a message\n if (error.cause instanceof Error && error.cause.message) {\n return error.cause.message;\n }\n }\n return null;\n}\n","/**\n * Pure helpers that classify an Atlas declarative-apply plan as destructive\n * or not, and decide what `rebase db push` should do about it.\n *\n * `db push` runs `atlas schema apply` to make the live database match the\n * generated `schema.sql`. Removing a collection field compiles to\n * `DROP COLUMN`; renaming compiles to drop-then-add — either destroys data.\n * We first run the apply with `--dry-run` to obtain the planned SQL, scan it\n * here, and refuse to auto-approve anything destructive without an explicit\n * opt-in.\n *\n * A type change is a destructive plan that names no DROP. `ALTER COLUMN\n * \"price\" TYPE integer` rounds every 19.99 to 20, and `TYPE date` throws away\n * every timestamp's time of day, in a statement that reads like a routine\n * alter. So a type change is gated too, unless it is a widening every value\n * survives unchanged — which needs the column's current type, read from the\n * catalogue by the caller.\n *\n * A primary key change names no DROP of data either: `DROP CONSTRAINT\n * \"<table>_pkey\", ADD PRIMARY KEY (\"id\", \"locale\")` keeps every value and\n * gives every row a new address. It is gated like a drop.\n *\n * Everything in this file is side-effect free so it can be unit-tested\n * without Atlas or a database.\n */\n\n/**\n * SQL fragments that destroy data or data-bearing objects. Matched\n * case-insensitively against each statement of the plan. `IF EXISTS` /\n * whitespace variations are tolerated by the regexes below.\n */\nconst DESTRUCTIVE_PATTERNS: { label: string; re: RegExp }[] = [\n { label: \"DROP TABLE\", re: /\\bDROP\\s+TABLE\\b/i },\n { label: \"DROP COLUMN\", re: /\\bDROP\\s+COLUMN\\b/i },\n { label: \"DROP SCHEMA\", re: /\\bDROP\\s+SCHEMA\\b/i },\n { label: \"DROP VIEW\", re: /\\bDROP\\s+(MATERIALIZED\\s+)?VIEW\\b/i },\n { label: \"DROP TYPE\", re: /\\bDROP\\s+TYPE\\b/i },\n { label: \"TRUNCATE\", re: /\\bTRUNCATE\\b/i }\n];\n\n/**\n * Split a SQL script into individual statements, dropping blank lines and\n * `--` comment lines. Deliberately simple: Atlas emits one plain statement\n * per `;`, without string literals that contain semicolons in a schema DDL\n * plan, so a naive split is safe and keeps this dependency-free.\n */\nexport function splitSqlStatements(sql: string): string[] {\n // Strip full-line SQL comments so a commented-out DROP never trips the\n // detector, then split on semicolons.\n const withoutComments = sql\n .split(\"\\n\")\n .filter((line) => !line.trim().startsWith(\"--\"))\n .join(\"\\n\");\n return withoutComments\n .split(\";\")\n .map((s) => s.trim())\n .filter((s) => s.length > 0);\n}\n\n/** `->`, the marker Atlas prints in front of the first line of every statement. */\nconst PLAN_MARKER_RE = /^\\s*->\\s?/;\n/** `-------`, both the rule between sections and the one above a summary. */\nconst PLAN_RULE_RE = /^\\s*-{5,}\\s*$/;\n/** `Planning migration statements (3 in total):` — and the count it announces. */\nconst PLAN_HEADING_RE = /^Planning migration statements \\((\\d+) in total\\):/m;\n/**\n * What Atlas prints, and nothing else, when there is nothing to plan — from\n * `schema apply`, or from `migrate apply` with no file pending.\n */\nconst PLAN_SYNCED_RE = /^\\s*(?:Schema is synced, no changes to be made|No migration files to execute)\\s*$/;\n\n/** What {@link readPlan} could make of a plan. */\ninterface ReadPlan {\n statements: string[];\n /** The `(N in total)` Atlas announced, when its report said one. */\n announced?: number;\n}\n\n/**\n * Read the statements out of whatever Atlas printed.\n *\n * `atlas schema apply --dry-run` does not print SQL. It prints a report, and\n * the same statements appear in it twice:\n *\n * Planning migration statements (2 in total):\n *\n * -- modify \"people\" table:\n * -> ALTER TABLE \"public\".\"people\" ALTER COLUMN \"rating\" TYPE numeric(4,1);\n * -- create \"notes\" table:\n * -> CREATE TABLE \"public\".\"notes\" (\n * \"body\" text NULL DEFAULT 'a;b',\n * …\n * );\n *\n * -------------------------------------------\n *\n * Analyzing planned statements (2 in total):\n * …\n * Running dry-run migration (2 statements in total):\n * -- modify \"people\" table\n * -> ALTER TABLE …; ← the same statement, again\n * -- ok (19µs)\n *\n * Splitting that on `;` is how the push gate went blind: the heading has no\n * `;` and does not start with `--`, so it became the first line of the first\n * statement, and an `ALTER TABLE` parse anchored at the start of a statement\n * read nothing — every narrowing type change applied without a prompt. Reading\n * both sections was a second bug, counting every destructive statement twice.\n *\n * So the report is read as a report: a statement is a `->` line and the lines\n * indented under it, up to the next `--` heading, `->`, blank line or rule;\n * only the first section that holds a `->` is read; and `;` plays no part,\n * because a default like `'a;b'` contains one.\n *\n * Plain SQL — a migration file, or an Atlas that printed SQL — has no `->`\n * and is split the old way.\n */\nfunction readPlan(plan: string): ReadPlan {\n if (PLAN_SYNCED_RE.test(plan)) return { statements: [] };\n const lines = plan.split(/\\r?\\n/);\n if (!lines.some(line => PLAN_MARKER_RE.test(line))) return { statements: splitSqlStatements(plan) };\n\n const heading = PLAN_HEADING_RE.exec(plan);\n const statements: string[] = [];\n let current: string[] | null = null;\n const flush = () => {\n if (current) statements.push(current.join(\"\\n\").trim().replace(/;\\s*$/, \"\").trimEnd());\n current = null;\n };\n\n for (const line of lines) {\n if (PLAN_MARKER_RE.test(line)) {\n flush();\n current = [line.replace(PLAN_MARKER_RE, \"\")];\n continue;\n }\n // A section heading sits in column 0 and is neither a marker nor a\n // `--` line; the rule between sections is dashes. Either one after a\n // statement has been read means the plan is over and what follows is\n // Atlas re-printing it.\n const sectionEnds = PLAN_RULE_RE.test(line) || (/^\\S/.test(line) && !line.startsWith(\"--\"));\n if (sectionEnds) {\n flush();\n if (statements.length > 0) break;\n continue;\n }\n if (!current) continue;\n if (line.trim() === \"\" || line.trim().startsWith(\"--\")) {\n flush();\n continue;\n }\n current.push(line);\n }\n flush();\n\n return { statements, announced: heading ? Number(heading[1]) : undefined };\n}\n\n/**\n * The statements of an Atlas plan, each exactly once, without Atlas's\n * rendering and without the trailing `;`. See {@link readPlan} for the shape\n * it reads; `test/fixtures/atlas-dry-run/` holds real captures of it.\n */\nexport function extractPlanStatements(plan: string): string[] {\n return readPlan(plan).statements;\n}\n\n/**\n * Whether Atlas answered the dry-run with \"Schema is synced\" and nothing else.\n *\n * Positive evidence only: an empty or unreadable report also yields no\n * statements, and is not the same answer.\n */\nexport function isSyncedPlan(plan: string): boolean {\n return PLAN_SYNCED_RE.test(plan);\n}\n\nexport interface DestructiveStatement {\n /** The offending statement (trimmed, without the trailing `;`). */\n statement: string;\n /** Which destructive operation it was flagged for, e.g. \"DROP COLUMN\". */\n kind: string;\n /**\n * What in the statement loses data, when the kind alone does not say:\n * for `ALTER COLUMN TYPE`, each lossy column with its old and new type.\n */\n detail?: string;\n}\n\n/** A column as the database has it now, with its type as `format_type` spells it. */\nexport interface ExistingColumnType {\n schema: string;\n table: string;\n column: string;\n /** e.g. `character varying(255)`, `numeric(10,2)`, `timestamp with time zone`. */\n type: string;\n}\n\n/**\n * Every column of every ordinary table outside the system schemas, with its\n * type. What {@link detectDestructiveStatements} compares a planned type\n * change against; `format_type` because it carries the modifier (`(255)`,\n * `(10,2)`) that decides whether a change widens or narrows.\n */\nexport const COLUMN_TYPES_SQL = `\n SELECT n.nspname AS \"schema\",\n c.relname AS \"table\",\n a.attname AS \"column\",\n format_type(a.atttypid, a.atttypmod) AS \"type\"\n FROM pg_attribute a\n JOIN pg_class c ON c.oid = a.attrelid\n JOIN pg_namespace n ON n.oid = c.relnamespace\n WHERE c.relkind IN ('r', 'p')\n AND a.attnum > 0\n AND NOT a.attisdropped\n AND n.nspname NOT IN ('pg_catalog', 'information_schema')\n AND n.nspname NOT LIKE 'pg\\\\_%'\n`;\n\n/**\n * Scan an Atlas plan (the SQL printed by `schema apply --dry-run`) and return\n * the statements that would destroy data. An empty array means the plan is\n * safe to auto-approve.\n *\n * `columnTypes` is what the database holds now. A type change to a column it\n * does not list is flagged: not knowing what a column is means not knowing\n * whether the change keeps its values.\n */\nexport function detectDestructiveStatements(\n planSql: string,\n columnTypes: readonly ExistingColumnType[] = []\n): DestructiveStatement[] {\n const found: DestructiveStatement[] = [];\n const { statements, announced } = readPlan(planSql);\n // Fail closed on a plan this could not read. Atlas says how many statements\n // it planned; reading a different number means its rendering changed under\n // us, and a gate that has not read the plan cannot call it safe. Reported\n // as one more destructive finding, so it takes the same road: a prompt on\n // a terminal, a refusal elsewhere, `--allow-destructive` to go ahead.\n if (announced !== undefined && announced !== statements.length) {\n found.push({\n statement: `Planning migration statements (${announced} in total)`,\n kind: \"UNREADABLE PLAN\",\n detail: `Atlas announced ${announced} statement(s) and ${statements.length} could be read — `\n + \"read the plan yourself before allowing it\"\n });\n }\n for (const statement of statements) {\n // One finding per statement, under the label that says the most, with\n // everything else it does in the detail: Atlas folds a key change, a\n // retype and a drop into one `ALTER TABLE`, and a reader shown only\n // the first would approve the rest unread.\n const dropped = DESTRUCTIVE_PATTERNS.find(({ re }) => re.test(statement));\n const rekeyed = describePrimaryKeyChange(statement);\n const lossy = parseColumnTypeChanges(statement)\n .map(change => ({ change, from: currentTypeOf(change, columnTypes) }))\n .filter(({ change, from }) => from === undefined || !isLosslessTypeChange(from, change.to))\n .map(({ change, from }) => `\"${change.column}\" ${from ?? \"(current type unknown)\"} → ${change.to}`)\n .join(\"; \");\n const kind = dropped?.label ?? (rekeyed ? \"PRIMARY KEY CHANGE\" : lossy ? \"ALTER COLUMN TYPE\" : undefined);\n if (!kind) continue;\n const detail = [rekeyed, lossy].filter(Boolean).join(\"; \");\n found.push(detail ? { statement, kind, detail } : { statement, kind });\n }\n return found;\n}\n\n/** A column an `ALTER TABLE` retypes, with the type the plan gives it. */\ninterface ColumnTypeChange {\n /** Absent when the plan did not qualify the table. */\n schema?: string;\n table: string;\n column: string;\n /** As the plan writes it, without any `USING` or `COLLATE`. */\n to: string;\n}\n\nconst IDENT = String.raw`(?:\"(?:[^\"]|\"\")+\"|[A-Za-z_][A-Za-z0-9_$]*)`;\nconst ALTER_TABLE_RE = new RegExp(\n String.raw`^ALTER\\s+TABLE\\s+(?:IF\\s+EXISTS\\s+)?(?:ONLY\\s+)?(${IDENT})(?:\\.(${IDENT}))?\\s+([\\s\\S]*)$`, \"i\"\n);\nconst TYPE_CLAUSE_RE = new RegExp(\n String.raw`^ALTER\\s+(?:COLUMN\\s+)?(${IDENT})\\s+(?:SET\\s+DATA\\s+)?TYPE\\s+([\\s\\S]+)$`, \"i\"\n);\n\nconst unquote = (ident: string): string =>\n ident.startsWith(\"\\\"\") ? ident.slice(1, -1).replace(/\"\"/g, \"\\\"\") : ident;\n\n/**\n * Split an `ALTER TABLE`'s clause list on its top-level commas — the ones\n * outside parentheses and quotes, so `TYPE numeric(10, 2)` stays one clause.\n */\nfunction splitClauses(body: string): string[] {\n const clauses: string[] = [];\n let depth = 0;\n let quote: string | undefined;\n let start = 0;\n for (let i = 0; i < body.length; i++) {\n const ch = body[i];\n if (quote) {\n if (ch === quote) quote = undefined;\n } else if (ch === \"\\\"\" || ch === \"'\") {\n quote = ch;\n } else if (ch === \"(\") {\n depth++;\n } else if (ch === \")\") {\n depth--;\n } else if (ch === \",\" && depth === 0) {\n clauses.push(body.slice(start, i).trim());\n start = i + 1;\n }\n }\n clauses.push(body.slice(start).trim());\n return clauses.filter(clause => clause.length > 0);\n}\n\n/**\n * The statement from its `ALTER TABLE` on, wherever on a line that starts.\n *\n * {@link readPlan} hands over clean statements. This is the second line of\n * defence for text it could not clean — a rendering it does not know yet —\n * where a line of prose above the statement must not hide the statement, which\n * is exactly how an anchored match once read Atlas's whole plan as safe.\n */\nexport function fromAlterTable(statement: string): string {\n const start = /(?:^|\\n)[ \\t]*(?:->[ \\t]*)?(ALTER\\s+TABLE\\b)/i.exec(statement);\n return start ? statement.slice(start.index + start[0].length - start[1].length).trim() : statement.trim();\n}\n\n/**\n * Every `ALTER COLUMN … [SET DATA] TYPE` in one statement. Atlas prints its\n * dry-run statements under a `-> ` marker and puts several clauses in one\n * `ALTER TABLE`, so both are handled.\n */\nfunction parseColumnTypeChanges(statement: string): ColumnTypeChange[] {\n const table = ALTER_TABLE_RE.exec(fromAlterTable(statement));\n if (!table) return [];\n const [schema, name] = table[2]\n ? [unquote(table[1]), unquote(table[2])]\n : [undefined, unquote(table[1])];\n const changes: ColumnTypeChange[] = [];\n for (const clause of splitClauses(table[3])) {\n const match = TYPE_CLAUSE_RE.exec(clause);\n if (!match) continue;\n const to = match[2].split(/\\s+(?:USING|COLLATE)\\s/i)[0].trim();\n changes.push({ schema, table: name, column: unquote(match[1]), to });\n }\n return changes;\n}\n\n/** `ADD [CONSTRAINT \"x\"] PRIMARY KEY (\"a\", \"b\")`, one clause of an `ALTER TABLE`. */\nconst ADD_PRIMARY_KEY_RE = new RegExp(\n String.raw`^ADD\\s+(?:CONSTRAINT\\s+${IDENT}\\s+)?PRIMARY\\s+KEY\\b\\s*(?:\\(([^)]*)\\))?`, \"i\"\n);\n/** `DROP CONSTRAINT [IF EXISTS] \"x\"`, one clause of an `ALTER TABLE`. */\nconst DROP_CONSTRAINT_RE = new RegExp(\n String.raw`^DROP\\s+CONSTRAINT\\s+(?:IF\\s+EXISTS\\s+)?(${IDENT})`, \"i\"\n);\n\n/** What a row loses when the columns that address it change. */\nconst ROW_ADDRESS_LOST = \"every row's id changes, so anything that refers to a row by its id — a URL, \" +\n \"a foreign key, an id stored elsewhere — stops finding it\";\n\n/**\n * What an `ALTER TABLE` does to the table's primary key, in a sentence — or\n * `undefined` when it leaves the key alone.\n *\n * The key is a row's address: Rebase reads, links and routes every row by it.\n * Atlas plans a key change as `DROP CONSTRAINT \"<table>_pkey\", ADD PRIMARY KEY\n * (…)` — one routine-looking statement that names no DROP of data — so moving\n * a collection from `(id)` to `(id, locale)` re-keyed a populated table with\n * no prompt. A `CREATE TABLE`'s own `PRIMARY KEY` is not a change and is never\n * read here: only an `ALTER TABLE`'s clauses are. A dropped constraint counts\n * as the key when it carries Postgres's default name for one, `<table>_pkey`;\n * a key under another name is caught by the `ADD PRIMARY KEY` that replaces it.\n */\nfunction describePrimaryKeyChange(statement: string): string | undefined {\n const alter = ALTER_TABLE_RE.exec(fromAlterTable(statement));\n if (!alter) return undefined;\n const table = alter[2] ? `${alter[1]}.${alter[2]}` : alter[1];\n\n let added: string | undefined;\n let dropped: string | undefined;\n for (const clause of splitClauses(alter[3])) {\n const add = ADD_PRIMARY_KEY_RE.exec(clause);\n if (add) {\n added = add[1] === undefined ? \"a new key\" : `(${add[1].split(\",\").map(c => c.trim()).join(\", \")})`;\n continue;\n }\n const drop = DROP_CONSTRAINT_RE.exec(clause);\n if (drop && unquote(drop[1]).endsWith(\"_pkey\")) dropped = drop[1];\n }\n\n if (added && dropped) return `${table} is re-keyed on ${added}: ${ROW_ADDRESS_LOST}`;\n if (added) {\n return `${table} gets the primary key ${added}: rows are addressed by it from now on, ` +\n \"and the push fails if two rows share a value\";\n }\n if (dropped) return `${table} loses its primary key ${dropped}: ${ROW_ADDRESS_LOST}`;\n return undefined;\n}\n\n/**\n * The column's current type. A table named without its schema matches in any\n * schema, but only when every match agrees — two same-named tables with\n * different types leave the answer unknown.\n */\nfunction currentTypeOf(change: ColumnTypeChange, columnTypes: readonly ExistingColumnType[]): string | undefined {\n const types = new Set(columnTypes\n .filter(col => col.table === change.table && col.column === change.column\n && (change.schema === undefined || col.schema === change.schema))\n .map(col => col.type));\n return types.size === 1 ? [...types][0] : undefined;\n}\n\n/** Spellings of one type, mapped to one name. Anything absent compares verbatim. */\nconst TYPE_ALIASES: Record<string, string> = {\n \"int\": \"integer\",\n \"int4\": \"integer\",\n \"int2\": \"smallint\",\n \"int8\": \"bigint\",\n \"character varying\": \"varchar\",\n \"decimal\": \"numeric\",\n \"timestamp without time zone\": \"timestamp\",\n \"timestamp with time zone\": \"timestamptz\",\n \"time without time zone\": \"time\",\n \"time with time zone\": \"timetz\",\n \"bool\": \"boolean\",\n \"float4\": \"real\",\n \"float8\": \"double precision\",\n \"character\": \"bpchar\",\n \"char\": \"bpchar\"\n};\n\n/** Decimal digits each integer type holds, which is what a `numeric` must hold too. */\nconst INTEGER_DIGITS: Record<string, number> = { smallint: 5, integer: 10, bigint: 19 };\n\n/** Types whose modifier is a fractional-seconds precision, 6 when absent. */\nconst SECONDS_PRECISION = new Set([\"timestamp\", \"timestamptz\", \"time\", \"timetz\"]);\n\ninterface ParsedType {\n base: string;\n /** The modifier, e.g. `[10, 2]` for `numeric(10,2)`. */\n args?: number[];\n /** Array dimensions. */\n dims: number;\n}\n\nfunction parseType(raw: string): ParsedType {\n let text = raw.trim().replace(/\\s+/g, \" \");\n let dims = 0;\n for (let suffix = /\\s*\\[\\d*\\]$/.exec(text); suffix; suffix = /\\s*\\[\\d*\\]$/.exec(text)) {\n dims++;\n text = text.slice(0, suffix.index);\n }\n // A quoted name is a user-defined type, compared exactly as written.\n if (text.includes(\"\\\"\")) return { base: text, dims };\n text = text.toLowerCase().replace(/^pg_catalog\\./, \"\");\n let args: number[] | undefined;\n const modifier = /\\(([^)]*)\\)/.exec(text);\n if (modifier) {\n args = modifier[1].split(\",\").map(part => Number(part.trim()));\n text = `${text.slice(0, modifier.index)} ${text.slice(modifier.index + modifier[0].length)}`\n .replace(/\\s+/g, \" \")\n .trim();\n }\n return { base: TYPE_ALIASES[text] ?? text, args, dims };\n}\n\n/** Does moving from one modifier to another, on the same type, keep every value? */\nfunction modifierWidens(base: string, from: number[] | undefined, to: number[] | undefined): boolean {\n if (JSON.stringify(from) === JSON.stringify(to)) return true;\n if (base === \"varchar\") {\n return to === undefined || (from !== undefined && to[0] >= from[0]);\n }\n if (base === \"numeric\") {\n if (to === undefined) return true;\n if (from === undefined) return false;\n const [p1, s1 = 0] = from;\n const [p2, s2 = 0] = to;\n return s2 >= s1 && p2 - s2 >= p1 - s1;\n }\n if (SECONDS_PRECISION.has(base)) {\n return (to?.[0] ?? 6) >= (from?.[0] ?? 6);\n }\n return false;\n}\n\n/**\n * Is `ALTER COLUMN … TYPE to` a widening, one that keeps every value exactly\n * as it was?\n *\n * Deliberately short. Everything not listed needs a confirmation, including\n * changes that are usually harmless: `timestamp` → `timestamptz` reads each\n * value in the session's time zone, `json` → `jsonb` drops duplicate keys,\n * `real` → `double precision` prints 0.1 as 0.100000001490116, and `integer` →\n * `text` keeps the digits but changes what the column is.\n *\n * - the same type, with a modifier at least as wide (`varchar(100)` →\n * `varchar(255)`, `numeric(10,2)` → `numeric(12,2)` or unbounded,\n * `timestamptz(3)` → `timestamptz`);\n * - `smallint` → `integer` → `bigint`, and any of them to a `numeric` with\n * room for all their digits;\n * - `varchar(n)` → `text`, and `text` → an unbounded `varchar`.\n *\n * Arrays follow their element type, and never gain or lose a dimension.\n */\nexport function isLosslessTypeChange(from: string, to: string): boolean {\n const a = parseType(from);\n const b = parseType(to);\n if (a.dims !== b.dims) return false;\n if (a.args?.some(Number.isNaN) || b.args?.some(Number.isNaN)) return false;\n if (a.base === b.base) return modifierWidens(a.base, a.args, b.args);\n\n const fromDigits = INTEGER_DIGITS[a.base];\n if (fromDigits !== undefined) {\n const toDigits = INTEGER_DIGITS[b.base];\n if (toDigits !== undefined) return toDigits >= fromDigits;\n if (b.base === \"numeric\") {\n return b.args === undefined || b.args[0] - (b.args[1] ?? 0) >= fromDigits;\n }\n return false;\n }\n if (a.base === \"varchar\" && b.base === \"text\") return true;\n if (a.base === \"text\" && b.base === \"varchar\") return b.args === undefined;\n return false;\n}\n\nexport type PushDecision = \"apply\" | \"confirm\" | \"refuse\";\n\n/**\n * Decide how `db push` should proceed given the plan's destructiveness and\n * the invocation context.\n *\n * - No destructive statements → `apply` (safe to auto-approve).\n * - Destructive + `--allow-destructive` → `apply` (operator opted in).\n * - Destructive + interactive TTY → `confirm` (prompt before applying).\n * - Destructive + non-interactive → `refuse` (never silently drop data in\n * CI / scripts / agents).\n */\nexport function decidePushSafety(opts: {\n destructiveCount: number;\n allowDestructive: boolean;\n interactive: boolean;\n}): PushDecision {\n if (opts.destructiveCount === 0) return \"apply\";\n if (opts.allowDestructive) return \"apply\";\n return opts.interactive ? \"confirm\" : \"refuse\";\n}\n","import path from \"path\";\nimport { fileURLToPath } from \"url\";\n\n// Isolated in its own module so the jest CommonJS transform can swap it for a\n// __dirname shim via moduleNameMapper — `import.meta` is a syntax error once\n// ts-jest transpiles to CJS.\nexport const moduleDir = path.dirname(fileURLToPath(import.meta.url));\n","/**\n * Where `db push` and `db generate` put the SQL they render from the collections.\n *\n * Five files — `schema.sql` (Atlas's desired state), `policies.sql`,\n * `search.sql`, `vector.sql` and `triggers.sql` — and every one of them is\n * scratch. Both commands regenerate all five before reading any of them, and\n * nothing else reads them at all. They used to be written into `drizzle/`, beside\n * the migrations, and committed: a copy nobody read, which every collection\n * change rewrote in the diff and every two branches touching collections\n * conflicted on.\n *\n * Relative to the directory the command runs in (the backend), like the\n * migrations are.\n */\nimport fs from \"fs\";\nimport path from \"path\";\n\n/** The directory, relative to the working directory. POSIX, because it is also a URL path. */\nexport const GENERATED_SQL_DIR = \".rebase/sql\";\n\n/**\n * `schema.sql` as Atlas takes it. Relative on purpose, like the\n * `file://drizzle/migrations` beside it: Atlas joins the URL's host and path, so\n * `.rebase` is read as the first path segment, measured against the pinned 1.2.3.\n */\nexport const DESIRED_STATE_URL = `file://${GENERATED_SQL_DIR}/schema.sql`;\n\n/** The files the DDL generator writes, all of them derived from the collections. */\nexport const GENERATED_SQL_FILES = [\"schema.sql\", \"policies.sql\", \"search.sql\", \"vector.sql\", \"triggers.sql\"] as const;\n\n/** The absolute directory, for the working directory the command runs in. */\nexport function generatedSqlDir(cwd: string = process.cwd()): string {\n return path.resolve(cwd, GENERATED_SQL_DIR);\n}\n\n/**\n * The first line of every file the generator writes, in every release that\n * wrote them into `drizzle/`. Matching it is what makes removing the old copy\n * safe: a file there under one of these names that does not open this way was\n * written by a person, and is left alone.\n */\nconst GENERATED_HEADER = /^-- This file (is auto-generated by the Rebase DDL generator|contains RLS policies generated by Rebase)\\./;\n\n/**\n * Get the directory ready to be written, and retire the copies an older release\n * committed.\n *\n * The directory ignores itself, rather than relying on the project's\n * `.gitignore`: a scaffold ignores `.rebase/`, but a project created before it\n * did would otherwise start committing these files from their new home.\n *\n * The old copies in `drizzle/` are deleted, not left behind. Nothing writes them\n * any more, so from the first push after an upgrade they only fall further\n * behind — and a stale `policies.sql` in the repository is exactly what someone\n * reviewing the project's RLS would read.\n *\n * With `retire: false` (`db push --dry-run`, which writes nothing of the\n * project's) the old copies are only found, not removed.\n *\n * @returns the old files it removed — or, with `retire: false`, would remove —\n * relative to the working directory.\n */\nexport function prepareGeneratedSqlDir(\n cwd: string = process.cwd(),\n options: { retire?: boolean } = {}\n): string[] {\n const dir = generatedSqlDir(cwd);\n fs.mkdirSync(dir, { recursive: true });\n const ignore = path.join(dir, \".gitignore\");\n if (!fs.existsSync(ignore)) {\n fs.writeFileSync(ignore, \"# Generated by `rebase db push` / `rebase db generate` from the collections.\\n*\\n\");\n }\n\n const removed: string[] = [];\n for (const name of GENERATED_SQL_FILES) {\n const legacy = path.join(cwd, \"drizzle\", name);\n if (!fs.existsSync(legacy)) continue;\n const firstLine = fs.readFileSync(legacy, \"utf-8\").split(\"\\n\", 1)[0];\n if (!GENERATED_HEADER.test(firstLine)) continue;\n if (options.retire !== false) fs.rmSync(legacy);\n removed.push(path.posix.join(\"drizzle\", name));\n }\n return removed;\n}\n","import path from \"path\";\nimport fs from \"fs\";\nimport { execSync } from \"child_process\";\nimport { createRequire } from \"module\";\nimport readline from \"readline\";\nimport { pathToFileURL } from \"url\";\nimport chalk from \"chalk\";\nimport { isRebaseIndexName } from \"./schema/collection-index\";\nimport type { GeneratedColumnDependency } from \"./schema/generated-column-conflicts\";\nimport { COLUMN_TYPES_SQL, type ExistingColumnType } from \"./schema/destructive-sql\";\nimport { out, outWarn } from \"./cli-output\";\nimport type { CollectionConfig } from \"@rebasepro/types\";\nimport { moduleDir as __helpersDirname } from \"./module-dir\";\nimport { generatedSqlDir } from \"./generated-sql\";\n\n\n\n/**\n * Why is a dependency's binary missing — never installed, or installed with its\n * build script blocked?\n *\n * These need opposite advice, and getting it wrong is not a cosmetic miss. pnpm\n * 10+ refuses to run a dependency's lifecycle scripts unless it is allowlisted\n * (`pnpm.onlyBuiltDependencies`, or `allowBuilds` in `pnpm-workspace.yaml`).\n * `@ariga/atlas` downloads its platform binary in `preinstall`, so a blocked\n * script leaves a state that looks like a successful install: the package is on\n * disk with its `install.js` and `package.json`, `node_modules/.bin` is empty,\n * the install exits 0, and the only signal is `ERR_PNPM_IGNORED_BUILDS` several\n * screens up.\n *\n * Telling somebody in that state to install the package again sends them round\n * the same loop forever — the add succeeds, the script is blocked again,\n * nothing changes. Verified by doing it: `pnpm add @ariga/atlas` into a bare\n * project yields exactly this, three \"Failed to create bin … ENOENT\" warnings\n * and no binary.\n *\n * Resolution is attempted from the user's project first and this package\n * second, matching the order {@link resolveLocalBin} searches — the driver may\n * be installed a level up from where the command runs.\n */\nexport type ProjectPackageManager = \"pnpm\" | \"npm\" | \"yarn\" | \"bun\" | \"unknown\";\n\n/**\n * Which package manager installed this project, read off its lockfile.\n *\n * Every remedy for a blocked build script is package-manager specific, and\n * naming the wrong one is the same failure as naming none: `pnpm approve-builds`\n * is not a thing an npm user can run, and `pnpm.onlyBuiltDependencies` is a key\n * npm does not read. This existed as a pnpm-only message for long enough that\n * npm 12 — which blocks dependency lifecycle scripts by default, the way pnpm 10\n * does — could ship and leave every npm reader three unfollowable commands deep.\n *\n * The lockfile, not `npm_config_user_agent`: the question is how the project's\n * `node_modules` was built, not which binary happens to be invoking us. Walks up\n * because commands run from `backend/` in a scaffolded project, and the lockfile\n * is at the workspace root.\n */\nexport function detectProjectPackageManager(startDir: string = process.cwd()): ProjectPackageManager {\n const lockfiles: Array<[string, ProjectPackageManager]> = [\n [\"pnpm-lock.yaml\", \"pnpm\"],\n [\"bun.lock\", \"bun\"],\n [\"bun.lockb\", \"bun\"],\n [\"yarn.lock\", \"yarn\"],\n [\"package-lock.json\", \"npm\"]\n ];\n\n let dir = path.resolve(startDir);\n while (true) {\n for (const [file, manager] of lockfiles) {\n if (fs.existsSync(path.join(dir, file))) return manager;\n }\n const parent = path.dirname(dir);\n if (parent === dir) return \"unknown\";\n dir = parent;\n }\n}\n\n/**\n * How to let `@ariga/atlas` run its `preinstall`, in the reader's own package\n * manager.\n *\n * Returns the lines to print after \"the binary is missing but the package is\n * installed\". Kept next to {@link detectProjectPackageManager} so the two move\n * together, and returned rather than printed so it can be asserted on.\n */\nexport function describeBuildScriptRemedy(\n packageName: string,\n manager: ProjectPackageManager = detectProjectPackageManager()\n): string[] {\n switch (manager) {\n case \"npm\":\n // npm 12 blocks dependency install scripts unless the root\n // package.json allowlists them. `rebase init` scaffolds the entry;\n // a project that predates it, or one that vendored its own\n // package.json, needs to add it.\n return [\n ` npm install-scripts approve ${packageName}`,\n \"\",\n \" or, to record it in the project (what `rebase init` scaffolds):\",\n \"\",\n \" // package.json\",\n ` \"allowScripts\": { \"${packageName}\": true }`,\n \"\",\n \" Then re-run `npm install`.\"\n ];\n case \"yarn\":\n return [\n \" // .yarnrc.yml\",\n \" enableScripts: true\",\n \"\",\n \" Then re-run `yarn install`.\"\n ];\n case \"bun\":\n return [\n \" // package.json\",\n ` \"trustedDependencies\": [\"${packageName}\"]`,\n \"\",\n \" Then re-run `bun install`.\"\n ];\n case \"pnpm\":\n default:\n return [\n \" pnpm approve-builds\",\n \"\",\n \" or, to record it in the project (what `rebase init` scaffolds):\",\n \"\",\n \" // package.json\",\n ` \"pnpm\": { \"onlyBuiltDependencies\": [\"${packageName}\"] }`,\n \"\",\n \" Then re-run `pnpm install`.\"\n ];\n }\n}\n\n/**\n * \"Install again, from scratch\", in the reader's own package manager.\n *\n * For the `bin-link-missing` state: the tree is half-written, and re-resolving\n * it is what recreates the `.bin` shim. `--force` on pnpm is what makes it\n * relink rather than decide the tree is already up to date.\n */\nexport function describeReinstallCommand(\n manager: ProjectPackageManager = detectProjectPackageManager()\n): string {\n switch (manager) {\n case \"npm\": return \"rm -rf node_modules && npm install\";\n case \"yarn\": return \"yarn install --force\";\n case \"bun\": return \"rm -rf node_modules && bun install\";\n case \"pnpm\":\n default: return \"pnpm install --force\";\n }\n}\n\n/** `add <pkg>` as a dev dependency, in the reader's own package manager. */\nexport function describeDevAddCommand(\n packageName: string,\n manager: ProjectPackageManager = detectProjectPackageManager()\n): string {\n switch (manager) {\n case \"npm\": return `npm install -D ${packageName}`;\n case \"yarn\": return `yarn add -D ${packageName}`;\n case \"bun\": return `bun add -d ${packageName}`;\n case \"pnpm\":\n default: return `pnpm add -D ${packageName}`;\n }\n}\n\n/**\n * Why a dependency's binary is not on `PATH`.\n *\n * `bin-link-missing` is the third state, and the reason this is not a boolean.\n * The `preinstall` did run and the binary IS on disk — only the\n * `node_modules/.bin` shim that points at it is absent. pnpm produces exactly\n * that when it writes the link before the script that creates the target\n * (\"Failed to create bin … ENOENT\"), and so does a tree copied without its\n * symlinks. Telling that reader their build scripts are blocked sends them to\n * `approve-builds`, which does nothing, because nothing is blocked.\n */\nexport type MissingBinDiagnosis = \"not-installed\" | \"build-script-blocked\" | \"bin-link-missing\";\n\n/**\n * Where a package's own manifest lives, or null if it is not installed here.\n *\n * `package.json` rather than the package root: a package with an `exports` map\n * that omits `.` is unresolvable by name even when it is perfectly installed,\n * which would misreport it as absent.\n */\nfunction resolvePackageManifest(packageName: string): string | null {\n const bases = [\n pathToFileURL(path.join(process.cwd(), \"package.json\")),\n pathToFileURL(path.join(__helpersDirname, \"package.json\"))\n ];\n for (const base of bases) {\n try {\n return createRequire(base).resolve(`${packageName}/package.json`);\n } catch {\n // Try the next base.\n }\n }\n return null;\n}\n\n/**\n * The binaries a package's own manifest declares, as absolute paths.\n *\n * `bin` is a string for a single binary named after the package, or a map.\n * Both forms appear in the wild and `@ariga/atlas` uses the map.\n */\nexport function declaredBinaries(manifestPath: string): string[] {\n let manifest: { name?: string; bin?: string | Record<string, string> };\n try {\n manifest = JSON.parse(fs.readFileSync(manifestPath, \"utf8\")) as typeof manifest;\n } catch {\n return [];\n }\n const dir = path.dirname(manifestPath);\n if (typeof manifest.bin === \"string\") return [path.resolve(dir, manifest.bin)];\n if (manifest.bin && typeof manifest.bin === \"object\") {\n return Object.values(manifest.bin)\n .filter((target): target is string => typeof target === \"string\")\n .map(target => path.resolve(dir, target));\n }\n return [];\n}\n\nexport function diagnoseMissingBin(packageName: string): MissingBinDiagnosis {\n const manifestPath = resolvePackageManifest(packageName);\n if (!manifestPath) return \"not-installed\";\n\n // Installed — but \"installed\" was the whole verdict, and it is not enough.\n // `@ariga/atlas` downloads its platform binary in `preinstall`, so a blocked\n // script leaves the package on disk with its `install.js` and manifest and\n // no binary: the state this branch was written for. It is NOT the state of\n // a tree whose binary exists and whose `.bin` shim does not, and those two\n // need opposite advice. Ask the manifest which file the binary is, and look.\n const binaries = declaredBinaries(manifestPath);\n if (binaries.length === 0) return \"build-script-blocked\";\n return binaries.some(file => fs.existsSync(file)) ? \"bin-link-missing\" : \"build-script-blocked\";\n}\n\nexport function resolveLocalBin(binName: string): string | null {\n // Try to find node_modules/.bin upwards from __helpersDirname first (package-relative)\n let dir = __helpersDirname;\n while (true) {\n const candidate = path.join(dir, \"node_modules\", \".bin\", binName);\n if (fs.existsSync(candidate)) return candidate;\n const parent = path.dirname(dir);\n if (parent === dir) break;\n dir = parent;\n }\n\n let cwd = process.cwd();\n // Try to find node_modules/.bin upwards from process.cwd()\n while (true) {\n const candidate = path.join(cwd, \"node_modules\", \".bin\", binName);\n if (fs.existsSync(candidate)) return candidate;\n const parent = path.dirname(cwd);\n if (parent === cwd) break;\n cwd = parent;\n }\n // Fall back to globally installed binary via which/where\n try {\n const cmd = process.platform === \"win32\" ? `where ${binName}` : `which ${binName}`;\n const globalPath = execSync(cmd, { encoding: \"utf-8\" }).trim().split(\"\\n\")[0].trim();\n if (globalPath && fs.existsSync(globalPath)) return globalPath;\n } catch {\n // not found globally\n }\n return null;\n}\n\nexport async function getTableIncludesFromCollections(allCollections: CollectionConfig[]): Promise<string[]> {\n const { getTableName, relationalCollections, resolveJunctionSpecs } = await import(\"@rebasepro/common\");\n const { isPostgresCollectionConfig } = await import(\"@rebasepro/types\");\n\n // The include list is the inverse of {@link getTableExcludes}: a name on it\n // is a name Atlas is allowed to drop. A Firestore collection called\n // `exercises` must not put `public.exercises` here — a real, unrelated table\n // of that name would lose its protection and be dropped by the next\n // auto-approved `db push`.\n const collections = relationalCollections(allCollections);\n const includes: string[] = [];\n for (const col of collections) {\n const tableName = getTableName(col);\n const schema = isPostgresCollectionConfig(col) && col.schema ? col.schema : \"public\";\n if (tableName) {\n includes.push(`${schema}.${tableName}`);\n }\n }\n // Junctions where they are created — `public`, whatever schema either\n // endpoint is in. Qualifying one with an endpoint's schema left the real\n // junction excluded, invisible to Atlas, and planned for creation again.\n for (const junction of resolveJunctionSpecs(collections).values()) {\n includes.push(`${junction.schema}.${junction.table}`);\n }\n\n return Array.from(new Set(includes));\n}\n\nexport async function getTableIncludes(collectionsPath: string): Promise<string[]> {\n return getTableIncludesFromCollections(await loadCollectionsForCli(collectionsPath));\n}\n\n/**\n * Load a project's collections the way the Atlas-facing commands need them.\n *\n * Deliberately forgiving — a file that fails to import is skipped rather than\n * fatal — because the callers use this to *narrow* what Atlas may touch, and a\n * hard failure here would block a push over an unrelated broken file. Callers\n * that cannot tolerate a partial answer (the table excludes, which fail closed)\n * check the result themselves.\n */\n/** Paths this process has already complained about. See {@link loadCollectionsForCli}. */\nconst warnedMissingCollectionsPaths = new Set<string>();\n\nexport async function loadCollectionsForCli(collectionsPath: string): Promise<CollectionConfig[]> {\n // Note: a relative --collections path resolves against the *current working\n // directory*, not the backend package. When invoked via the generated npm\n // script (cwd = backend/), the script uses \"../config/collections\" to\n // compensate — so running the CLI directly from the repo root with a\n // different relative path can silently point at the wrong place.\n const resolvedPath = path.resolve(collectionsPath);\n const collections: CollectionConfig[] = [];\n if (!fs.existsSync(resolvedPath) && !warnedMissingCollectionsPaths.has(resolvedPath)) {\n // Once per path per process. `db push` re-enters this from\n // `schemaCommand`, `generatePostgresDdlCommand` and the Atlas argv\n // assembly, so the same four lines were printed four times — sixteen\n // lines of identical text ahead of the real output.\n //\n // An explicitly typed `--collections` never reaches here at all: it is\n // refused at the entry point, before anything is generated. See\n // `cli-collections-path.ts`. What is left is the default, which a\n // headless project legitimately does not have.\n warnedMissingCollectionsPaths.add(resolvedPath);\n outWarn(chalk.yellow(\n ` ⚠ Collections path not found: \"${collectionsPath}\"\\n` +\n ` Resolved to: ${resolvedPath}\\n` +\n ` (relative to cwd: ${process.cwd()})\\n` +\n ` Pass an absolute path, or a path relative to where you run the command.`\n ));\n }\n if (fs.existsSync(resolvedPath)) {\n const stats = fs.statSync(resolvedPath);\n if (stats.isDirectory()) {\n const files = fs.readdirSync(resolvedPath);\n for (const file of files) {\n if ((file.endsWith(\".ts\") || file.endsWith(\".js\")) &&\n !file.includes(\".test.\") &&\n !file.endsWith(\".d.ts\") &&\n file !== \"index.ts\" && file !== \"index.js\") {\n try {\n const filePath = path.join(resolvedPath, file);\n const fileUrl = pathToFileURL(filePath).href;\n const module = await import(fileUrl);\n if (module && module.default) {\n collections.push(module.default);\n }\n } catch {\n // ignore\n }\n }\n }\n }\n }\n\n return collections;\n}\n\n/**\n * The app database connections this process has open, by URL — one each.\n *\n * `rebase db push` used to open a connection per step: the auth bootstrap, the\n * connectivity probe, each catalogue read behind the Atlas excludes, the\n * column types, the generated-column edges, and then every post-step — RLS\n * lock, auth tables, vector, search, triggers, policies, reconcile, role,\n * legacy schema. 18 of them on a push that changed nothing. Locally each costs\n * a few milliseconds; against a hosted Postgres over TLS a connect is four or\n * five round trips (TCP, TLS 1.3, startup, SCRAM twice) — about 4 s of every\n * push at 40 ms, and more on a serverless database that wakes per connection.\n *\n * Opened on first use and closed by {@link closeAppClients} when the command\n * ends. The scratch database Atlas plans in is created and dropped through the\n * `postgres` database, on a connection of its own: it is not this one.\n */\nconst appClients = new Map<string, Promise<PgClient>>();\n\ntype PgClient = import(\"pg\").Client;\n\nasync function openAppClient(databaseUrl: string, connectionTimeoutMillis?: number): Promise<PgClient> {\n const open = appClients.get(databaseUrl);\n if (open) return open;\n const opening = (async () => {\n const { Client } = await import(\"pg\");\n const client = new Client({\n connectionString: databaseUrl,\n ...(connectionTimeoutMillis ? { connectionTimeoutMillis } : {})\n });\n // A connection can die while it sits idle between steps — Atlas runs\n // for seconds with this one open. Unheard, that `error` is an uncaught\n // exception and kills the process; heard, the next step opens another,\n // and a step running on it fails with its own error, as before.\n const forget = (): void => {\n if (appClients.get(databaseUrl) === opening) appClients.delete(databaseUrl);\n };\n client.on(\"error\", forget);\n client.on(\"end\", forget);\n await client.connect();\n return client;\n })();\n appClients.set(databaseUrl, opening);\n try {\n return await opening;\n } catch (err) {\n // Not kept: the next step tries again, and reports its own failure the\n // way it did when every step connected for itself.\n appClients.delete(databaseUrl);\n throw err;\n }\n}\n\n/**\n * Run one step on this process's connection to the app database.\n *\n * Every step leaves the session as it found it, which is what a connection of\n * its own used to guarantee by being closed. None of them sets anything for\n * the session — `SET LOCAL` inside their own transaction, or `RESET` — and one\n * that fails part-way through a transaction it opened is rolled back here, so\n * the next step does not meet `current transaction is aborted`.\n *\n * @param options.connectionTimeoutMillis applies only when this call is the one\n * that connects.\n */\nexport async function withAppClient<T>(\n databaseUrl: string,\n step: (client: PgClient) => Promise<T>,\n options: { connectionTimeoutMillis?: number } = {}\n): Promise<T> {\n const client = await openAppClient(databaseUrl, options.connectionTimeoutMillis);\n try {\n return await step(client);\n } catch (err) {\n // Outside a transaction this is a warning Postgres sends and nobody\n // reads; on a dead connection it fails too, which changes nothing.\n await client.query(\"ROLLBACK\").catch(() => undefined);\n throw err;\n }\n}\n\n/** Close every connection {@link withAppClient} opened. Called once, as the command ends. */\nexport async function closeAppClients(): Promise<void> {\n const open = [...appClients.values()];\n appClients.clear();\n await Promise.all(open.map(async opening => {\n try {\n await (await opening).end();\n } catch {\n // Already gone, or never connected: nothing to close.\n }\n }));\n}\n\n/**\n * Names the empty database Atlas plans against, instead of `<db>_dev_diff`.\n *\n * The derived one is made with `CREATE DATABASE` beside the real database,\n * which assumes a server that holds more than one. PGlite — the managed\n * development database — serves exactly one: the CREATE succeeds and creates\n * nothing, so Atlas connected straight back to the database it was comparing\n * and refused it as \"not clean\". `rebase db push` on the managed database now\n * starts a second, throwaway PGlite and names it here. Anyone can name their\n * own the same way; a named database is neither created nor dropped.\n */\nexport const ATLAS_DEV_URL_ENV = \"REBASE_ATLAS_DEV_URL\";\n\n/**\n * The database Atlas replays the desired schema into: the one\n * {@link ATLAS_DEV_URL_ENV} names (`named: true` — not ours to create or\n * drop), else `<db>_dev_diff`.\n */\nexport function atlasDevDatabaseUrl(\n databaseUrl: string,\n env: Readonly<Record<string, string | undefined>>\n): { url: string; named: boolean } {\n const named = env[ATLAS_DEV_URL_ENV]?.trim();\n if (!named) return { url: getDevDatabaseUrl(databaseUrl), named: false };\n if (named === databaseUrl.trim()) {\n // Atlas would replay the schema into the database it is about to change.\n throw new Error(\n `${ATLAS_DEV_URL_ENV} names the same database as DATABASE_URL. It must be a separate, empty ` +\n \"database that Atlas may fill and clear.\"\n );\n }\n return { url: named, named: true };\n}\n\nexport function getDevDatabaseUrl(databaseUrl: string): string {\n try {\n const parsed = new URL(databaseUrl);\n const dbName = parsed.pathname.slice(1);\n parsed.pathname = `/${dbName}_dev_diff`;\n return parsed.toString();\n } catch {\n return databaseUrl + \"_dev_diff\";\n }\n}\n\n/**\n * Create the scratch database Atlas plans against, and say so when we cannot.\n *\n * Atlas needs a `--dev-url` — an empty database it can build the desired state\n * in — and this makes `<db>_dev_diff` next to the real one. The whole body used\n * to be wrapped in an empty catch commented \"Ignore, let Atlas handle\n * connection failures\", and Atlas's version of handling it is `postgres:\n * querying system variables: pq: database \"app_dev_diff\" does not exist\n * (3D000)` — the symptom, four frames downstream of the cause, with the cause\n * thrown away.\n *\n * The cause is almost always `42501`: `CREATE DATABASE` is a privilege managed\n * providers do not grant, so this is the first thing a hosted user hits and the\n * one they can do least with.\n *\n * @returns whether the scratch database was created by *this* call — the only\n * case in which dropping it afterwards is ours to do.\n */\nexport async function ensureDevDatabaseExists(databaseUrl: string, devDatabaseUrl: string): Promise<boolean> {\n let devDbName = \"\";\n try {\n const { Client } = await import(\"pg\");\n const parsed = new URL(databaseUrl);\n devDbName = new URL(devDatabaseUrl).pathname.slice(1);\n\n parsed.pathname = \"/postgres\";\n const client = new Client({ connectionString: parsed.toString() });\n await client.connect();\n try {\n const res = await client.query(\"SELECT 1 FROM pg_database WHERE datname = $1\", [devDbName]);\n if (res.rowCount === 0) {\n await client.query(`CREATE DATABASE \"${devDbName}\"`);\n out(chalk.gray(` ✓ Created validation database \"${devDbName}\"`));\n\n return true;\n }\n } finally {\n await client.end();\n }\n } catch (err) {\n outWarn(describeDevDatabaseFailure(err, devDbName || \"<db>_dev_diff\"));\n }\n\n return false;\n}\n\n/**\n * What went wrong creating the scratch database, and what to do about it.\n *\n * Exported for its test: the `42501` branch is the one that matters and it\n * cannot be provoked without a non-superuser role.\n */\nexport function describeDevDatabaseFailure(err: unknown, devDbName: string): string {\n const code = (err as { code?: string } | null)?.code;\n const message = err instanceof Error ? err.message : String(err);\n\n const lines = [\n chalk.yellow(` ⚠ Could not create the Atlas scratch database \"${devDbName}\": ${message}`)\n ];\n if (code === \"42501\") {\n lines.push(chalk.gray(\n \" Atlas plans every change in a throwaway copy of the schema, and CREATE DATABASE\\n\" +\n \" is a privilege most managed providers withhold. Two ways forward:\\n\" +\n ` • GRANT the role CREATEDB: ALTER ROLE \"<role>\" CREATEDB;\\n` +\n ` • or create it once by hand: CREATE DATABASE \"${devDbName}\";`\n ));\n } else {\n lines.push(chalk.gray(\" Atlas will fail next with `database \\\"\" + devDbName + \"\\\" does not exist (3D000)`, which is this.\"));\n }\n\n return lines.join(\"\\n\");\n}\n\n/**\n * Drop the scratch database again, after the command that needed it succeeded.\n *\n * It is a full copy of the schema — no rows, but every table, type and index —\n * and it was left behind per target forever. The only notice anyone got was\n * `rebase db branch prune` reporting \"3 Atlas scratch database(s) left over\n * from db push\", and on a provider that bills per database it is a silent cost.\n *\n * Deliberately NOT called on the failure path: after a failed push the scratch\n * database holds the state Atlas was planning against, which is the one thing\n * worth inspecting.\n */\nexport async function dropDevDatabase(databaseUrl: string, devDatabaseUrl: string): Promise<void> {\n try {\n const { Client } = await import(\"pg\");\n const parsed = new URL(databaseUrl);\n const devDbName = new URL(devDatabaseUrl).pathname.slice(1);\n\n parsed.pathname = \"/postgres\";\n const client = new Client({ connectionString: parsed.toString() });\n await client.connect();\n try {\n await client.query(`DROP DATABASE IF EXISTS \"${devDbName}\"`);\n out(chalk.gray(` ✓ Removed validation database \"${devDbName}\"`));\n } finally {\n await client.end();\n }\n } catch (err) {\n // Not fatal, and not silent: the push has already succeeded, so the\n // worst case is one database left where `prune` can find it.\n outWarn(chalk.gray(\n ` ⚠ Could not remove the Atlas scratch database: ${err instanceof Error ? err.message : String(err)}\\n` +\n \" `rebase db branch prune --include-dev-diff` clears them.\"\n ));\n }\n}\n\n/**\n * The generated SQL for the project's `search` blocks, if it has any.\n *\n * @param sqlDir directory holding the generated SQL. Defaults to\n * `.rebase/sql` under the working directory.\n */\nexport function readSearchDdl(sqlDir: string = generatedSqlDir()): string {\n const searchFile = path.join(sqlDir, \"search.sql\");\n if (!fs.existsSync(searchFile)) return \"\";\n return fs.readFileSync(searchFile, \"utf-8\").trim();\n}\n\n/**\n * Bring the search column, its index and their helpers up to date.\n *\n * Runs *after* Atlas, not before: the statements are `ALTER TABLE ... ADD\n * COLUMN`, so the table has to exist. Atlas is told to ignore these objects\n * entirely (`getSearchExcludes`) — it cannot manage them, and left to itself it\n * would plan a `DROP COLUMN` for every one, since they are absent from the\n * desired state it was given.\n *\n * A no-op when no collection declared `search`. A failure is *not* swallowed:\n * silently pushing a schema whose search column never appeared is how a\n * collection ends up with search configured, no error anywhere, and no results.\n */\nexport async function applySearchDdl(\n databaseUrl: string,\n sqlDir: string = generatedSqlDir()\n): Promise<void> {\n const sql = readSearchDdl(sqlDir);\n if (!sql) return;\n\n await withAppClient(databaseUrl, client => client.query(sql));\n out(chalk.gray(\" ✓ Applied full-text search columns and indexes\"));\n}\n\n/**\n * Glob patterns keeping Atlas away from the search objects.\n *\n * Returns an empty list — and so changes nothing — for a project with no\n * `search` block, which is every project that has not opted in.\n */\nexport async function getSearchExcludes(collectionsPath: string): Promise<string[]> {\n const { searchExcludePatterns } = await import(\"./schema/generate-postgres-ddl-logic\");\n return searchExcludePatterns(await loadCollectionsForCli(collectionsPath));\n}\n\n/**\n * Every `generated column → column it reads` edge in the database.\n *\n * Read from `pg_depend` rather than by matching names in the expression text:\n * a generated column's `pg_attrdef` row carries one `deptype = 'n'` dependency\n * per column its expression references, which is the catalogue's own answer and\n * cannot be fooled by a column name that also appears inside a string literal\n * or as part of a longer identifier.\n *\n * Used by `db push` to find what has to come out of the way before Atlas runs —\n * see `generated-column-conflicts.ts` for why that is necessary at all.\n */\nexport async function queryGeneratedColumnDependencies(\n databaseUrl: string\n): Promise<GeneratedColumnDependency[]> {\n return withAppClient(databaseUrl, async client => {\n const res = await client.query(`\n SELECT n.nspname AS \"schema\",\n c.relname AS \"table\",\n gen.attname AS \"column\",\n dep.attname AS \"dependsOn\"\n FROM pg_attrdef ad\n JOIN pg_class c ON c.oid = ad.adrelid\n JOIN pg_namespace n ON n.oid = c.relnamespace\n JOIN pg_attribute gen ON gen.attrelid = ad.adrelid AND gen.attnum = ad.adnum\n JOIN pg_depend d ON d.classid = 'pg_attrdef'::regclass\n AND d.objid = ad.oid\n AND d.refclassid = 'pg_class'::regclass\n AND d.refobjid = ad.adrelid\n AND d.deptype = 'n'\n JOIN pg_attribute dep ON dep.attrelid = ad.adrelid AND dep.attnum = d.refobjsubid\n WHERE gen.attgenerated <> ''\n AND NOT gen.attisdropped\n AND NOT dep.attisdropped;\n `);\n return res.rows as GeneratedColumnDependency[];\n });\n}\n\n/**\n * Every column in the database with its current type, for the destructive\n * gate to tell a widening `ALTER COLUMN … TYPE` from one that loses data.\n */\nexport async function queryColumnTypes(databaseUrl: string): Promise<ExistingColumnType[]> {\n return withAppClient(databaseUrl, async client => (await client.query<ExistingColumnType>(COLUMN_TYPES_SQL)).rows);\n}\n\n/**\n * Drop the generated columns standing in the way of an apply.\n *\n * Their indexes and their `COMMENT ON COLUMN` stamp go with them, which is\n * exactly right: `applySearchDdl` rebuilds all three from `search.sql` and a\n * surviving stamp would make the rebuilt column look like drift.\n */\nexport async function dropGeneratedColumns(\n databaseUrl: string,\n statements: string[]\n): Promise<void> {\n if (statements.length === 0) return;\n await withAppClient(databaseUrl, async client => {\n for (const statement of statements) {\n await client.query(statement);\n }\n });\n}\n\n/**\n * The generated SQL for the project's `vector` properties, if it has any.\n *\n * @param sqlDir directory holding the generated SQL. Defaults to\n * `.rebase/sql` under the working directory.\n */\nexport function readVectorDdl(sqlDir: string = generatedSqlDir()): string {\n const vectorFile = path.join(sqlDir, \"vector.sql\");\n if (!fs.existsSync(vectorFile)) return \"\";\n return fs.readFileSync(vectorFile, \"utf-8\").trim();\n}\n\n/**\n * Install pgvector and bring the vector columns and their ANN indexes up to\n * date.\n *\n * Runs *after* Atlas, like {@link applySearchDdl} and for the same reason: the\n * statements are `ALTER TABLE … ADD COLUMN`, so the table has to exist. Atlas\n * is told to ignore these objects entirely ({@link getVectorExcludes}) — it\n * cannot manage a type its dev database is structurally incapable of having,\n * and left to itself it would plan a `DROP COLUMN` for every one.\n *\n * A no-op when no collection declares a `vector` property. A failure is *not*\n * swallowed: a push that reported success while the embedding column never\n * appeared is how a project ends up with vector search configured, no error\n * anywhere, and no results.\n */\nexport async function applyVectorDdl(\n databaseUrl: string,\n sqlDir: string = generatedSqlDir()\n): Promise<void> {\n const sql = readVectorDdl(sqlDir);\n if (!sql) return;\n\n const { vectorExtensionHint } = await import(\"./schema/vector-index\");\n try {\n await withAppClient(databaseUrl, client => client.query(sql));\n } catch (err) {\n // The boot ensure explains this failure and the push has to as well —\n // it is the same missing extension, and a push is the *more* likely\n // place to meet it. Without this the developer gets a bare\n // `type \"vector\" does not exist`, which names nothing they can act on\n // and reads like a broken database rather than a line of config.\n const message = err instanceof Error ? err.message : String(err);\n const hint = vectorExtensionHint(message);\n if (!hint) throw err;\n throw new Error(`${message}${hint}`, { cause: err });\n }\n out(chalk.gray(\" ✓ Applied vector columns and ANN indexes\"));\n}\n\n/**\n * Glob patterns keeping Atlas away from the vector objects.\n *\n * Returns an empty list — and so changes nothing — for a project with no\n * `vector` property, which is every project that has not opted in.\n */\nexport async function getVectorExcludes(collectionsPath: string): Promise<string[]> {\n const { vectorExcludePatterns } = await import(\"./schema/generate-postgres-ddl-logic\");\n return vectorExcludePatterns(await loadCollectionsForCli(collectionsPath));\n}\n\n/**\n * The generated SQL for the project's `autoValue: \"on_update\"` properties, if\n * it has any.\n *\n * @param sqlDir directory holding the generated SQL. Defaults to\n * `.rebase/sql` under the working directory.\n */\nexport function readTriggersDdl(sqlDir: string = generatedSqlDir()): string {\n const triggersFile = path.join(sqlDir, \"triggers.sql\");\n if (!fs.existsSync(triggersFile)) return \"\";\n return fs.readFileSync(triggersFile, \"utf-8\").trim();\n}\n\n/**\n * Install the `updated_at` trigger function and bind it to every column that\n * asked for one.\n *\n * Runs *after* Atlas, like {@link applySearchDdl} and {@link applyVectorDdl}:\n * a trigger needs its table and its column to exist. Atlas is told to ignore\n * the triggers ({@link getTriggerExcludes}) because it cannot manage a function\n * at all — its free tier refuses to parse a desired state containing one.\n *\n * A no-op when no property declares `autoValue: \"on_update\"`. A failure is\n * *not* swallowed: a push that reported success while the trigger never\n * appeared is how `updated_at` ends up silently stale for every writer that is\n * not the driver, which is the whole reason the trigger exists.\n */\nexport async function applyTriggersDdl(\n databaseUrl: string,\n sqlDir: string = generatedSqlDir()\n): Promise<void> {\n const sql = readTriggersDdl(sqlDir);\n if (!sql) return;\n\n await withAppClient(databaseUrl, client => client.query(sql));\n out(chalk.gray(\" ✓ Applied `updated_at` triggers\"));\n}\n\n/** @see getSearchExcludes */\nexport async function getTriggerExcludes(collectionsPath: string): Promise<string[]> {\n const { triggerExcludePatterns } = await import(\"./schema/generate-postgres-ddl-logic\");\n return triggerExcludePatterns(await loadCollectionsForCli(collectionsPath));\n}\n\n/**\n * Query the live database for every user table/view outside the system\n * catalogs. Separated from {@link getTableExcludes} so its failure mode can\n * be handled explicitly (fail closed) and so tests can inject a stub.\n */\n/**\n * Every index Postgres holds on a table Rebase manages, as\n * `schema.table.index`.\n *\n * Constraint-backed indexes are left out: a `PRIMARY KEY` or a `UNIQUE` is a\n * *constraint* to Atlas, diffed from the constraint declaration, and naming its\n * index in an exclude would shield the constraint itself.\n */\nexport async function queryExistingIndexes(databaseUrl: string): Promise<string[]> {\n return withAppClient(databaseUrl, async client => {\n const res = await client.query(`\n SELECT n.nspname || '.' || t.relname || '.' || i.relname AS full_name\n FROM pg_index x\n JOIN pg_class i ON i.oid = x.indexrelid\n JOIN pg_class t ON t.oid = x.indrelid\n JOIN pg_namespace n ON n.oid = t.relnamespace\n WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')\n AND t.relkind IN ('r', 'p')\n AND NOT EXISTS (\n SELECT 1 FROM pg_constraint c WHERE c.conindid = i.oid\n );\n `);\n return res.rows.map((row: { full_name: string }) => row.full_name);\n });\n}\n\n/**\n * Glob patterns keeping Atlas away from indexes that are not Rebase's.\n *\n * The problem this solves is live and predates the `indexes:` block. `db push`\n * is declarative, so an index on a managed table that is absent from\n * `schema.sql` is drift — and Atlas plans `DROP INDEX` for it. Verified\n * against atlas v1.2.3: create one by hand, re-run an unchanged push, and the\n * plan is a bare drop. `DROP INDEX` is not in `DESTRUCTIVE_PATTERNS`, so the\n * auto-approved apply took it without asking. Until now the only way to have\n * an index at all was to write it by hand, which made that the *only* outcome.\n *\n * Ownership rather than a prompt, because once indexes are declarable a drop\n * is usually correct: removing one from the config should remove it from the\n * database, quietly. What must never be dropped is an index Rebase never\n * created — a hand-written one, or one an introspected database arrived with.\n * {@link isRebaseIndexName} is the test, the same arrangement\n * `isGeneratedPolicyName` uses to let policy reconciliation drop only what it\n * generated.\n *\n * The named-in-the-desired-state check comes first and is what makes removal\n * work: a declared index that the author has just deleted still matches the\n * name pattern, is no longer in the plan, and so is *not* excluded — Atlas\n * drops it, as intended.\n *\n * Three-part `schema.table.index`, never two: a two-part pattern reads as a\n * *table* named `<index>` in a *schema* named `<table>`, matches nothing, and\n * reports no error — the trap already recorded for the search excludes.\n */\nexport async function getForeignIndexExcludes(\n databaseUrl: string,\n collectionsPath: string,\n deps: {\n queryExistingIndexes?: (databaseUrl: string) => Promise<string[]>;\n getManagedIndexNames?: (collectionsPath: string) => Promise<Set<string>>;\n } = {}\n): Promise<string[]> {\n const queryIndexes = deps.queryExistingIndexes ?? queryExistingIndexes;\n const getManaged = deps.getManagedIndexNames ?? managedIndexNames;\n\n const managed = await getManaged(collectionsPath);\n\n let existing: string[];\n try {\n existing = await queryIndexes(databaseUrl);\n } catch (err) {\n // Fails CLOSED, like getTableExcludes: without the catalogue we cannot\n // tell a foreign index from one of ours, and guessing wrong destroys\n // an index somebody is relying on.\n throw new ExcludeIntrospectionError(\n `Failed to introspect the database for unmanaged indexes: ${err instanceof Error ? err.message : String(err)}`,\n err\n );\n }\n\n return existing.filter(full => {\n const indexName = full.slice(full.lastIndexOf(\".\") + 1);\n if (managed.has(indexName)) return false; // ours, and still declared\n return !isRebaseIndexName(indexName); // ours, but no longer declared -> let it drop\n });\n}\n\n/**\n * The index names the current collections would create — search, vector and\n * declared alike. Anything Atlas would emit is by definition not foreign.\n */\nasync function managedIndexNames(collectionsPath: string): Promise<Set<string>> {\n const collections = await loadCollectionsForCli(collectionsPath);\n const { searchExcludePatterns, vectorExcludePatterns } =\n await import(\"./schema/generate-postgres-ddl-logic\");\n const { resolveColumnName } = await import(\"./schema/column-plan-helpers\");\n const { buildCollectionIndexPlan } = await import(\"./schema/collection-index\");\n\n const names = new Set<string>(\n buildCollectionIndexPlan(collections, resolveColumnName).map(spec => spec.indexName)\n );\n // Search and vector objects are excluded from Atlas wholesale by their own\n // patterns; listing them here too is harmless and keeps this the single\n // answer to \"is this index one of ours\". The doc comment above claimed\n // vector was here long before it was — so an ANN index answered that\n // question with \"somebody else's\".\n for (const pattern of [...searchExcludePatterns(collections), ...vectorExcludePatterns(collections)]) {\n names.add(pattern.slice(pattern.lastIndexOf(\".\") + 1));\n }\n return names;\n}\n\nexport async function queryExistingTables(databaseUrl: string): Promise<string[]> {\n return withAppClient(databaseUrl, async client => {\n const res = await client.query(`\n SELECT table_schema || '.' || table_name AS full_name\n FROM information_schema.tables\n WHERE table_schema NOT IN ('pg_catalog', 'information_schema')\n AND table_type IN ('BASE TABLE', 'VIEW');\n `);\n return res.rows.map((row: { full_name: string }) => row.full_name);\n });\n}\n\n/**\n * Raised when the exclude list could not be built. `db push` MUST abort on\n * this rather than continue: the exclude list is the only thing shielding\n * non-collection (user/system) tables from the auto-approved declarative\n * apply. A partial list — the old fail-open behaviour — meant a transient\n * introspection hiccup dropped every table not present in `schema.sql`.\n */\nexport class ExcludeIntrospectionError extends Error {\n constructor(message: string, readonly cause?: unknown) {\n super(message);\n this.name = \"ExcludeIntrospectionError\";\n }\n}\n\n/**\n * Build the `--exclude` list that protects tables Rebase doesn't manage from\n * the declarative apply. Anything not backing a collection (or its M2M\n * junctions) is excluded so Atlas never drops it.\n *\n * Fails **closed**: if the database can't be introspected we cannot know\n * which tables to protect, so we throw {@link ExcludeIntrospectionError}\n * instead of returning a near-empty list and letting the caller drop\n * everything.\n *\n * `deps` is injectable for tests; production uses the real pg-backed queries.\n */\nexport async function getTableExcludes(\n databaseUrl: string,\n collectionsPath: string,\n deps: {\n queryExistingTables?: (databaseUrl: string) => Promise<string[]>;\n getIncludes?: (collectionsPath: string) => Promise<string[]>;\n } = {}\n): Promise<string[]> {\n const getIncludes = deps.getIncludes ?? getTableIncludes;\n const queryTables = deps.queryExistingTables ?? queryExistingTables;\n\n const includes = await getIncludes(collectionsPath);\n // Schemas the declarative apply must never touch. `rebase` holds the auth\n // tables (users, refresh_tokens, …), the RLS helper functions and Atlas's\n // own revision table. Excluding both the schema object AND its contents\n // keeps Atlas from planning a `DROP SCHEMA … CASCADE` — which on a live\n // database would take the user/auth tables with it.\n // `atlas_schema_revisions.*` covers a revision table living outside the\n // `rebase` schema — an externally managed Atlas setup — which the apply\n // must not plan changes against either.\n //\n // `auth` stays on this list even though Rebase no longer creates it, and\n // that is the point: on a database still holding the pre-1.0 schema it must\n // survive until the guarded cleanup retires it, and on a Supabase database\n // it is somebody else's and must never be in scope at all. Removing it here\n // would let a `db push` plan `DROP SCHEMA auth CASCADE` against a live\n // Supabase installation.\n const excludes: string[] = [\"atlas_schema_revisions.*\", \"auth\", \"auth.*\", \"rebase\", \"rebase.*\"];\n\n let existingTables: string[];\n try {\n existingTables = await queryTables(databaseUrl);\n } catch (err) {\n throw new ExcludeIntrospectionError(\n `Failed to introspect the database for unmapped tables: ${err instanceof Error ? err.message : String(err)}`,\n err\n );\n }\n\n for (const table of existingTables) {\n if (!includes.includes(table)) {\n excludes.push(table);\n }\n }\n\n return excludes;\n}\n\n\n/**\n * Ask a yes/no question on an interactive terminal.\n *\n * The `isTTY` guard is the contract, not an optimisation: non-interactive\n * shells (CI, pipes, agents) can never answer, and `readline` on a\n * non-TTY stdin resolves with whatever the pipe happens to contain — or never\n * resolves at all. Returning false there is what makes an unattended\n * `db push` abort instead of auto-confirming a destructive change. Callers\n * should already have gone through {@link decidePushSafety}, which decides\n * whether interactive confirmation is even possible; this is the backstop.\n *\n * Lives here rather than in cli.ts so it is reachable from a test — cli.ts uses\n * `import.meta` and cannot be imported by the jest runner.\n */\nexport async function promptConfirm(question: string): Promise<boolean> {\n if (!process.stdin.isTTY) return false;\n const rl = readline.createInterface({ input: process.stdin, output: process.stdout });\n try {\n const answer: string = await new Promise((resolve) => rl.question(question, resolve));\n return /^y(es)?$/i.test(answer.trim());\n } finally {\n rl.close();\n }\n}\n","import chalk from \"chalk\";\nimport { outWarn, outError } from \"./cli-output\";\nimport { extractCauseMessage } from \"./utils/error-cause\";\nimport { withAppClient } from \"./cli-helpers\";\n\n/**\n * Detect whether an error (or AggregateError wrapping multiple attempts)\n * represents an ECONNREFUSED — i.e. the database is simply not running.\n *\n * Handles:\n * - Direct `{ code: \"ECONNREFUSED\" }` errors from Node `net`\n * - `AggregateError` from dual-stack IPv4+IPv6 connection attempts\n * - Drizzle's `cause`-wrapped pg errors\n */\nexport function isEconnrefused(err: unknown): boolean {\n if (!err || typeof err !== \"object\") return false;\n const e = err as { code?: string; cause?: unknown; errors?: unknown[] };\n if (e.code === \"ECONNREFUSED\") return true;\n // AggregateError from Node net (dual-stack IPv4 + IPv6)\n if (Array.isArray(e.errors)) {\n return e.errors.some(inner =>\n inner && typeof inner === \"object\" && (inner as { code?: string }).code === \"ECONNREFUSED\"\n );\n }\n // Drizzle wraps the pg error in `cause`\n if (e.cause && typeof e.cause === \"object\") {\n return isEconnrefused(e.cause);\n }\n return false;\n}\n\n/**\n * Detect PostgreSQL authentication failures.\n * PG error codes: 28P01 (invalid_password), 28000 (invalid_authorization_specification)\n */\nexport function isAuthFailure(err: unknown): boolean {\n if (!err || typeof err !== \"object\") return false;\n const e = err as { code?: string; cause?: unknown };\n if (e.code === \"28P01\" || e.code === \"28000\") return true;\n if (e.cause && typeof e.cause === \"object\") {\n return isAuthFailure(e.cause);\n }\n // Also check the message for common pg auth failure text\n if (\"message\" in e && typeof (e as { message?: string }).message === \"string\") {\n const msg = (e as { message: string }).message.toLowerCase();\n if (msg.includes(\"password authentication failed\") || msg.includes(\"no pg_hba.conf entry\")) {\n return true;\n }\n }\n return false;\n}\n\n/**\n * Detect the \"SSL is not enabled on the server\" failure — the client attempted\n * an SSL handshake against a Postgres server that doesn't support it (common\n * with a plain local dev database). The fix is `?sslmode=disable` on the URL.\n */\nexport function isSslNotEnabled(err: unknown): boolean {\n if (!err || typeof err !== \"object\") return false;\n const e = err as { message?: string; cause?: unknown };\n if (typeof e.message === \"string\" && e.message.toLowerCase().includes(\"ssl is not enabled on the server\")) {\n return true;\n }\n if (e.cause && typeof e.cause === \"object\") {\n return isSslNotEnabled(e.cause);\n }\n return false;\n}\n\n/**\n * Detect PostgreSQL \"cannot drop ... because other objects depend on it\"\n * (error code 2BP01, dependent_objects_still_exist). This is the failure that\n * strands a declarative `db push` half-applied when a collection is removed but\n * an enum type it defined is still referenced by another object.\n */\nexport function isDependencyDropError(err: unknown): boolean {\n if (!err || typeof err !== \"object\") return false;\n const e = err as { code?: string; message?: string; cause?: unknown };\n if (e.code === \"2BP01\") return true;\n if (typeof e.message === \"string\") {\n const msg = e.message.toLowerCase();\n if (msg.includes(\"other objects depend on it\") || msg.includes(\"cannot drop type\")) {\n return true;\n }\n }\n if (e.cause && typeof e.cause === \"object\") {\n return isDependencyDropError(e.cause);\n }\n return false;\n}\n\n/**\n * Parse host:port from a DATABASE_URL for display purposes.\n *\n * Exported because every message about a connection has to name the thing it\n * could not reach, and the boot path needs the same rendering the CLI banners\n * use — including the same refusal to print the URL itself, which carries the\n * password.\n */\nexport function parseHostInfo(databaseUrl: string): string {\n try {\n const parsed = new URL(databaseUrl);\n return `${parsed.hostname}:${parsed.port || 5432}`;\n } catch {\n return \"unknown\";\n }\n}\n\n/**\n * The sentence the operating system actually produced, dug out of the wrappers.\n *\n * `connect ECONNREFUSED 127.0.0.1:5432` is written by `net`, then wrapped by\n * `pg`, then wrapped again by Drizzle as `Failed query: …` — and on a\n * dual-stack host it is not in `.cause` at all but inside the\n * `AggregateError.errors` array of one attempt per resolved address. Printing\n * the banner without it loses the one token every search engine, runbook and\n * colleague recognises.\n */\nexport function deepestErrorMessage(err: unknown): string | null {\n if (!err || typeof err !== \"object\") return null;\n const e = err as { message?: string; code?: string; cause?: unknown; errors?: unknown[] };\n\n if (Array.isArray(e.errors)) {\n for (const inner of e.errors) {\n const deeper = deepestErrorMessage(inner);\n if (deeper) return deeper;\n }\n }\n if (e.cause) {\n const deeper = deepestErrorMessage(e.cause);\n if (deeper) return deeper;\n }\n if (typeof e.message === \"string\" && e.message && !e.message.startsWith(\"Failed query:\")) {\n return e.code ? `${e.message} (${e.code})` : e.message;\n }\n return null;\n}\n\n/** One indented line naming the driver's own reason, or nothing. */\nfunction driverReasonLine(err: unknown): string {\n const reason = err === undefined ? null : deepestErrorMessage(err);\n return reason ? ` The driver said: ${reason}\\n\\n` : \"\";\n}\n\n/**\n * Format a diagnostic banner for ECONNREFUSED errors.\n */\nfunction formatConnectionRefusedBanner(databaseUrl: string, err?: unknown): string {\n const hostInfo = parseHostInfo(databaseUrl);\n return (\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n ` ❌ Cannot connect to PostgreSQL at ${hostInfo}\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n `\\n` +\n driverReasonLine(err) +\n ` The database server is not running or is not accepting\\n` +\n ` connections. Common fixes:\\n` +\n `\\n` +\n ` • docker compose up -d db (the service a Rebase scaffold ships)\\n` +\n ` • brew services start postgresql@18\\n` +\n ` • Verify DATABASE_URL in your .env file\\n` +\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n`\n );\n}\n\n/**\n * Format a diagnostic banner for authentication failures.\n */\nfunction formatAuthFailureBanner(databaseUrl: string, err?: unknown): string {\n const hostInfo = parseHostInfo(databaseUrl);\n let username = \"unknown\";\n try {\n username = new URL(databaseUrl).username || \"unknown\";\n } catch { /* ignore */ }\n\n return (\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n ` ❌ Authentication failed for user \"${username}\" at ${hostInfo}\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n `\\n` +\n driverReasonLine(err) +\n ` PostgreSQL rejected the credentials. Common fixes:\\n` +\n `\\n` +\n ` • Check the username and password in DATABASE_URL\\n` +\n ` • Verify the user exists: psql -c \"\\\\du\"\\n` +\n ` • Reset the password: ALTER USER ${username} PASSWORD 'new_password';\\n` +\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n`\n );\n}\n\n/**\n * Format a diagnostic banner for \"SSL is not enabled on the server\".\n */\nfunction formatSslNotEnabledBanner(databaseUrl: string): string {\n const hostInfo = parseHostInfo(databaseUrl);\n const suggestion = databaseUrl.includes(\"?\") ? \"&sslmode=disable\" : \"?sslmode=disable\";\n return (\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n ` ❌ SSL is not enabled on the PostgreSQL server at ${hostInfo}\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n `\\n` +\n ` The client tried to connect over SSL, but the server does not\\n` +\n ` support it. This is normal for a plain local dev database.\\n` +\n `\\n` +\n ` Fix: append ${chalk.bold(\"sslmode=disable\")} to DATABASE_URL, e.g.\\n` +\n `\\n` +\n ` DATABASE_URL=...${suggestion}\\n` +\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n`\n );\n}\n\n/**\n * Format a diagnostic banner for a dependency-drop failure during `db push`.\n * Explains that the database may be left partially migrated and how to recover.\n */\nfunction formatDependencyDropBanner(): string {\n return (\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n ` ❌ Schema push failed: a type/table could not be dropped\\n` +\n ` because other objects still depend on it.\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n `\\n` +\n ` ${chalk.yellow(\"The database may now be partially migrated.\")} Atlas applies\\n` +\n ` statements individually, so earlier changes in this push may\\n` +\n ` already be committed while later ones failed.\\n` +\n `\\n` +\n ` This commonly happens when a collection is removed but an enum\\n` +\n ` type it defined is still referenced. To recover:\\n` +\n `\\n` +\n ` 1. Inspect the leftover object named in the error above.\\n` +\n ` 2. Drop it with CASCADE, e.g.:\\n` +\n ` psql \"$DATABASE_URL\" -c 'DROP TYPE \"<name>\" CASCADE;'\\n` +\n ` 3. Re-run: ${chalk.bold.green(\"rebase db push\")}\\n` +\n `\\n` +\n ` Prefer a safe, versioned workflow? Use ${chalk.bold(\"rebase db generate\")}\\n` +\n ` + ${chalk.bold(\"rebase db migrate\")} instead of push for destructive changes.\\n` +\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n`\n );\n}\n\n/**\n * Pre-flight check: verify that the database is reachable before running\n * a heavy subprocess (Atlas, migrations, etc.).\n *\n * Exits with code 1 and a friendly banner on known failure modes.\n * On unknown errors, logs a warning and allows the caller to proceed.\n */\nexport async function checkDatabaseConnectivity(databaseUrl: string): Promise<void> {\n try {\n // On the command's own connection, which every step after this uses\n // too — so a probe that passes leaves it open rather than closing a\n // connection the next step would reopen. See `withAppClient`.\n await withAppClient(databaseUrl, client => client.query(\"SELECT 1\"), { connectionTimeoutMillis: 5000 });\n } catch (err: unknown) {\n if (isEconnrefused(err)) {\n outError(formatConnectionRefusedBanner(databaseUrl, err));\n process.exit(1);\n }\n if (isAuthFailure(err)) {\n outError(formatAuthFailureBanner(databaseUrl, err));\n process.exit(1);\n }\n if (isSslNotEnabled(err)) {\n outError(formatSslNotEnabledBanner(databaseUrl));\n process.exit(1);\n }\n // Unknown error — warn but don't block; let the downstream tool surface details\n outWarn(chalk.yellow(` ⚠ Could not verify database connectivity: ${err instanceof Error ? err.message : String(err)}`));\n outWarn(chalk.gray(\" Proceeding anyway — the command may fail if the database is unreachable.\"));\n }\n}\n\n/**\n * Post-hoc error diagnosis for direct database operations (e.g. applyPolicies).\n * Returns a formatted diagnostic string if the error matches a known pattern,\n * or null if unrecognized.\n */\nexport function diagnoseDbError(err: unknown, databaseUrl?: string): string | null {\n if (isEconnrefused(err)) {\n return formatConnectionRefusedBanner(databaseUrl || \"\", err);\n }\n if (isAuthFailure(err)) {\n return formatAuthFailureBanner(databaseUrl || \"\", err);\n }\n if (isSslNotEnabled(err)) {\n return formatSslNotEnabledBanner(databaseUrl || \"\");\n }\n if (isDependencyDropError(err)) {\n return formatDependencyDropBanner();\n }\n return null;\n}\n\n/**\n * Say why the command failed, on the way out.\n *\n * The entry point below used to be `.catch(() => process.exit(1))`, which threw\n * the error away. Every message this file and its services raise — \"Branch\n * \\\"x\\\" already exists.\", \"the source database has active connections\", \"Branch\n * name is too long\" — was written, wrapped in the right PG error code, and then\n * discarded one frame before it reached a terminal. What a developer saw was a\n * header line, no error, and exit 1.\n *\n * Two shapes are deliberately kept quiet:\n *\n * - **A child process that already spoke.** Atlas, `pg_dump` and `psql` run\n * with inherited stdio, so their diagnosis is on the terminal already and\n * execa's wrapper adds only `Command failed with exit code 1: atlas …`.\n * `packages/cli` filters exactly these two phrasings one level up\n * (`runDbCommand`), and this is that filter, for the process that is actually\n * throwing.\n *\n * - **A message that is only a query.** Drizzle reports failures as\n * `Failed query: <sql> params:` and hides the real PostgreSQL error in\n * `cause`, so the wrapper alone tells a reader nothing they can act on. The\n * cause is appended when it says something the message does not.\n *\n * - **An error that has already printed its own diagnosis**, marked\n * `alreadyReported`. `CollectionsPathMissing` is one: it prints the path, what\n * it resolved to and the cwd it resolved against, and then throws so the entry\n * point owns the exit code. Repeating its one-line summary underneath would\n * undo the \"printed once\" this whole path exists for.\n */\nexport function reportCommandFailure(error: unknown): void {\n const message = error instanceof Error ? error.message : String(error ?? \"\");\n\n if ((error as { alreadyReported?: boolean } | null)?.alreadyReported) return;\n if (!message || /Command failed|exited with code/i.test(message)) return;\n\n outError(\"\");\n outError(chalk.red(` ✗ ${message}`));\n\n const cause = extractCauseMessage(error);\n if (cause && !message.includes(cause)) {\n outError(chalk.gray(` ${cause}`));\n }\n outError(\"\");\n}\n\n/* ------------------------------------------------------------------------- *\n * Atlas failures that have a remedy\n *\n * Atlas runs with a teed stderr, so what reaches these is its output as text,\n * not a `pg` error object carrying a `code`. They are deliberately NOT part of\n * `diagnoseDbError`: that one is shared with the boot path, and \"record a\n * baseline with `rebase db migrate`\" is advice for somebody standing at a\n * terminal, not for a container that has just failed to start.\n * ------------------------------------------------------------------------- */\n\n/**\n * `migrate apply` refusing because the database already has the schema.\n *\n * `42710` (duplicate_object) and `42P07` (duplicate_table) are what a migration\n * hits when boot-ensure — or a `db push` — has already provisioned the objects\n * it was going to create. Since boot-ensure provisions *every* production\n * database, this is the normal case rather than the exotic one, and the raw\n * failure (`pq: type \"posts_status\" already exists (42710)`, then `sql/migrate:\n * write revision: … current transaction is aborted`) names no way forward.\n */\nexport function parseAlreadyProvisioned(text: string): { object: string; code: string } | null {\n const withCode = /(?:type|relation|constraint|schema) \"([^\"]+)\" already exists \\((42710|42P07)\\)/.exec(text);\n if (withCode) return { object: withCode[1], code: withCode[2] };\n // Some Atlas builds keep the SQLSTATE on a following line instead.\n const bare = /(?:type|relation|constraint|schema) \"([^\"]+)\" already exists/.exec(text);\n if (bare && /42710|42P07/.test(text)) return { object: bare[1], code: /42P07/.test(text) ? \"42P07\" : \"42710\" };\n return null;\n}\n\n/**\n * What to do about it: tell Atlas the database is already at a version.\n *\n * Atlas's own mechanism (`migrate apply --baseline <version>`), not a ledger of\n * ours — it writes the revision row Atlas reads, so every later `rebase db\n * migrate` is an ordinary one.\n */\nexport function formatBaselineRemedy(version: string | null, object?: string): string {\n const versionToken = version ?? \"<version>\";\n return (\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n ` ❌ This database already has the schema this migration creates\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n `\\n` +\n (object ? ` ${chalk.gray(`\"${object}\" is already there, so the migration cannot create it.`)}\\n\\n` : \"\") +\n ` Nothing is wrong with the migration — the database was provisioned\\n` +\n ` another way. Every Rebase boot ensures the schema, and ${chalk.bold(\"rebase db push\")}\\n` +\n ` applies it directly, so a database that has ever run either one is\\n` +\n ` ahead of a migration history that was never recorded.\\n` +\n `\\n` +\n ` Record where it already is, then migrate normally:\\n` +\n `\\n` +\n ` ${chalk.bold.green(`rebase db migrate --baseline ${versionToken}`)}\\n` +\n ` ${chalk.bold.green(\"rebase db migrate\")}\\n` +\n `\\n` +\n ` The baseline is the version already applied — the numeric prefix of\\n` +\n ` the migration file describing what is in the database now. Migrations\\n` +\n ` after it run; it and everything before it are marked done.\\n` +\n `\\n` +\n ` On a database nothing has ever booted against, none of this is needed.\\n` +\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n`\n );\n}\n\n/** `ALTER TABLE … SET NOT NULL` on a table that already holds rows (23502). */\nexport function parseNotNullViolation(text: string): { table: string; column: string } | null {\n const match = /column \"([^\"]+)\" of relation \"([^\"]+)\" contains null values/.exec(text);\n return match ? { table: match[2], column: match[1] } : null;\n}\n\n/**\n * Whether the column that refused NOT NULL was being added by this push, read\n * from the statement Atlas was running: `true` for `ADD COLUMN \"<column>\"`,\n * `false` for any other statement that names it (`ALTER COLUMN … SET NOT\n * NULL`), `null` when the error carried no statement.\n */\nexport function notNullColumnIsNew(stderr: string, column: string): boolean | null {\n const statement = parseFailedStatement(stderr);\n if (!statement) return null;\n const quoted = column.replace(/\"/g, \"\\\"\\\"\").replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n return new RegExp(`ADD\\\\s+COLUMN\\\\s+(?:IF\\\\s+NOT\\\\s+EXISTS\\\\s+)?\"${quoted}\"`, \"i\").test(statement);\n}\n\n/**\n * The ways out, and only the ones that work for this column.\n *\n * A `defaultValue` backfills only a column the push is adding: PostgreSQL\n * fills `ADD COLUMN … NOT NULL DEFAULT …` for every existing row, but `ALTER\n * COLUMN … SET DEFAULT` touches no row that is already there, so for an\n * existing column that remedy fails exactly as before. When the error does not\n * say which it was (`isNew` null), the default is offered with that caveat.\n *\n * Boot-ensure handles this case — it adds the column nullable and sets NOT NULL\n * only when the table is empty — so a push that dies here is strictly worse\n * than the boot that would have run instead.\n */\nexport function formatNotNullViolationBanner(\n violation: { table: string; column: string },\n rowCount: number | null,\n isNew: boolean | null = null\n): string {\n const { table, column } = violation;\n const rows = rowCount === null\n ? ` \"${table}\" already holds rows, and none of them has a value for \"${column}\".\\n`\n : ` \"${table}\" already holds ${rowCount} row${rowCount === 1 ? \"\" : \"s\"}, and they have no value for \"${column}\".\\n`;\n const backfill =\n `Backfill by hand first, then push again:\\n` +\n ` ${chalk.gray(`psql \"$DATABASE_URL\" -c 'UPDATE \"${table}\" SET \"${column}\" = … WHERE \"${column}\" IS NULL;'`)}\\n`;\n const leaveOptional = `Leave it optional — take \\`validation.required\\` off the property.\\n`;\n const defaultLine = (caveat: string) =>\n `Give the property a default, so the push can backfill${caveat}:\\n` +\n ` ${chalk.gray(`{ type: \"string\", defaultValue: \"…\", validation: { required: true } }`)}\\n`;\n const remedies = isNew === false\n ? [backfill, leaveOptional]\n : isNew === true\n ? [defaultLine(\"\"), backfill, leaveOptional]\n : [backfill, defaultLine(\" (only if this push adds the column — a default never fills rows that already exist)\"), leaveOptional];\n const count = [\"\", \"One way\", \"Two ways\", \"Three ways\"][remedies.length];\n return (\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n ` ❌ Cannot make \"${table}\".\"${column}\" required: existing rows are null\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n `\\n` +\n rows +\n ` PostgreSQL will not add the NOT NULL until every one of them does.\\n` +\n `\\n` +\n ` ${count} forward:\\n` +\n `\\n` +\n remedies.map((r, i) => ` ${i + 1}. ${r}`).join(\"\") +\n `\\n` +\n ` ${chalk.gray(\"Nothing in this push was applied: Atlas applies it as one transaction.\")}\\n` +\n ` ${chalk.gray(\"`rebase dev` would have kept the column nullable instead:\")}\\n` +\n ` ${chalk.gray(\"boot sets NOT NULL only when it is safe.\")}\\n` +\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n`\n );\n}\n\n/** Atlas refusing to remove a label from an enum — a renamed `enum` option id. */\nexport function parseEnumLabelDrop(text: string): { label: string; enumType: string } | null {\n const match = /dropping (?:enum )?value \"([^\"]+)\" from enum \"([^\"]+)\" is not supported/.exec(text);\n return match ? { label: match[1], enumType: match[2] } : null;\n}\n\n/**\n * Why the two paths disagree, and what retiring an option id actually costs.\n *\n * Boot-ensure adds enum labels and never removes one, so `rebase dev` accepts\n * the very edit that stops `db push` dead. PostgreSQL has no `ALTER TYPE …\n * DROP VALUE`: a label goes only by rewriting the type, which is a data\n * migration and not a schema push.\n */\nexport function formatEnumLabelDropBanner(drop: { label: string; enumType: string }): string {\n const { label, enumType } = drop;\n return (\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n ` ❌ \"${label}\" cannot be removed from the enum \"${enumType}\"\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n `\\n` +\n ` PostgreSQL has no ALTER TYPE … DROP VALUE. An option id can be added\\n` +\n ` to an enum but never taken out of one, so renaming an id reads as a\\n` +\n ` removal and a push cannot carry it.\\n` +\n `\\n` +\n ` ${chalk.yellow(\"Nothing was applied.\")} ${chalk.bold(\"rebase dev\")} accepts this same edit — boot adds\\n` +\n ` new labels and never removes one — so the two paths disagree by\\n` +\n ` design, and the old label would simply have stayed behind, unused.\\n` +\n `\\n` +\n ` To keep the id: put \"${label}\" back and change only its label text.\\n` +\n `\\n` +\n ` To retire it for real, it is a data migration:\\n` +\n `\\n` +\n ` 1. Add the new id alongside the old one, and push.\\n` +\n ` 2. Move the rows: ${chalk.gray(`UPDATE … SET … = '<new>' WHERE … = '${label}';`)}\\n` +\n ` 3. Rewrite the type, which is what actually drops the label:\\n` +\n ` ${chalk.gray(\"rebase db generate retire_option && rebase db migrate\")}\\n` +\n ` ${chalk.gray(\"(a migration can CREATE TYPE …, ALTER TABLE … TYPE … USING, DROP TYPE)\")}\\n` +\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n`\n );\n}\n\n/**\n * The statement Atlas was running when it failed, unescaped.\n *\n * Two spellings, one per phase: the apply quotes it\n * (`executing statement \"ALTER TABLE \\\"public\\\".\\\"people\\\" …;\": pq: …`), and\n * the dry-run's analysis does not\n * (`analyzing plan: executing statement: ALTER TABLE \"public\".\"people\" …;: pq: …`).\n */\nexport function parseFailedStatement(text: string): string | null {\n const quoted = /executing statement \"((?:[^\"\\\\]|\\\\.)*)\"/.exec(text);\n if (quoted) return quoted[1].replace(/\\\\(.)/g, \"$1\");\n const bare = /executing statement: ([\\s\\S]*?;): pq:/.exec(text);\n return bare ? bare[1] : null;\n}\n\n/** `ALTER TABLE \"public\".\"people\" …` → `{ schema: \"public\", table: \"people\" }`. */\nexport function failedStatementTable(statement: string): { schema?: string; table: string } | null {\n const ident = String.raw`(?:\"((?:[^\"]|\"\")+)\"|([A-Za-z_][A-Za-z0-9_$]*))`;\n const match = new RegExp(String.raw`^\\s*(?:ALTER\\s+TABLE|CREATE\\s+UNIQUE\\s+INDEX\\s+\\S+\\s+ON)\\s+(?:ONLY\\s+)?${ident}(?:\\.${ident})?`, \"i\")\n .exec(statement);\n if (!match) return null;\n const first = (match[1] ?? match[2]).replace(/\"\"/g, \"\\\"\");\n const second = match[3] ?? match[4];\n return second === undefined\n ? { table: first }\n : { schema: first, table: second.replace(/\"\"/g, \"\\\"\") };\n}\n\n/** A generated column standing in the way of a retype, as the catalogue has it. */\nexport interface GeneratedColumnBlocker {\n schema: string;\n table: string;\n /** The generated column. */\n column: string;\n /** The column it reads, which the statement retypes. */\n reads: string;\n}\n\n/**\n * PostgreSQL refusing to retype a column a `GENERATED ALWAYS AS … STORED`\n * expression reads (0A000). `db push` moves Rebase's own search columns out of\n * the way before the apply; this is what is left when it could not.\n */\nexport function parseGeneratedColumnRefusal(text: string): { statement: string | null } | null {\n if (!/cannot alter type of a column used by a generated column/.test(text)) return null;\n return { statement: parseFailedStatement(text) };\n}\n\n/**\n * Which generated columns read a column the failed statement retypes — read\n * from the catalogue, because PostgreSQL's DETAIL naming them does not survive\n * Atlas.\n */\nasync function generatedColumnsBlocking(\n databaseUrl: string | undefined,\n statement: string | null\n): Promise<GeneratedColumnBlocker[]> {\n if (!databaseUrl || !statement) return [];\n const target = failedStatementTable(statement);\n if (!target) return [];\n const retyped = [...statement.matchAll(/ALTER\\s+(?:COLUMN\\s+)?\"((?:[^\"]|\"\")+)\"\\s+(?:SET\\s+DATA\\s+)?TYPE\\b/gi)]\n .map(match => match[1].replace(/\"\"/g, \"\\\"\"));\n if (retyped.length === 0) return [];\n try {\n const { Client } = await import(\"pg\");\n const client = new Client({ connectionString: databaseUrl, connectionTimeoutMillis: 5000 });\n await client.connect();\n try {\n const res = await client.query<GeneratedColumnBlocker>(`\n SELECT n.nspname AS \"schema\", c.relname AS \"table\", gen.attname AS \"column\", dep.attname AS \"reads\"\n FROM pg_attrdef ad\n JOIN pg_class c ON c.oid = ad.adrelid\n JOIN pg_namespace n ON n.oid = c.relnamespace\n JOIN pg_attribute gen ON gen.attrelid = ad.adrelid AND gen.attnum = ad.adnum\n JOIN pg_depend d ON d.classid = 'pg_attrdef'::regclass AND d.objid = ad.oid\n AND d.refclassid = 'pg_class'::regclass AND d.refobjid = ad.adrelid\n JOIN pg_attribute dep ON dep.attrelid = ad.adrelid AND dep.attnum = d.refobjsubid\n WHERE gen.attgenerated <> '' AND c.relname = $1\n AND ($2::text IS NULL OR n.nspname = $2) AND dep.attname = ANY($3::text[])\n ORDER BY 1, 2, 3, 4`, [target.table, target.schema ?? null, retyped]);\n return res.rows;\n } finally {\n await client.end();\n }\n } catch {\n return [];\n }\n}\n\n/**\n * What to do when a generated column stops a retype.\n *\n * A collection's own search column should never reach this — `db push` drops\n * it before the apply and rebuilds it after — so seeing one named here means\n * that machinery missed it, which is a bug worth a report. Any other generated\n * column is somebody's, with an expression Rebase holds no copy of.\n */\nexport function formatGeneratedColumnRefusalBanner(\n refusal: { statement: string | null },\n blockers: GeneratedColumnBlocker[]\n): string {\n const named = blockers.length > 0\n ? blockers.map(b => ` ${chalk.bold(`${b.schema}.${b.table}.${b.column}`)} reads \"${b.reads}\"`).join(\"\\n\") + \"\\n\\n\"\n : refusal.statement\n ? ` The statement:\\n ${chalk.gray(refusal.statement.replace(/\\s+/g, \" \"))}\\n\\n`\n : \"\";\n const first = blockers[0];\n const drop = first\n ? `ALTER TABLE \"${first.schema}\".\"${first.table}\" DROP COLUMN \"${first.column}\";`\n : \"ALTER TABLE … DROP COLUMN <the generated column>;\";\n return (\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n ` ❌ A generated column reads a column this push retypes\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n `\\n` +\n named +\n ` PostgreSQL will not retype a column while a GENERATED ALWAYS AS …\\n` +\n ` STORED expression reads it, and Atlas applies a push as one\\n` +\n ` transaction — ${chalk.yellow(\"nothing in this push was applied.\")}\\n` +\n `\\n` +\n ` A collection's ${chalk.bold(\"search\")} column is dropped and rebuilt around the apply\\n` +\n ` by the push itself. If that is the column named above, run the push\\n` +\n ` again, and please report it: it should not have got this far.\\n` +\n `\\n` +\n ` Any other generated column is yours. Drop it, push, and put it back\\n` +\n ` with its original expression:\\n` +\n `\\n` +\n ` ${chalk.gray(drop)}\\n` +\n ` ${chalk.gray(\"rebase db push\")}\\n` +\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n`\n );\n}\n\n/**\n * PostgreSQL refusing a retype it has no implicit cast for (42804): `string` →\n * `number`, `time` → `date`, an array back to a string. Atlas reports it from\n * the dry-run's analysis as well as from the apply, so without this a\n * `--dry-run` died with the raw error and showed no plan at all.\n */\nexport function parseUncastableRetype(text: string): { column: string; type: string; statement: string | null } | null {\n const match = /column \"((?:[^\"]|\"\")+)\" cannot be cast automatically to type ([^\\n(]+?)\\s*(?:\\(42804\\)|$|\\n)/m.exec(text);\n if (!match) return null;\n return { column: match[1], type: match[2].trim(), statement: parseFailedStatement(text) };\n}\n\n/** The `ALTER … USING` a developer would run, and the other ways past it. Boot's drift report says the same. */\nexport function formatUncastableRetypeBanner(refusal: { column: string; type: string; statement: string | null }): string {\n const { column, type, statement } = refusal;\n const target = statement ? failedStatementTable(statement) : null;\n const table = target ? `${target.schema ? `\"${target.schema}\".` : \"\"}\"${target.table}\"` : \"<table>\";\n const quotedColumn = `\"${column.replace(/\"/g, \"\\\"\\\"\")}\"`;\n return (\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n ` ❌ ${table}.${quotedColumn} cannot be converted to ${type} automatically\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n `\\n` +\n ` The collection now declares this column as ${type}, and PostgreSQL has\\n` +\n ` no implicit conversion from what the column holds today. Atlas stops\\n` +\n ` at the first such statement — ${chalk.yellow(\"nothing in this push was applied\")}, and a\\n` +\n ` --dry-run cannot show the rest of the plan until this one is settled.\\n` +\n `\\n` +\n ` Three ways forward:\\n` +\n `\\n` +\n ` 1. Convert the values yourself, saying how, then push again:\\n` +\n ` ${chalk.gray(`ALTER TABLE ${table} ALTER COLUMN ${quotedColumn} TYPE ${type} USING ${quotedColumn}::${type};`)}\\n` +\n ` ${chalk.gray(\"(adjust the USING expression to whatever turns each old value into a new one)\")}\\n` +\n ` 2. Add a new property for the new type, copy the values over with an\\n` +\n ` UPDATE, then remove the old property.\\n` +\n ` 3. Keep the column as it is — put the property's type back, or pin the\\n` +\n ` database type with ${chalk.bold(\"columnType\")}.\\n` +\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n`\n );\n}\n\n/**\n * A unique constraint or index PostgreSQL could not build because the rows\n * already repeat a value (23505). The DETAIL naming the value does not survive\n * Atlas, so the duplicates are read back from the table.\n */\nexport function parseUniqueBuildFailure(text: string): { index: string; statement: string | null; columns: string[] } | null {\n const match = /could not create unique index \"((?:[^\"]|\"\")+)\"/.exec(text);\n if (!match) return null;\n const statement = parseFailedStatement(text);\n let columns: string[] = [];\n if (statement) {\n const list = /UNIQUE\\s*\\(([^)]*)\\)/i.exec(statement)\n ?? /\\bON\\s+(?:ONLY\\s+)?(?:\"(?:[^\"]|\"\")+\"|[A-Za-z_][\\w$]*)(?:\\.(?:\"(?:[^\"]|\"\")+\"|[A-Za-z_][\\w$]*))?\\s*(?:USING\\s+\\w+\\s*)?\\(([^)]*)\\)/i.exec(statement);\n if (list) {\n columns = [...list[1].matchAll(/\"((?:[^\"]|\"\")+)\"|([A-Za-z_][\\w$]*)/g)]\n .map(m => (m[1] ?? m[2]).replace(/\"\"/g, \"\\\"\"));\n }\n }\n return { index: match[1], statement, columns };\n}\n\n/** Up to five repeated values, read from the database, or `[]` when we cannot say. */\nasync function duplicateValues(\n databaseUrl: string | undefined,\n target: { schema?: string; table: string } | null,\n columns: string[]\n): Promise<{ values: string; count: number }[]> {\n if (!databaseUrl || !target || columns.length === 0) return [];\n const ident = (name: string) => `\"${name.replace(/\"/g, \"\\\"\\\"\")}\"`;\n const table = `${target.schema ? `${ident(target.schema)}.` : \"\"}${ident(target.table)}`;\n const cols = columns.map(ident).join(\", \");\n try {\n const { Client } = await import(\"pg\");\n const client = new Client({ connectionString: databaseUrl, connectionTimeoutMillis: 5000 });\n await client.connect();\n try {\n // Identifiers from the failed statement, quoted; no value is interpolated.\n const res = await client.query<{ values: string; count: number }>(\n `SELECT concat_ws(', ', ${columns.map(c => `${ident(c)}::text`).join(\", \")}) AS \"values\", count(*)::int AS \"count\"\n FROM ${table} GROUP BY ${cols} HAVING count(*) > 1 ORDER BY count(*) DESC LIMIT 5`);\n return res.rows;\n } finally {\n await client.end();\n }\n } catch {\n return [];\n }\n}\n\nexport function formatUniqueBuildFailureBanner(\n failure: { index: string; statement: string | null; columns: string[] },\n duplicates: { values: string; count: number }[]\n): string {\n const target = failure.statement ? failedStatementTable(failure.statement) : null;\n const table = target ? `${target.schema ? `\"${target.schema}\".` : \"\"}\"${target.table}\"` : \"<table>\";\n const cols = failure.columns.length > 0 ? failure.columns.map(c => `\"${c}\"`).join(\", \") : \"<column>\";\n const listed = duplicates.length > 0\n ? ` Repeated values:\\n` + duplicates.map(d => ` ${chalk.bold(d.values)} ${chalk.gray(`× ${d.count}`)}`).join(\"\\n\") + `\\n\\n`\n : \"\";\n return (\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n ` ❌ Cannot make ${table} (${cols}) unique: rows already repeat a value\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n `\\n` +\n listed +\n ` PostgreSQL will not build \"${failure.index}\" while two rows share a\\n` +\n ` value, and Atlas applies a push as one transaction — ${chalk.yellow(\"nothing in\")}\\n` +\n ` ${chalk.yellow(\"this push was applied.\")}\\n` +\n `\\n` +\n ` Find them:\\n` +\n ` ${chalk.gray(`SELECT ${cols}, count(*) FROM ${table} GROUP BY ${cols} HAVING count(*) > 1;`)}\\n` +\n `\\n` +\n ` Then change or delete the repeats and push again, or take ${chalk.bold(\"unique\")} off\\n` +\n ` the property.\\n` +\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n`\n );\n}\n\n/** How many rows a table holds, or `null` when we cannot say. */\nasync function countRows(databaseUrl: string | undefined, table: string): Promise<number | null> {\n if (!databaseUrl) return null;\n try {\n const { Client } = await import(\"pg\");\n const client = new Client({ connectionString: databaseUrl, connectionTimeoutMillis: 5000 });\n await client.connect();\n try {\n // Identifier interpolation, and it has to be: the name comes from\n // PostgreSQL's own error text, and `count(*)` takes no parameter in\n // that position. Quoted, with any embedded quote doubled.\n const res = await client.query(`SELECT count(*)::int AS n FROM \"${table.replace(/\"/g, \"\\\"\\\"\")}\"`);\n return (res.rows[0] as { n: number } | undefined)?.n ?? null;\n } finally {\n await client.end();\n }\n } catch {\n return null;\n }\n}\n\n/**\n * A hand-written generated column reading a column the plan wants to retype or\n * drop.\n *\n * Rebase's own generated columns are dropped and rebuilt around the apply — it\n * holds their definition in `search.sql`. This one it does not: PostgreSQL\n * stores a generated expression only in the catalogue, so dropping it to let\n * the apply through would destroy the only copy. Refuse instead, and say what\n * the operator can do about it.\n */\nexport function formatForeignGeneratedColumnBanner(\n conflicts: { schema: string; table: string; column: string; blocking: { column: string; kind: string }[] }[]\n): string {\n const lines = conflicts.map(c => {\n const reads = c.blocking.map(b => `\"${b.column}\"`).join(\", \");\n return ` ${chalk.bold(`${c.schema}.${c.table}.${c.column}`)} reads ${reads}`;\n });\n const first = conflicts[0];\n return (\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n ` ❌ A generated column depends on a column this push changes\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n` +\n `\\n` +\n lines.join(\"\\n\") + `\\n` +\n `\\n` +\n ` PostgreSQL refuses to retype or drop a column while a GENERATED\\n` +\n ` ALWAYS AS … STORED expression reads it, and an Atlas apply is one\\n` +\n ` transaction — so this refusal would roll back every other statement\\n` +\n ` in the push with it.\\n` +\n `\\n` +\n ` ${chalk.yellow(\"Nothing was applied.\")} Rebase rebuilds the generated columns it made\\n` +\n ` itself (a collection's ${chalk.bold(\"search\")} block) around the apply. It has no\\n` +\n ` definition for this one, and a dropped generated expression is not\\n` +\n ` recoverable from anywhere but your own migration.\\n` +\n `\\n` +\n ` Drop it, push, and put it back:\\n` +\n `\\n` +\n ` ${chalk.gray(`ALTER TABLE \"${first.schema}\".\"${first.table}\" DROP COLUMN \"${first.column}\";`)}\\n` +\n ` ${chalk.gray(\"rebase db push\")}\\n` +\n ` ${chalk.gray(\"-- then re-create the column with its original expression\")}\\n` +\n `\\n` +\n ` Or declare it through a ${chalk.bold(\"search\")} block, and Rebase will own it.\\n` +\n `\\n` +\n `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n`\n );\n}\n\n/**\n * The remedy for an Atlas invocation that failed, or `null`.\n *\n * Scoped by the invocation, because the same database state means different\n * things to different subcommands: \"already exists\" under `migrate apply` wants\n * a baseline, while under `schema apply` it is a genuine conflict.\n */\nexport async function diagnoseAtlasFailure(context: {\n domain: string;\n args: string[];\n stderr: string;\n databaseUrl?: string;\n /** The newest migration version on disk, named in the baseline remedy. */\n latestMigrationVersion?: string | null;\n}): Promise<string | null> {\n const { domain, args, stderr, databaseUrl } = context;\n\n if (domain === \"migrate\" && args.includes(\"apply\")) {\n const provisioned = parseAlreadyProvisioned(stderr);\n if (provisioned) {\n return formatBaselineRemedy(context.latestMigrationVersion ?? null, provisioned.object);\n }\n }\n\n if (domain === \"schema\" && args.includes(\"apply\")) {\n const enumDrop = parseEnumLabelDrop(stderr);\n if (enumDrop) return formatEnumLabelDropBanner(enumDrop);\n\n const notNull = parseNotNullViolation(stderr);\n if (notNull) {\n return formatNotNullViolationBanner(notNull, await countRows(databaseUrl, notNull.table), notNullColumnIsNew(stderr, notNull.column));\n }\n\n const uncastable = parseUncastableRetype(stderr);\n if (uncastable) return formatUncastableRetypeBanner(uncastable);\n\n const unique = parseUniqueBuildFailure(stderr);\n if (unique) {\n const target = unique.statement ? failedStatementTable(unique.statement) : null;\n return formatUniqueBuildFailureBanner(unique, await duplicateValues(databaseUrl, target, unique.columns));\n }\n\n const generated = parseGeneratedColumnRefusal(stderr);\n if (generated) {\n return formatGeneratedColumnRefusalBanner(generated, await generatedColumnsBlocking(databaseUrl, generated.statement));\n }\n }\n\n return null;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAMA,SAAgB,oBAAoB,OAA+B;CAC/D,IAAI,CAAC,SAAS,OAAO,UAAU,UAAU,OAAO;CAChD,IAAI,EAAE,iBAAiB,QAAQ,OAAO;CAEtC,IAAI,MAAM,SAAS,OAAO,MAAM,UAAU,UAAU;EAChD,MAAM,SAAS,oBAAoB,MAAM,KAAK;EAC9C,IAAI,QAAQ,OAAO;EAEnB,IAAI,MAAM,iBAAiB,SAAS,MAAM,MAAM,SAC5C,OAAO,MAAM,MAAM;CAE3B;CACA,OAAO;AACX;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACYA,IAAM,uBAAwD;CAC1D;EAAE,OAAO;EAAc,IAAI;CAAoB;CAC/C;EAAE,OAAO;EAAe,IAAI;CAAqB;CACjD;EAAE,OAAO;EAAe,IAAI;CAAqB;CACjD;EAAE,OAAO;EAAa,IAAI;CAAqC;CAC/D;EAAE,OAAO;EAAa,IAAI;CAAmB;CAC7C;EAAE,OAAO;EAAY,IAAI;CAAgB;AAC7C;;;;;;;AAQA,SAAgB,mBAAmB,KAAuB;CAOtD,OAJwB,IACnB,MAAM,IAAI,CAAC,CACX,QAAQ,SAAS,CAAC,KAAK,KAAK,CAAC,CAAC,WAAW,IAAI,CAAC,CAAC,CAC/C,KAAK,IACH,CAAA,CACF,MAAM,GAAG,CAAC,CACV,KAAK,MAAM,EAAE,KAAK,CAAC,CAAC,CACpB,QAAQ,MAAM,EAAE,SAAS,CAAC;AACnC;;AAGA,IAAM,iBAAiB;;AAEvB,IAAM,eAAe;;A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OAAO,OAAO,CAAC;EAG5G,MAAM,YAAY,4BAA4B,MAAM;EACpD,IAAI,WACA,OAAO,mCAAmC,WAAW,MAAM,yBAAyB,aAAa,UAAU,SAAS,CAAC;CAE7H;CAEA,OAAO;AACX"}
@@ -119,18 +119,29 @@ export declare function parseNotNullViolation(text: string): {
119
119
  column: string;
120
120
  } | null;
121
121
  /**
122
- * The three ways out, because there is no fourth.
122
+ * Whether the column that refused NOT NULL was being added by this push, read
123
+ * from the statement Atlas was running: `true` for `ADD COLUMN "<column>"`,
124
+ * `false` for any other statement that names it (`ALTER COLUMN … SET NOT
125
+ * NULL`), `null` when the error carried no statement.
126
+ */
127
+ export declare function notNullColumnIsNew(stderr: string, column: string): boolean | null;
128
+ /**
129
+ * The ways out, and only the ones that work for this column.
130
+ *
131
+ * A `defaultValue` backfills only a column the push is adding: PostgreSQL
132
+ * fills `ADD COLUMN … NOT NULL DEFAULT …` for every existing row, but `ALTER
133
+ * COLUMN … SET DEFAULT` touches no row that is already there, so for an
134
+ * existing column that remedy fails exactly as before. When the error does not
135
+ * say which it was (`isNew` null), the default is offered with that caveat.
123
136
  *
124
137
  * Boot-ensure handles this case — it adds the column nullable and sets NOT NULL
125
138
  * only when the table is empty — so a push that dies here is strictly worse
126
- * than the boot that would have run instead, and the developer deserves to be
127
- * told which of the three they want rather than left with `pq: … contains null
128
- * values (23502)` and no next step.
139
+ * than the boot that would have run instead.
129
140
  */
130
141
  export declare function formatNotNullViolationBanner(violation: {
131
142
  table: string;
132
143
  column: string;
133
- }, rowCount: number | null): string;
144
+ }, rowCount: number | null, isNew?: boolean | null): string;
134
145
  /** Atlas refusing to remove a label from an enum — a renamed `enum` option id. */
135
146
  export declare function parseEnumLabelDrop(text: string): {
136
147
  label: string;
@@ -190,6 +201,41 @@ export declare function parseGeneratedColumnRefusal(text: string): {
190
201
  export declare function formatGeneratedColumnRefusalBanner(refusal: {
191
202
  statement: string | null;
192
203
  }, blockers: GeneratedColumnBlocker[]): string;
204
+ /**
205
+ * PostgreSQL refusing a retype it has no implicit cast for (42804): `string` →
206
+ * `number`, `time` → `date`, an array back to a string. Atlas reports it from
207
+ * the dry-run's analysis as well as from the apply, so without this a
208
+ * `--dry-run` died with the raw error and showed no plan at all.
209
+ */
210
+ export declare function parseUncastableRetype(text: string): {
211
+ column: string;
212
+ type: string;
213
+ statement: string | null;
214
+ } | null;
215
+ /** The `ALTER … USING` a developer would run, and the other ways past it. Boot's drift report says the same. */
216
+ export declare function formatUncastableRetypeBanner(refusal: {
217
+ column: string;
218
+ type: string;
219
+ statement: string | null;
220
+ }): string;
221
+ /**
222
+ * A unique constraint or index PostgreSQL could not build because the rows
223
+ * already repeat a value (23505). The DETAIL naming the value does not survive
224
+ * Atlas, so the duplicates are read back from the table.
225
+ */
226
+ export declare function parseUniqueBuildFailure(text: string): {
227
+ index: string;
228
+ statement: string | null;
229
+ columns: string[];
230
+ } | null;
231
+ export declare function formatUniqueBuildFailureBanner(failure: {
232
+ index: string;
233
+ statement: string | null;
234
+ columns: string[];
235
+ }, duplicates: {
236
+ values: string;
237
+ count: number;
238
+ }[]): string;
193
239
  /**
194
240
  * A hand-written generated column reading a column the plan wants to retype or
195
241
  * drop.
@@ -54,7 +54,10 @@ var DRIVER_FLAG_SPECS = {
54
54
  "--collections": String,
55
55
  "-c": "--collections"
56
56
  },
57
- "db migrate": { "--baseline": String },
57
+ "db migrate": {
58
+ "--baseline": String,
59
+ "--refuse-destructive": Boolean
60
+ },
58
61
  "schema generate": {
59
62
  "--collections": String,
60
63
  "-c": "--collections",
@@ -133,6 +136,6 @@ function assertKnownFlags(domain, subcommand, args) {
133
136
  }
134
137
  }
135
138
  //#endregion
136
- export { collectionsPathIn as n, parseDriverLine as r, assertKnownFlags as t };
139
+ export { parseDriverLine as i, assertKnownFlags as n, collectionsPathIn as r, RELAYED_FLAGS as t };
137
140
 
138
- //# sourceMappingURL=cli-flags-BglvpjHv.js.map
141
+ //# sourceMappingURL=cli-flags-JNI-pwSf.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli-flags-JNI-pwSf.js","names":[],"sources":["../src/cli-flags.ts"],"sourcesContent":["/**\n * The flags each driver command takes, in one place, and the check that\n * enforces them.\n *\n * Every parser in `cli.ts` runs `arg(..., { permissive: true })`, which does not\n * mean \"be lenient\" — it means **an undeclared flag becomes a positional**. So\n * `rebase db push --alow-destructive` did not fail: the typo landed in `_`, the\n * push ran with the destructive gate still closed, and the developer read the\n * refusal as Rebase ignoring the flag they had just typed. `rebase schema\n * generate --ouput src/schema.ts` was worse — it wrote the default path and\n * said nothing, so the next build compiled a file nobody had regenerated.\n *\n * The check has to live at the entry point rather than in those parsers, and\n * that is not a detail: `db push` and `db generate` re-enter `schemaCommand`\n * and `generatePostgresDdlCommand` with the *db* line, so a strict parser\n * inside either of them would reject `--allow-destructive` on a line where it\n * is correct. One validation, once, against the spec for the command the user\n * actually named; the inner parsers keep reading an already-validated line.\n *\n * Commands with their own argument handling — `db branch` (`unexpectedBranchArgs`),\n * `db backup`, `db restore`, `db backups` — are deliberately absent: they own\n * their spec, and a second list here would be one more thing to keep in step.\n * An absent entry is \"not checked here\", never \"takes nothing\".\n */\nimport arg from \"arg\";\n\n/**\n * Flags that reach this process on a line it did not compose.\n *\n * `rebase db …` hands the driver the user's whole line: the CLI *reads*\n * `--database-url` and `--docker` to resolve the database and then relays them\n * verbatim, and `--debug` is what `bin/rebase.js` prints after every failure as\n * the thing to re-run with. Rejecting any of the three would make the driver\n * refuse commands the CLI documents.\n */\nexport const RELAYED_FLAGS = {\n \"--database-url\": String,\n \"--docker\": Boolean,\n \"--debug\": Boolean,\n \"--help\": Boolean,\n \"-h\": \"--help\"\n} satisfies arg.Spec;\n\n/**\n * Parse a driver command's line — `[\"db\", \"<subcommand>\", …]` — with the\n * relayed flags declared alongside the command's own.\n *\n * Every driver parser is permissive, and an undeclared flag in a permissive\n * parse is a positional. The positionals are what `db migrate` hands\n * `atlas migrate apply`, what `db generate` names its migration after and\n * which backup `db restore` restores, so a relayed flag left undeclared was\n * read as one of those: `rebase db migrate --docker` died on Atlas's\n * `unknown flag: --docker` after it had already written the auth schema.\n */\nexport function parseDriverLine<S extends arg.Spec>(\n spec: S,\n args: string[]\n): arg.Result<typeof RELAYED_FLAGS & S> {\n return arg({ ...RELAYED_FLAGS, ...spec }, { argv: args.slice(2), permissive: true });\n}\n\n/** Keyed by `\"<domain> <subcommand>\"`, the way the user types it. */\nexport const DRIVER_FLAG_SPECS: Record<string, arg.Spec> = {\n \"db push\": {\n \"--collections\": String,\n \"-c\": \"--collections\",\n \"--dry-run\": Boolean,\n \"--allow-destructive\": Boolean,\n \"--yes\": Boolean,\n \"-y\": \"--yes\"\n },\n \"db generate\": {\n \"--collections\": String,\n \"-c\": \"--collections\"\n },\n // Bare positionals still pass through to `atlas migrate apply`, which takes\n // an optional amount; flags do not, apart from `--baseline`.\n //\n // `--baseline <version>` is the escape hatch for the normal case, not an\n // exotic one: every Rebase boot ensures the schema, so a production\n // database has the tables before its first migration ever runs, and\n // `migrate apply` then dies on `type \"…\" already exists (42710)`. It maps\n // onto Atlas's own `migrate apply --baseline`, which writes the revision\n // row Atlas reads — no ledger of ours.\n //\n // `--refuse-destructive` is opt-in: it dry-runs the pending files and exits\n // 1, applying nothing, when one drops or narrows data — the gate `db push`\n // has always had, for the path that had none.\n \"db migrate\": {\n \"--baseline\": String,\n \"--refuse-destructive\": Boolean\n },\n \"schema generate\": {\n \"--collections\": String,\n \"-c\": \"--collections\",\n \"--output\": String,\n \"-o\": \"--output\",\n \"--out\": \"--output\",\n \"--watch\": Boolean,\n \"-w\": \"--watch\"\n },\n \"schema introspect\": {\n \"--output\": String,\n \"-o\": \"--output\",\n \"--out\": \"--output\",\n \"--collections\": String,\n \"-c\": \"--collections\",\n \"--force\": Boolean,\n \"-f\": \"--force\",\n \"--schema\": String\n },\n \"schema stale\": {\n \"--collections\": String,\n \"-c\": \"--collections\",\n \"--output\": String,\n \"-o\": \"--output\",\n \"--out\": \"--output\",\n \"--fix\": Boolean\n }\n};\n\n/**\n * One destination flag, two spellings, everywhere.\n *\n * `--out` is the primary on `rebase build`, an alias on `generate-sdk`, `db\n * backup` and `cloud env pull`, and was refused outright by the three `schema`\n * commands — so the spelling a user learned on one command was an\n * \"unknown or unexpected option\" on the next. Neither name can be retired (both\n * are shipped), so both are accepted, and {@link assertOutputAliasesPaired}\n * makes that the rule rather than a habit.\n */\nexport const OUTPUT_FLAG_ALIASES = [\"--out\", \"--output\"] as const;\n\n/**\n * Every spec that names one of the pair names both.\n *\n * Exported so the CLI's own specs can be held to it too: the drift this fixes\n * ran across two packages, and a check that only reads this file would let the\n * next `--out`-only command through.\n */\nexport function assertOutputAliasesPaired(specs: Record<string, arg.Spec>): string[] {\n const problems: string[] = [];\n for (const [command, spec] of Object.entries(specs)) {\n const present = OUTPUT_FLAG_ALIASES.filter(flag => flag in spec);\n if (present.length === 0 || present.length === OUTPUT_FLAG_ALIASES.length) continue;\n const missing = OUTPUT_FLAG_ALIASES.filter(flag => !(flag in spec));\n problems.push(`\\`${command}\\` takes ${present.join(\", \")} but not ${missing.join(\", \")}`);\n }\n return problems;\n}\n\n/**\n * The `--collections` path the user typed, or `null` if they typed none.\n *\n * Read once, at the entry point, because every command that takes it re-enters\n * the generators with the same line and each of them resolved it again — which\n * is how \"Collections path not found\" came to be printed four times before the\n * real output, and how a path that does not exist got as far as *writing* an\n * empty schema.\n *\n * Permissive, and it has to be: {@link assertKnownFlags} has already judged the\n * line, and `db push` carries flags the `--collections` spec does not name.\n */\nexport function collectionsPathIn(args: string[]): string | null {\n try {\n const parsed = arg(\n { \"--collections\": String, \"-c\": \"--collections\" },\n { argv: args.slice(2), permissive: true }\n );\n\n return parsed[\"--collections\"] ?? null;\n } catch {\n return null;\n }\n}\n\n/**\n * The long flags a `--help` usage line documents.\n *\n * `\"rebase db push [--collections <dir>] [--dry-run] …\"` → `[\"--collections\",\n * \"--dry-run\"]`. Placeholders (`<dir>`) and the alternation inside a positional\n * (`<create|list|switch>`) are not flags and are not returned.\n */\nexport function flagsInUsage(usage: string): string[] {\n return [...new Set(usage.match(/--[a-z][a-z0-9-]*/g) ?? [])];\n}\n\n/**\n * Every flag the help documents is accepted, and every flag accepted is documented.\n *\n * The drift this catches shipped: `rebase db push --help` has printed\n * `[--dry-run]` since the flag was written, `DRIVER_FLAG_SPECS[\"db push\"]` never\n * listed it, and {@link assertKnownFlags} — added later to stop typos being\n * swallowed — turned the documented flag into `unknown or unexpected option`.\n * The only way to see a push's SQL was to trip the destructive gate, which is\n * the exact problem `--dry-run` was written to solve. Worse in a project on an\n * older driver, whose permissive parser *applied* the schema on that line.\n *\n * Two hand-maintained lists in two packages cannot be kept in step by care, so\n * they are held to each other instead: `usages` is keyed the way\n * {@link DRIVER_FLAG_SPECS} is (`\"db push\"`), and comes from the CLI's own help\n * pages. A key in only one of the two is not this function's business — the\n * help covers commands that parse their own lines (`db branch`, `db backup`),\n * and an absent spec entry means \"not checked here\".\n *\n * Aliases (`\"-c\": \"--collections\"`, `\"--out\": \"--output\"`) need no line of their\n * own: they are spellings of a documented flag. Neither do {@link RELAYED_FLAGS},\n * which every driver command accepts because the CLI relays them.\n */\nexport function assertSpecMatchesUsage(\n specs: Record<string, arg.Spec>,\n usages: Record<string, string>\n): string[] {\n const problems: string[] = [];\n for (const [command, spec] of Object.entries(specs)) {\n const usage = usages[command];\n if (usage === undefined) continue;\n\n const documented = flagsInUsage(usage);\n const accepted = Object.entries(spec);\n // An alias resolves to the flag it spells; only the target needs a line.\n const canonical = new Set(\n accepted\n .map(([flag, kind]) => (typeof kind === \"string\" ? kind : flag))\n .filter(flag => flag.startsWith(\"--\"))\n );\n\n for (const flag of documented) {\n if (flag in spec || flag in RELAYED_FLAGS) continue;\n problems.push(`\\`${command}\\` documents ${flag} in its usage line but the spec rejects it`);\n }\n for (const flag of canonical) {\n if (documented.includes(flag) || flag in RELAYED_FLAGS) continue;\n problems.push(`\\`${command}\\` accepts ${flag} but no usage line documents it`);\n }\n }\n return problems;\n}\n\n/**\n * Reject a flag the named command does not take.\n *\n * `args` is the driver's whole line — `[\"db\", \"push\", …]` — so the flags are\n * read from `args.slice(2)`, past the two command words.\n *\n * Throws rather than exiting: `runPluginCommand`'s caller already turns a\n * thrown error into one red line and exit 1, and a thrown error is what the\n * tests can see.\n */\nexport function assertKnownFlags(domain: string, subcommand: string | undefined, args: string[]): void {\n if (!subcommand) return;\n const spec = DRIVER_FLAG_SPECS[`${domain} ${subcommand}`];\n if (!spec) return;\n\n try {\n arg({ ...RELAYED_FLAGS, ...spec }, { argv: args.slice(2) });\n } catch (err) {\n if (err instanceof Error && (err as arg.ArgError).code === \"ARG_UNKNOWN_OPTION\") {\n throw new Error(\n `${err.message} — run \\`rebase ${domain} --help\\` for the options ` +\n `\\`${domain} ${subcommand}\\` takes.`\n );\n }\n throw err;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAmCA,IAAa,gBAAgB;CACzB,kBAAkB;CAClB,YAAY;CACZ,WAAW;CACX,UAAU;CACV,MAAM;AACV;;;;;;;;;;;;AAaA,SAAgB,gBACZ,MACA,MACoC;CACpC,QAAA,GAAA,WAAA,QAAA,CAAW;EAAE,GAAG;EAAe,GAAG;CAAK,GAAG;EAAE,MAAM,KAAK,MAAM,CAAC;EAAG,YAAY;CAAK,CAAC;AACvF;;AAGA,IAAa,oBAA8C;CACvD,WAAW;EACP,iBAAiB;EACjB,MAAM;EACN,aAAa;EACb,uBAAuB;EACvB,SAAS;EACT,MAAM;CACV;CACA,eAAe;EACX,iBAAiB;EACjB,MAAM;CACV;CAcA,cAAc;EACV,cAAc;EACd,wBAAwB;CAC5B;CACA,mBAAmB;EACf,iBAAiB;EACjB,MAAM;EACN,YAAY;EACZ,MAAM;EACN,SAAS;EACT,WAAW;EACX,MAAM;CACV;CACA,qBAAqB;EACjB,YAAY;EACZ,MAAM;EACN,SAAS;EACT,iBAAiB;EACjB,MAAM;EACN,WAAW;EACX,MAAM;EACN,YAAY;CAChB;CACA,gBAAgB;EACZ,iBAAiB;EACjB,MAAM;EACN,YAAY;EACZ,MAAM;EACN,SAAS;EACT,SAAS;CACb;AACJ;;;;;;;;;;;;;AA4CA,SAAgB,kBAAkB,MAA+B;CAC7D,IAAI;EAMA,QAAA,GAAA,WAAA,QAAA,CAJI;GAAE,iBAAiB;GAAQ,MAAM;EAAgB,GACjD;GAAE,MAAM,KAAK,MAAM,CAAC;GAAG,YAAY;EAAK,CAGrC,CAAA,CAAO,oBAAoB;CACtC,QAAQ;EACJ,OAAO;CACX;AACJ;;;;;;;;;;;AA2EA,SAAgB,iBAAiB,QAAgB,YAAgC,MAAsB;CACnG,IAAI,CAAC,YAAY;CACjB,MAAM,OAAO,kBAAkB,GAAG,OAAO,GAAG;CAC5C,IAAI,CAAC,MAAM;CAEX,IAAI;EACA,CAAA,GAAA,WAAA,QAAA,CAAI;GAAE,GAAG;GAAe,GAAG;EAAK,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC,EAAE,CAAC;CAC9D,SAAS,KAAK;EACV,IAAI,eAAe,SAAU,IAAqB,SAAS,sBACvD,MAAM,IAAI,MACN,GAAG,IAAI,QAAQ,kBAAkB,OAAO,8BACnC,OAAO,GAAG,WAAW,UAC9B;EAEJ,MAAM;CACV;AACJ"}
@@ -84,6 +84,45 @@ export declare function resolveLocalBin(binName: string): string | null;
84
84
  export declare function getTableIncludesFromCollections(allCollections: CollectionConfig[]): Promise<string[]>;
85
85
  export declare function getTableIncludes(collectionsPath: string): Promise<string[]>;
86
86
  export declare function loadCollectionsForCli(collectionsPath: string): Promise<CollectionConfig[]>;
87
+ type PgClient = import("pg").Client;
88
+ /**
89
+ * Run one step on this process's connection to the app database.
90
+ *
91
+ * Every step leaves the session as it found it, which is what a connection of
92
+ * its own used to guarantee by being closed. None of them sets anything for
93
+ * the session — `SET LOCAL` inside their own transaction, or `RESET` — and one
94
+ * that fails part-way through a transaction it opened is rolled back here, so
95
+ * the next step does not meet `current transaction is aborted`.
96
+ *
97
+ * @param options.connectionTimeoutMillis applies only when this call is the one
98
+ * that connects.
99
+ */
100
+ export declare function withAppClient<T>(databaseUrl: string, step: (client: PgClient) => Promise<T>, options?: {
101
+ connectionTimeoutMillis?: number;
102
+ }): Promise<T>;
103
+ /** Close every connection {@link withAppClient} opened. Called once, as the command ends. */
104
+ export declare function closeAppClients(): Promise<void>;
105
+ /**
106
+ * Names the empty database Atlas plans against, instead of `<db>_dev_diff`.
107
+ *
108
+ * The derived one is made with `CREATE DATABASE` beside the real database,
109
+ * which assumes a server that holds more than one. PGlite — the managed
110
+ * development database — serves exactly one: the CREATE succeeds and creates
111
+ * nothing, so Atlas connected straight back to the database it was comparing
112
+ * and refused it as "not clean". `rebase db push` on the managed database now
113
+ * starts a second, throwaway PGlite and names it here. Anyone can name their
114
+ * own the same way; a named database is neither created nor dropped.
115
+ */
116
+ export declare const ATLAS_DEV_URL_ENV = "REBASE_ATLAS_DEV_URL";
117
+ /**
118
+ * The database Atlas replays the desired schema into: the one
119
+ * {@link ATLAS_DEV_URL_ENV} names (`named: true` — not ours to create or
120
+ * drop), else `<db>_dev_diff`.
121
+ */
122
+ export declare function atlasDevDatabaseUrl(databaseUrl: string, env: Readonly<Record<string, string | undefined>>): {
123
+ url: string;
124
+ named: boolean;
125
+ };
87
126
  export declare function getDevDatabaseUrl(databaseUrl: string): string;
88
127
  /**
89
128
  * Create the scratch database Atlas plans against, and say so when we cannot.
@@ -322,3 +361,4 @@ export declare function getTableExcludes(databaseUrl: string, collectionsPath: s
322
361
  * `import.meta` and cannot be imported by the jest runner.
323
362
  */
324
363
  export declare function promptConfirm(question: string): Promise<boolean>;
364
+ export {};
@@ -1,34 +1,22 @@
1
- /**
2
- * Terminal output for the `rebase db|schema|doctor` commands.
3
- *
4
- * These commands used to write every line through `logger`, and that is a
5
- * category error with three separate consequences:
6
- *
7
- * - `logger` prefixes each line with its own level, so a box-drawn report
8
- * arrived as `ℹ️ [INFO] ┌─ ✗ Missing Column ───` and the frame no longer
9
- * lined up with anything;
10
- * - `logger` is gated by `LOG_LEVEL`, which ships in the scaffold's own
11
- * `.env.example` — a developer who quietened their dev server with
12
- * `LOG_LEVEL=warn` got a `rebase db push` that printed almost nothing and
13
- * still exited non-zero, indistinguishable from a crash;
14
- * - under `NODE_ENV=production` `logger` emits JSON, so the whole report
15
- * became log records with the chalk escape codes embedded in them.
16
- *
17
- * A CLI's report *is* its return value. It goes to the terminal unconditionally
18
- * and unadorned. `packages/cli` has always written its output this way; this is
19
- * the same three functions for the plugin CLI that `rebase` delegates to.
20
- *
21
- * `logger` still belongs in this package's *runtime* — a request handler has no
22
- * terminal and its lines want levels, timestamps and redaction. The rule is the
23
- * caller, not the severity: anything a developer reads because they typed a
24
- * command goes here, anything a server emits while running goes to `logger`.
25
- *
26
- * Errors and warnings go to stderr so `rebase db push > plan.txt` keeps the
27
- * diagnosis on the terminal where it is readable.
28
- */
29
1
  /** One line of human-facing output on stdout. */
30
2
  export declare const out: (line?: string) => void;
31
3
  /** One line of human-facing warning output on stderr. */
32
4
  export declare const outWarn: (line?: string) => void;
33
5
  /** One line of human-facing error output on stderr. */
34
6
  export declare const outError: (line?: string) => void;
7
+ /**
8
+ * Whether a failure should be printed with its stack — `rebase --debug`, which
9
+ * the launcher passes down as `REBASE_DEBUG`, or `REBASE_DEBUG` set by hand.
10
+ */
11
+ export declare const wantsStack: () => boolean;
12
+ /**
13
+ * An error as a person reads it: its message, and its stack only under
14
+ * `--debug`.
15
+ *
16
+ * The rule `rebase` itself follows (`bin/rebase.js`). These commands run in a
17
+ * child process the launcher's handler never sees, so a syntax error in a
18
+ * collection file reached the terminal as `Doctor failed CollectionConfigError:`
19
+ * followed by twenty frames of loader internals, under a sentence that already
20
+ * said which file and which line.
21
+ */
22
+ export declare const describeFailure: (error: unknown) => string;