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

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 (193) hide show
  1. package/README.md +16 -0
  2. package/dist/{BranchService-BRt78gfa.js → BranchService-UgKa416x.js} +14 -3
  3. package/dist/BranchService-UgKa416x.js.map +1 -0
  4. package/dist/PostgresBackendDriver.d.ts +104 -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 +75 -2
  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-yiKd1GsC.js} +168 -75
  12. package/dist/backup-cli-yiKd1GsC.js.map +1 -0
  13. package/dist/{backup-service-EwcDVG-8.js → backup-service-AZWc2wvI.js} +99 -28
  14. package/dist/backup-service-AZWc2wvI.js.map +1 -0
  15. package/dist/boot-ddl-B9ZmBJ75.js +105 -0
  16. package/dist/boot-ddl-B9ZmBJ75.js.map +1 -0
  17. package/dist/cli-console-DGee3Zl7.js +225 -0
  18. package/dist/cli-console-DGee3Zl7.js.map +1 -0
  19. package/dist/cli-console.d.ts +39 -0
  20. package/dist/cli-errors-oVUuD8k0.js +2180 -0
  21. package/dist/cli-errors-oVUuD8k0.js.map +1 -0
  22. package/dist/cli-errors.d.ts +91 -9
  23. package/dist/{cli-flags-BglvpjHv.js → cli-flags-DnPHXjRH.js} +56 -6
  24. package/dist/cli-flags-DnPHXjRH.js.map +1 -0
  25. package/dist/cli-flags.d.ts +15 -0
  26. package/dist/cli-helpers.d.ts +76 -22
  27. package/dist/cli-output.d.ts +16 -28
  28. package/dist/{arg-Dni7MzLB.js → cli-prompt-egtNKkZl.js} +49 -3
  29. package/dist/cli-prompt-egtNKkZl.js.map +1 -0
  30. package/dist/cli-prompt.d.ts +19 -0
  31. package/dist/cli-push.d.ts +87 -0
  32. package/dist/cli-scratch-database.d.ts +33 -8
  33. package/dist/cli.js +764 -222
  34. package/dist/cli.js.map +1 -1
  35. package/dist/{column-plan-helpers-1-LQD0yI.js → column-plan-helpers-D_V4oaZh.js} +11 -5
  36. package/dist/{column-plan-helpers-1-LQD0yI.js.map → column-plan-helpers-D_V4oaZh.js.map} +1 -1
  37. package/dist/{databasePoolManager-Bj5FbeAs.js → connection-C938nR3E.js} +25 -113
  38. package/dist/connection-C938nR3E.js.map +1 -0
  39. package/dist/connection.d.ts +13 -2
  40. package/dist/databasePoolManager-DZgnBOgj.js +119 -0
  41. package/dist/databasePoolManager-DZgnBOgj.js.map +1 -0
  42. package/dist/{ensure-collection-tables-kkkHk8oo.js → deferred-index-build-BqmEHGaD.js} +485 -347
  43. package/dist/deferred-index-build-BqmEHGaD.js.map +1 -0
  44. package/dist/{doctor-C-sYWbmt.js → doctor-CUAAOFZm.js} +65 -54
  45. package/dist/doctor-CUAAOFZm.js.map +1 -0
  46. package/dist/ensure-collection-policies-Bb51V2OV.js +541 -0
  47. package/dist/ensure-collection-policies-Bb51V2OV.js.map +1 -0
  48. package/dist/ensure-collection-tables-egjGBjhv.js +532 -0
  49. package/dist/ensure-collection-tables-egjGBjhv.js.map +1 -0
  50. package/dist/{ensure-tables-BmI_tRxc.js → ensure-tables-DMPWhJ6R.js} +174 -78
  51. package/dist/ensure-tables-DMPWhJ6R.js.map +1 -0
  52. package/dist/enum-labels-kept-Msq5gMcb.js +56 -0
  53. package/dist/enum-labels-kept-Msq5gMcb.js.map +1 -0
  54. package/dist/generate-drizzle-schema-DrA-YgXv.js +57 -0
  55. package/dist/generate-drizzle-schema-DrA-YgXv.js.map +1 -0
  56. package/dist/{generate-drizzle-schema-logic-sSFDp6LR.js → generate-drizzle-schema-logic-BaT0H7Gq.js} +6 -6
  57. package/dist/generate-drizzle-schema-logic-BaT0H7Gq.js.map +1 -0
  58. package/dist/generate-files-OoRJLEdk.js +199 -0
  59. package/dist/generate-files-OoRJLEdk.js.map +1 -0
  60. package/dist/{generate-postgres-ddl-logic-BJsLaVNX.js → generate-postgres-ddl-logic-B4k9djAV.js} +4 -4
  61. package/dist/{generate-postgres-ddl-logic-BJsLaVNX.js.map → generate-postgres-ddl-logic-B4k9djAV.js.map} +1 -1
  62. package/dist/{generated-schema-staleness-Di9c4ZxN.js → generated-schema-staleness-BWN_PsMi.js} +2 -2
  63. package/dist/{generated-schema-staleness-Di9c4ZxN.js.map → generated-schema-staleness-BWN_PsMi.js.map} +1 -1
  64. package/dist/generated-sql.d.ts +8 -2
  65. package/dist/history/HistoryService.d.ts +64 -5
  66. package/dist/history/ensure-history-table.d.ts +63 -4
  67. package/dist/index.d.ts +1 -0
  68. package/dist/index.es.js +139 -19236
  69. package/dist/index.es.js.map +1 -1
  70. package/dist/{introspect-db-logic-kCETE8TY.js → introspect-db-logic-BCc1Jym2.js} +136 -9
  71. package/dist/introspect-db-logic-BCc1Jym2.js.map +1 -0
  72. package/dist/{introspect-db-queries-C_Q5VgQw.js → introspect-db-queries-BhhJg0b8.js} +48 -1
  73. package/dist/introspect-db-queries-BhhJg0b8.js.map +1 -0
  74. package/dist/open-rebase-CAjWpPjs.js +20895 -0
  75. package/dist/open-rebase-CAjWpPjs.js.map +1 -0
  76. package/dist/open-rebase.d.ts +94 -0
  77. package/dist/pg-error-utils-Bs_Ue4wn.js +395 -0
  78. package/dist/pg-error-utils-Bs_Ue4wn.js.map +1 -0
  79. package/dist/{plan-schema-DU9exq6C.js → plan-schema-CsNgK0qi.js} +152 -42
  80. package/dist/plan-schema-CsNgK0qi.js.map +1 -0
  81. package/dist/{policy-drift-B-J2hhm0.js → policy-drift-BKC50y2p.js} +32 -8
  82. package/dist/policy-drift-BKC50y2p.js.map +1 -0
  83. package/dist/push-index-prebuild-C_qhyoJa.js +45 -0
  84. package/dist/push-index-prebuild-C_qhyoJa.js.map +1 -0
  85. package/dist/push-legacy-renames-5g9h3HOa.js +34 -0
  86. package/dist/push-legacy-renames-5g9h3HOa.js.map +1 -0
  87. package/dist/{render-ddl-Ds2t_d9V.js → render-ddl-CrfEe__B.js} +15 -10
  88. package/dist/render-ddl-CrfEe__B.js.map +1 -0
  89. package/dist/{rls-bootstrap-sql-_KNnjanK.js → rls-bootstrap-sql-Bd-hJk08.js} +125 -1325
  90. package/dist/rls-bootstrap-sql-Bd-hJk08.js.map +1 -0
  91. package/dist/{rls-enforcement-CfXOJJaW.js → rls-enforcement-D7P4-ZYX.js} +130 -18
  92. package/dist/rls-enforcement-D7P4-ZYX.js.map +1 -0
  93. package/dist/{__vite-browser-external-BnuHet1e.js → rolldown-runtime-V7rFbMRe.js} +1 -8
  94. package/dist/schema/atlas-exclude.d.ts +42 -0
  95. package/dist/schema/auth-schema.d.ts +2 -2
  96. package/dist/schema/boot-ddl.d.ts +47 -0
  97. package/dist/schema/carved-out-migration.d.ts +5 -4
  98. package/dist/schema/collection-index.d.ts +45 -0
  99. package/dist/schema/column-plan-helpers.d.ts +5 -0
  100. package/dist/schema/deferred-index-build.d.ts +131 -0
  101. package/dist/schema/destructive-sql.d.ts +30 -1
  102. package/dist/schema/doctor-cli.js +110 -6
  103. package/dist/schema/doctor-cli.js.map +1 -1
  104. package/dist/schema/doctor-search-scale.d.ts +45 -0
  105. package/dist/schema/doctor.d.ts +20 -1
  106. package/dist/schema/ensure-collection-policies.d.ts +73 -35
  107. package/dist/schema/ensure-collection-tables.d.ts +56 -3
  108. package/dist/schema/enum-labels-kept.d.ts +26 -0
  109. package/dist/schema/generate-drizzle-schema.js +1 -1
  110. package/dist/schema/generate-files.d.ts +44 -0
  111. package/dist/schema/generate-next-step.d.ts +0 -13
  112. package/dist/schema/generate-postgres-ddl.js +10 -81
  113. package/dist/schema/generate-postgres-ddl.js.map +1 -1
  114. package/dist/schema/generated-schema-diff.d.ts +7 -0
  115. package/dist/schema/introspect-db-logic.d.ts +32 -0
  116. package/dist/schema/introspect-db-queries.d.ts +20 -1
  117. package/dist/schema/introspect-db-structure.d.ts +10 -0
  118. package/dist/schema/introspect-db.js +16 -20
  119. package/dist/schema/introspect-db.js.map +1 -1
  120. package/dist/schema/migrate-baseline.d.ts +33 -0
  121. package/dist/schema/migration-post-process.d.ts +34 -0
  122. package/dist/schema/plan/diff-plan.d.ts +126 -0
  123. package/dist/schema/plan/render-ddl.d.ts +2 -1
  124. package/dist/schema/plan/types.d.ts +8 -0
  125. package/dist/schema/push-index-prebuild.d.ts +25 -0
  126. package/dist/schema/push-legacy-renames.d.ts +20 -0
  127. package/dist/schema/rls-bootstrap-sql.d.ts +15 -0
  128. package/dist/schema/undeclarable-constraints.d.ts +59 -0
  129. package/dist/security/rls-enforcement.d.ts +32 -4
  130. package/dist/services/BranchService.d.ts +7 -0
  131. package/dist/services/FetchService.d.ts +58 -8
  132. package/dist/services/PersistService.d.ts +15 -0
  133. package/dist/services/RelationService.d.ts +88 -3
  134. package/dist/services/cdc/CdcListener.d.ts +17 -6
  135. package/dist/services/cdc/trigger-cdc.d.ts +151 -27
  136. package/dist/services/channel-bus/index.d.ts +1 -1
  137. package/dist/services/channel-history.d.ts +15 -4
  138. package/dist/services/collection-helpers.d.ts +15 -0
  139. package/dist/services/field-viewer.d.ts +15 -1
  140. package/dist/services/pg-notify-listener.d.ts +7 -0
  141. package/dist/services/realtimeService.d.ts +314 -14
  142. package/dist/services/row-pipeline.d.ts +16 -9
  143. package/dist/services/socket-liveness.d.ts +42 -2
  144. package/dist/services/sql-script.d.ts +207 -3
  145. package/dist/services/vector-iterative-scan.d.ts +40 -0
  146. package/dist/services/write-depth.d.ts +6 -0
  147. package/dist/services/write-transaction-scope.d.ts +9 -0
  148. package/dist/socket-opt-ins.d.ts +64 -0
  149. package/dist/source-DB6xJv3p.js +873 -0
  150. package/dist/source-DB6xJv3p.js.map +1 -0
  151. package/dist/tsx-loader.d.ts +14 -0
  152. package/dist/utils/connection-string.d.ts +31 -0
  153. package/dist/utils/drizzle-conditions.d.ts +21 -1
  154. package/dist/utils/error-cause.d.ts +7 -0
  155. package/dist/utils/pg-error-utils.d.ts +2 -4
  156. package/dist/utils/result-rows.d.ts +10 -0
  157. package/dist/{vector-index-CMJKb3C9.js → vector-index-BFhyEaiz.js} +2 -2
  158. package/dist/{vector-index-CMJKb3C9.js.map → vector-index-BFhyEaiz.js.map} +1 -1
  159. package/dist/websocket-DUiiwQd2.js +1632 -0
  160. package/dist/websocket-DUiiwQd2.js.map +1 -0
  161. package/dist/websocket.d.ts +13 -0
  162. package/package.json +7 -7
  163. package/dist/BranchService-BRt78gfa.js.map +0 -1
  164. package/dist/__vite-browser-external-BnuHet1e.js.map +0 -1
  165. package/dist/arg-Dni7MzLB.js.map +0 -1
  166. package/dist/backup-cli-DW5p9_zv.js.map +0 -1
  167. package/dist/backup-service-EwcDVG-8.js.map +0 -1
  168. package/dist/cli-errors-DsA-K9uP.js +0 -893
  169. package/dist/cli-errors-DsA-K9uP.js.map +0 -1
  170. package/dist/cli-flags-BglvpjHv.js.map +0 -1
  171. package/dist/cli-output-CNdMql-L.js +0 -47
  172. package/dist/cli-output-CNdMql-L.js.map +0 -1
  173. package/dist/collection-index-DdnsxiJ_.js +0 -286
  174. package/dist/collection-index-DdnsxiJ_.js.map +0 -1
  175. package/dist/databasePoolManager-Bj5FbeAs.js.map +0 -1
  176. package/dist/doctor-C-sYWbmt.js.map +0 -1
  177. package/dist/ensure-collection-policies-B1ureSIV.js +0 -160
  178. package/dist/ensure-collection-policies-B1ureSIV.js.map +0 -1
  179. package/dist/ensure-collection-tables-kkkHk8oo.js.map +0 -1
  180. package/dist/ensure-tables-BmI_tRxc.js.map +0 -1
  181. package/dist/generate-drizzle-schema-93M0lUxK.js +0 -123
  182. package/dist/generate-drizzle-schema-93M0lUxK.js.map +0 -1
  183. package/dist/generate-drizzle-schema-logic-sSFDp6LR.js.map +0 -1
  184. package/dist/introspect-db-logic-kCETE8TY.js.map +0 -1
  185. package/dist/introspect-db-queries-C_Q5VgQw.js.map +0 -1
  186. package/dist/plan-schema-DU9exq6C.js.map +0 -1
  187. package/dist/policy-drift-B-J2hhm0.js.map +0 -1
  188. package/dist/render-ddl-Ds2t_d9V.js.map +0 -1
  189. package/dist/rls-bootstrap-sql-_KNnjanK.js.map +0 -1
  190. package/dist/rls-enforcement-CfXOJJaW.js.map +0 -1
  191. package/dist/source-Br7L7GOI.js +0 -410
  192. package/dist/source-Br7L7GOI.js.map +0 -1
  193. package/dist/utils/sql-redaction.d.ts +0 -21
@@ -0,0 +1,131 @@
1
+ /**
2
+ * Building the indexes a live table is missing — or holds INVALID — without
3
+ * holding anything up: the derived foreign-key indexes, and the declared,
4
+ * search and vector ones the boot-time ensure finds missing on a table that
5
+ * already exists.
6
+ *
7
+ * ## Why not a boot action like an index on a new table
8
+ *
9
+ * A table that already exists may hold millions of rows, and an index over
10
+ * them takes minutes to build. Boot-ensure's actions run before the server
11
+ * listens, so an index planned there is minutes of a deployment not serving —
12
+ * on the first boot after an upgrade that adds one to every `belongsTo` in the
13
+ * project, that is every large table at once. So these are planned apart
14
+ * (`DeferredIndexBuild`) and built here, after the boot, one at a time. A
15
+ * UNIQUE one is the exception: `ensureCollectionTables` builds it here too, but
16
+ * before it returns.
17
+ *
18
+ * ## The build
19
+ *
20
+ * `CREATE INDEX CONCURRENTLY`, outside any transaction (Postgres refuses it
21
+ * inside one), on a connection of its own with `statement_timeout` lifted: the
22
+ * pool's 30 s would cancel any build worth deferring. `CONCURRENTLY` takes
23
+ * `SHARE UPDATE EXCLUSIVE`, which conflicts with no read and no write — only
24
+ * with other DDL, `VACUUM` and `ANALYZE` — so live traffic never queues behind
25
+ * it, however long it runs.
26
+ *
27
+ * The boot's lock budget still applies, to the one wait that matters: getting
28
+ * the table at all. A table something else holds — another instance's build,
29
+ * an `ALTER TABLE`, a `VACUUM FULL` — is probed for `BOOT_LOCK_TIMEOUT_MS` at a
30
+ * time within the boot's retry budget, and left for the next boot when it
31
+ * stays held. Once the build has the table, it waits for the transactions
32
+ * already running against it with no deadline: a timeout there would only turn
33
+ * a slow build into a failed one.
34
+ *
35
+ * ## The husk a failed build leaves
36
+ *
37
+ * A cancelled `CONCURRENTLY` — a timeout, a killed process, a deploy that
38
+ * replaced the pod mid-build, two instances racing on one name — leaves the
39
+ * index behind marked `indisvalid = false`: maintained on every write, used by
40
+ * no query. And `IF NOT EXISTS` is a *name* check, so every later attempt
41
+ * skips it and reports success. This reads `pg_index` instead: an invalid index
42
+ * under our name is dropped (`DROP INDEX CONCURRENTLY`) and built again, once.
43
+ * One that another backend is still building (`pg_stat_progress_create_index`)
44
+ * is left to it.
45
+ *
46
+ * Nothing here is fatal. A failure is logged with the index named and the
47
+ * reason, and the next boot plans the index again.
48
+ */
49
+ import type { Pool } from "pg";
50
+ import { type DeferredIndexBuild } from "./plan/diff-plan.js";
51
+ import { type BootLockOptions } from "./boot-ddl.js";
52
+ /**
53
+ * A connection the builder may hold for as long as an index takes.
54
+ *
55
+ * `dedicated` says whether consecutive queries reach the same backend. Only
56
+ * then can a session setting be relied on — and be reset afterwards: on a
57
+ * pool, `SET statement_timeout = 0` would land on whichever connection served
58
+ * it and stay there for the next request to inherit.
59
+ */
60
+ export interface IndexBuildSession {
61
+ query<T = unknown>(sql: string): Promise<{
62
+ rows: T[];
63
+ }>;
64
+ readonly dedicated: boolean;
65
+ /** Hand the connection back. Called once, after the last index. */
66
+ release(): void | Promise<void>;
67
+ }
68
+ export type OpenIndexBuildSession = () => Promise<IndexBuildSession>;
69
+ /** A `pg.Pool` — what `connect()` checks a client out of — and not a `pg.Client`, whose `connect()` opens a socket. */
70
+ export declare const isPgPool: (value: unknown) => value is Pick<Pool, "connect" | "totalCount">;
71
+ /**
72
+ * Sessions checked out of a `pg.Pool`, one connection each.
73
+ *
74
+ * Its own connection rather than the pool's next free one, because a build
75
+ * has to outlive both of a pool's timeouts: the server-side
76
+ * `statement_timeout` (30 s on this framework's pools), which the builder
77
+ * lifts for the session — something it may only do on a connection nobody
78
+ * else is handed until it is reset — and the client-side read timeout, which
79
+ * is lifted here per query.
80
+ */
81
+ export declare const poolIndexBuildSession: (pool: Pick<Pool, "connect">) => OpenIndexBuildSession;
82
+ export interface DeferredIndexOptions extends BootLockOptions {
83
+ /** Progress, one line per index. Failures are also `logger.warn`ed. */
84
+ log?: (message: string) => void;
85
+ /** Checked between indexes; once true, nothing further is started. */
86
+ cancelled?: () => boolean;
87
+ }
88
+ export interface DeferredIndexOutcome {
89
+ /** `schema.index` this run built. */
90
+ built: string[];
91
+ /** Left alone, and why — already valid, or another instance is on it. */
92
+ skipped: {
93
+ index: string;
94
+ reason: string;
95
+ }[];
96
+ /** Not built. Logged; the next boot plans them again. */
97
+ failed: {
98
+ index: string;
99
+ error: string;
100
+ }[];
101
+ }
102
+ /** Why an index is skipped while a build of another index holds its table. */
103
+ export declare const TABLE_BUSY_REASON = "another index on the table is being built";
104
+ /**
105
+ * Build every deferred index, one at a time, on one connection.
106
+ *
107
+ * Never throws for an index: each failure is recorded and the next one is
108
+ * attempted. Throws only when no connection can be had at all.
109
+ */
110
+ export declare function buildDeferredIndexes(openSession: OpenIndexBuildSession, indexes: readonly DeferredIndexBuild[], options?: DeferredIndexOptions): Promise<DeferredIndexOutcome>;
111
+ /**
112
+ * Start {@link buildDeferredIndexes} later, in the background.
113
+ *
114
+ * `start()` begins it after `delayMs`; the fallback timer begins it after
115
+ * `fallbackMs` if nothing calls `start()` — so a process whose boot never
116
+ * reaches the step that would have started it still builds. Both timers are
117
+ * unref'd: a pending build never keeps a process alive, and a process that
118
+ * exits mid-build leaves a husk the next boot rebuilds.
119
+ */
120
+ export interface ScheduledIndexBuild {
121
+ /** Begin after the schedule's `delayMs`, instead of waiting for the fallback timer. */
122
+ start(): void;
123
+ /** Start nothing further. An index already building runs to completion. */
124
+ cancel(): void;
125
+ /** Settles when the run ends — `undefined` if it never ran or could not open a connection. */
126
+ done: Promise<DeferredIndexOutcome | undefined>;
127
+ }
128
+ export declare function scheduleDeferredIndexBuild(openSession: OpenIndexBuildSession, indexes: readonly DeferredIndexBuild[], options?: DeferredIndexOptions & {
129
+ delayMs?: number;
130
+ fallbackMs?: number;
131
+ }): ScheduledIndexBuild;
@@ -20,9 +20,16 @@
20
20
  * "<table>_pkey", ADD PRIMARY KEY ("id", "locale")` keeps every value and
21
21
  * gives every row a new address. It is gated like a drop.
22
22
  *
23
+ * So is the drop of a CHECK or EXCLUDE constraint. It keeps every row and
24
+ * stops refusing the ones the rule was there to refuse. `db push` keeps Atlas
25
+ * away from those constraints (`schema/undeclarable-constraints.ts`), so a plan
26
+ * that still drops one is a pattern that missed, and the gate is the second
27
+ * line. Which constraints those are is read from the catalogue by the caller.
28
+ *
23
29
  * Everything in this file is side-effect free so it can be unit-tested
24
30
  * without Atlas or a database.
25
31
  */
32
+ import type { ExistingConstraint } from "./undeclarable-constraints.js";
26
33
  /**
27
34
  * Split a SQL script into individual statements, dropping blank lines and
28
35
  * `--` comment lines. Deliberately simple: Atlas emits one plain statement
@@ -36,6 +43,13 @@ export declare function splitSqlStatements(sql: string): string[];
36
43
  * it reads; `test/fixtures/atlas-dry-run/` holds real captures of it.
37
44
  */
38
45
  export declare function extractPlanStatements(plan: string): string[];
46
+ /**
47
+ * Whether Atlas answered the dry-run with "Schema is synced" and nothing else.
48
+ *
49
+ * Positive evidence only: an empty or unreadable report also yields no
50
+ * statements, and is not the same answer.
51
+ */
52
+ export declare function isSyncedPlan(plan: string): boolean;
39
53
  export interface DestructiveStatement {
40
54
  /** The offending statement (trimmed, without the trailing `;`). */
41
55
  statement: string;
@@ -70,8 +84,12 @@ export declare const COLUMN_TYPES_SQL = "\n SELECT n.nspname AS \"schema\",\n
70
84
  * `columnTypes` is what the database holds now. A type change to a column it
71
85
  * does not list is flagged: not knowing what a column is means not knowing
72
86
  * whether the change keeps its values.
87
+ *
88
+ * `constraints` are the CHECK and EXCLUDE constraints the database holds now.
89
+ * A `DROP CONSTRAINT` of one of them is flagged; of any other constraint (a
90
+ * foreign key, a `UNIQUE`) it is not, as before.
73
91
  */
74
- export declare function detectDestructiveStatements(planSql: string, columnTypes?: readonly ExistingColumnType[]): DestructiveStatement[];
92
+ export declare function detectDestructiveStatements(planSql: string, columnTypes?: readonly ExistingColumnType[], constraints?: readonly ExistingConstraint[]): DestructiveStatement[];
75
93
  /**
76
94
  * The statement from its `ALTER TABLE` on, wherever on a line that starts.
77
95
  *
@@ -102,6 +120,17 @@ export declare function fromAlterTable(statement: string): string;
102
120
  */
103
121
  export declare function isLosslessTypeChange(from: string, to: string): boolean;
104
122
  export type PushDecision = "apply" | "confirm" | "refuse";
123
+ /**
124
+ * The flag that opted a push into destructive changes, as it was given —
125
+ * `--allow-destructive`, or its alias `--yes` / `-y` — or `undefined` for
126
+ * none. The line that says why a destructive push went ahead names the one
127
+ * the user typed: "because --allow-destructive was passed" to someone who
128
+ * typed `--yes` reads like a flag they never set.
129
+ */
130
+ export declare function destructiveOptInFlag(args: {
131
+ "--allow-destructive"?: boolean;
132
+ "--yes"?: boolean;
133
+ }): "--allow-destructive" | "--yes" | undefined;
105
134
  /**
106
135
  * Decide how `db push` should proceed given the plan's destructiveness and
107
136
  * the invocation context.
@@ -1,11 +1,14 @@
1
1
  #!/usr/bin/env node
2
2
  import { createRequire as __createRequire } from "module";
3
3
  __createRequire(import.meta.url);
4
- import { i as diagnoseDbError } from "../cli-errors-DsA-K9uP.js";
5
- import { c as warnOnAnonymousGrants, s as validatePolicyPgRoles } from "../rls-enforcement-CfXOJJaW.js";
6
- import { t as chalk } from "../source-Br7L7GOI.js";
7
- import { n as loadCollections, r as runDoctor } from "../doctor-C-sYWbmt.js";
8
- import { n as formatPolicyDrift, r as hasDrift, t as checkPolicyDrift } from "../policy-drift-B-J2hhm0.js";
4
+ import { a as buildSearchColumnSpec } from "../search-column-BM-GV6vH.js";
5
+ import { i as diagnoseDbError } from "../cli-errors-oVUuD8k0.js";
6
+ import { l as validatePolicyPgRoles, u as warnOnAnonymousGrants } from "../rls-enforcement-D7P4-ZYX.js";
7
+ import { n as describeFailure, t as chalk } from "../source-DB6xJv3p.js";
8
+ import { n as loadCollections, r as runDoctor } from "../doctor-CUAAOFZm.js";
9
+ import { n as formatPolicyDrift, r as hasDrift, t as checkPolicyDrift } from "../policy-drift-BKC50y2p.js";
10
+ import { isPostgresCollectionConfig } from "@rebasepro/types";
11
+ import { getTableName, relationalCollections } from "@rebasepro/common";
9
12
  import path from "path";
10
13
  //#region src/schema/doctor-policy-checks.ts
11
14
  /**
@@ -80,6 +83,106 @@ async function runPolicyChecks(collectionsPath, databaseUrl) {
80
83
  return problems ? "problems" : "ok";
81
84
  }
82
85
  //#endregion
86
+ //#region src/schema/doctor-search-scale.ts
87
+ /**
88
+ * `rebase doctor`: collections searched by a full scan of a large table.
89
+ *
90
+ * Without a `search` block, a search — the admin's search box, `?searchString=`,
91
+ * `.search()` — is `ILIKE '%term%'` over every text column. A leading `%`
92
+ * defeats a B-tree, so each one reads the whole table: on 100k posts a search
93
+ * took 326 ms, half for the page and half for its count. Nothing reports it;
94
+ * the box just gets slower as the table grows. This names the collections
95
+ * where that has started to matter, and points at the block that fixes it.
96
+ *
97
+ * A hint, not a failure: it never changes doctor's exit code, and the database
98
+ * is not changed — Rebase does not add a search index on its own.
99
+ *
100
+ * Lives beside `doctor-cli.ts` rather than inside it because that file runs
101
+ * `main()` on import.
102
+ */
103
+ /** Above this many rows an unindexed search is worth a word. */
104
+ var UNINDEXED_SEARCH_ROWS = 1e4;
105
+ /**
106
+ * The collections a search reaches without an index: no `search` block (or
107
+ * one that does not build), and at least one property the default search
108
+ * reads — a `string`, not an enum, not a uuid key (the rule in
109
+ * `DrizzleConditionBuilder.buildSearchConditions`).
110
+ */
111
+ function ilikeSearchedCollections(collections) {
112
+ const found = [];
113
+ for (const collection of relationalCollections(collections)) {
114
+ if (!isPostgresCollectionConfig(collection)) continue;
115
+ let indexed;
116
+ try {
117
+ indexed = buildSearchColumnSpec(collection) !== void 0;
118
+ } catch {
119
+ indexed = false;
120
+ }
121
+ if (indexed) continue;
122
+ const fields = Object.entries(collection.properties ?? {}).filter(([, property]) => property.type === "string" && !property.enum && property.isId !== "uuid").map(([key]) => key);
123
+ const table = getTableName(collection);
124
+ if (fields.length === 0 || !table) continue;
125
+ found.push({
126
+ slug: collection.slug,
127
+ schema: collection.schema ?? "public",
128
+ table,
129
+ fields
130
+ });
131
+ }
132
+ return found;
133
+ }
134
+ /** Those of `candidates` whose table holds more than `threshold` rows, by estimate. */
135
+ function unindexedSearchFindings(candidates, rowEstimates, threshold = UNINDEXED_SEARCH_ROWS) {
136
+ return candidates.map((candidate) => ({
137
+ ...candidate,
138
+ rows: rowEstimates.get(`${candidate.schema}.${candidate.table}`) ?? 0
139
+ })).filter((candidate) => candidate.rows > threshold);
140
+ }
141
+ /**
142
+ * Row counts by `schema.table`, estimated — `pg_class.reltuples`, or the
143
+ * statistics collector's live-tuple count when the table was never analysed.
144
+ * Nothing is scanned: counting the tables this is about would be the cost it
145
+ * warns of.
146
+ */
147
+ async function estimateRowCounts(query, schemas) {
148
+ const { rows } = await query(`SELECT n.nspname AS schema, c.relname AS table,
149
+ GREATEST(c.reltuples, COALESCE(s.n_live_tup, 0), 0)::bigint AS rows
150
+ FROM pg_class c
151
+ JOIN pg_namespace n ON n.oid = c.relnamespace
152
+ LEFT JOIN pg_stat_user_tables s ON s.relid = c.oid
153
+ WHERE c.relkind IN ('r', 'p') AND n.nspname = ANY($1)`, [schemas]);
154
+ return new Map(rows.map((row) => [`${row.schema}.${row.table}`, Number(row.rows)]));
155
+ }
156
+ /** The report, one warning per collection. */
157
+ function formatUnindexedSearches(findings) {
158
+ if (findings.length === 0) return [];
159
+ const lines = ["", chalk.yellow(` ⚠ ${findings.length} collection(s) searched by reading the whole table:`)];
160
+ for (const finding of findings) lines.push(chalk.yellow(` • ${finding.slug} — about ${finding.rows.toLocaleString("en-US")} rows, no \`search\` block: a search is ILIKE '%term%' over ${finding.fields.join(", ")}`));
161
+ lines.push(chalk.gray(" Each search reads every row, and its count reads them again. A `search` block indexes it\n (full-text, or `mode: \"hybrid\"` for substrings too): https://rebase.pro/docs/backend/search"));
162
+ return lines;
163
+ }
164
+ /**
165
+ * Print the warning for the project at `collectionsPath`, against
166
+ * `databaseUrl`. Never throws and never fails the run: it is a hint, and a
167
+ * check that could not run says so in one grey line.
168
+ */
169
+ async function runSearchScaleCheck(collectionsPath, databaseUrl) {
170
+ if (!databaseUrl) return;
171
+ const { Client } = await import("pg");
172
+ const client = new Client({ connectionString: databaseUrl });
173
+ try {
174
+ const candidates = ilikeSearchedCollections(await loadCollections(path.resolve(process.cwd(), collectionsPath)));
175
+ if (candidates.length === 0) return;
176
+ await client.connect();
177
+ const estimates = await estimateRowCounts((text, values) => client.query(text, values), [...new Set(candidates.map((candidate) => candidate.schema))]);
178
+ for (const line of formatUnindexedSearches(unindexedSearchFindings(candidates, estimates))) console.log(line);
179
+ } catch (err) {
180
+ console.log(chalk.gray(` ⏭ Search on large tables: not checked (${err instanceof Error ? err.message : String(err)})`));
181
+ } finally {
182
+ await client.end().catch(() => void 0);
183
+ }
184
+ }
185
+ //#endregion
83
186
  //#region src/schema/doctor-cli.ts
84
187
  /**
85
188
  * CLI entry point for `rebase doctor`.
@@ -111,6 +214,7 @@ async function main() {
111
214
  databaseUrl: databaseUrl ?? void 0
112
215
  });
113
216
  const policyStatus = await runPolicyChecks(collectionsPath, databaseUrl);
217
+ await runSearchScaleCheck(collectionsPath, databaseUrl);
114
218
  const blocked = [
115
219
  report.collectionsToSchema,
116
220
  report.schemaToDatabase,
@@ -121,7 +225,7 @@ async function main() {
121
225
  main().catch((err) => {
122
226
  const banner = diagnoseDbError(err, process.env.DATABASE_URL || process.env.ADMIN_CONNECTION_STRING);
123
227
  if (banner) console.error(banner);
124
- else console.error(chalk.red(" ✗ Doctor failed"), err instanceof Error ? err.stack ?? err.message : String(err));
228
+ else console.error(chalk.red(` ✗ Doctor failed: ${describeFailure(err)}`));
125
229
  process.exit(1);
126
230
  });
127
231
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"doctor-cli.js","names":[],"sources":["../../src/schema/doctor-policy-checks.ts","../../src/schema/doctor-cli.ts"],"sourcesContent":["/**\n * The RLS half of `rebase doctor`, and the exit code `--policies` gates CI on.\n *\n * Lives beside `doctor-cli.ts` rather than inside it because that file runs\n * `main()` on import: a gate whose failure modes cannot be unit-tested is how\n * this one shipped reporting success for work it never did.\n */\nimport path from \"path\";\nimport chalk from \"chalk\";\nimport { CollectionConfig } from \"@rebasepro/types\";\nimport { loadCollections } from \"./doctor\";\nimport { checkPolicyDrift, formatPolicyDrift, hasDrift } from \"../security/policy-drift\";\nimport { validatePolicyPgRoles, warnOnAnonymousGrants } from \"../security/rls-enforcement\";\n\n/**\n * What the RLS checks concluded.\n *\n * `unchecked` exists because \"we could not look\" and \"we looked and it is fine\"\n * used to be reported identically: a collections path that did not resolve made\n * the loader return `[]` (it warns, it does not throw), `checkPolicyDrift`\n * early-returned an empty diff, and the gate printed\n * `✓ RLS policies match your collections` having compared zero policies against\n * zero collections. Any exception at all — a collection file that throws on\n * import, a `pg_policies` read the CI role is not granted, a connection reset —\n * did the same thing through a `warn`, and exited 0.\n */\nexport type PolicyCheckStatus = \"ok\" | \"problems\" | \"unchecked\";\n\n/**\n * The exit code for `rebase doctor --policies`.\n *\n * A gate that could not run has not passed. Only a completed, clean check\n * exits 0 — anything else, including \"we never opened a connection\", is a\n * failure, or the flag certifies a database nobody looked at.\n */\nexport function exitCodeForPolicyGate(status: PolicyCheckStatus): 0 | 1 {\n return status === \"ok\" ? 0 : 1;\n}\n\n/**\n * Policies actually deployed vs the ones the collections describe, plus policy\n * roles this server could never satisfy.\n *\n * Never reports `ok` for work it did not do — see {@link PolicyCheckStatus}.\n */\nexport async function runPolicyChecks(collectionsPath: string, databaseUrl?: string): Promise<PolicyCheckStatus> {\n if (!databaseUrl) {\n console.error(chalk.yellow(\" ⚠ No DATABASE_URL — RLS policies were NOT checked\"));\n return \"unchecked\";\n }\n\n const resolvedPath = path.resolve(process.cwd(), collectionsPath);\n let problems = false;\n const { Pool } = await import(\"pg\");\n const pool = new Pool({ connectionString: databaseUrl });\n try {\n const collections: CollectionConfig[] = await loadCollections(resolvedPath);\n\n // Zero collections is not \"no drift\", it is nothing to compare. The\n // loader returns `[]` for a path that does not exist, so the commonest\n // way to get here is a `--collections` path resolved against the wrong\n // directory — which used to render as a green tick.\n if (collections.length === 0) {\n console.error(chalk.red(` ✗ No collections found in ${resolvedPath}`));\n console.error(chalk.gray(\" RLS policies were NOT checked. Pass --collections=<dir> if your collections live elsewhere.\"));\n return \"unchecked\";\n }\n\n const runSql = async (text: string) => (await pool.query(text)).rows as Record<string, unknown>[];\n\n // Prove the connection before anything is ticked.\n //\n // `validatePolicyPgRoles` returns without a query when no collection\n // names a `pgRoles` — which is most projects — so it \"passed\" against a\n // database that was never reached, and the tick printed above an\n // ECONNREFUSED that only surfaced two checks later. Ten lines up, this\n // file says that reporting success for work it never did is the reason\n // it exists. This is the same shape, one function along.\n try {\n await runSql(\"SELECT 1\");\n } catch (err) {\n console.error(chalk.gray(\" ⏭ Policy roles: not checked\"));\n console.error(chalk.red(\" ✗ Could not check RLS policies:\"), err instanceof Error ? err.message : String(err));\n return \"unchecked\";\n }\n\n // A policy naming a role the server never runs as filters every row, so\n // the collection reads as empty. Report it without booting a server.\n try {\n await validatePolicyPgRoles(runSql, collections as never);\n console.log(chalk.green(\" ✓ Policy roles are usable by this server\"));\n } catch (err) {\n problems = true;\n console.log(\"\");\n console.error(chalk.red(err instanceof Error ? err.message : String(err)));\n }\n\n // A rule that reads as a lockdown but is true for every caller compiles\n // to a grant. Report it without booting a server.\n warnOnAnonymousGrants(collections as never);\n\n const drift = await checkPolicyDrift(pool as never, collections);\n console.log(\"\");\n if (hasDrift(drift)) {\n problems = true;\n console.log(chalk.yellow(\" RLS policies: database does not match your collections\"));\n console.log(formatPolicyDrift(drift));\n } else {\n console.log(chalk.green(` ✓ RLS policies match your collections (${collections.length} collection(s) checked)`));\n }\n } catch (err) {\n // Fail closed. This catch covers every query and every collection import\n // above, so returning the pre-catch verdict made the documented CI gate\n // green on exactly the runs it exists to catch.\n console.error(chalk.red(\" ✗ Could not check RLS policies:\"), err instanceof Error ? err.message : String(err));\n return \"unchecked\";\n } finally {\n await pool.end();\n }\n return problems ? \"problems\" : \"ok\";\n}\n","#!/usr/bin/env node\n/**\n * CLI entry point for `rebase doctor`.\n * Invoked via tsx by the server-postgres CLI plugin.\n */\nimport path from \"path\";\nimport chalk from \"chalk\";\nimport { runDoctor } from \"./doctor\";\nimport { exitCodeForPolicyGate, runPolicyChecks } from \"./doctor-policy-checks\";\nimport { diagnoseDbError } from \"../cli-errors\";\n\nasync function main() {\n const collectionsArg = process.argv.find((a) => a.startsWith(\"--collections=\"));\n const schemaArg = process.argv.find((a) => a.startsWith(\"--schema=\"));\n const sdkArg = process.argv.find((a) => a.startsWith(\"--sdk=\"));\n // --policies runs only the RLS checks: no schema diff, no SDK types. Useful\n // as a CI gate against a deployed database, where the collection files and\n // generated schema are not what you are asking about.\n const policiesOnly = process.argv.includes(\"--policies\");\n\n const collectionsPath = collectionsArg?.split(\"=\")[1] ?? path.join(\"..\", \"config\", \"collections\");\n const schemaPath = schemaArg?.split(\"=\")[1] ?? path.join(\"src\", \"schema.generated.ts\");\n const sdkPath = sdkArg?.split(\"=\")[1] ?? path.join(\"..\", \"generated\", \"sdk\", \"database.types.ts\");\n\n // Load .env\n try {\n const dotenv = await import(\"dotenv\");\n const envPath = process.env.DOTENV_CONFIG_PATH;\n if (envPath) {\n dotenv.config({ path: envPath, quiet: true });\n } else {\n dotenv.config({ quiet: true });\n }\n } catch {\n // dotenv may not be installed\n }\n\n const databaseUrl = process.env.DATABASE_URL || process.env.ADMIN_CONNECTION_STRING;\n\n if (policiesOnly) {\n // Non-zero so this can gate CI — the whole point of the flag, and a\n // check that could not run has not passed. See exitCodeForPolicyGate.\n process.exit(exitCodeForPolicyGate(await runPolicyChecks(collectionsPath, databaseUrl)));\n }\n\n const report = await runDoctor({\n collectionsPath: path.resolve(process.cwd(), collectionsPath),\n schemaPath: path.resolve(process.cwd(), schemaPath),\n sdkPath: path.resolve(process.cwd(), sdkPath),\n databaseUrl: databaseUrl ?? undefined\n });\n\n const policyStatus = await runPolicyChecks(collectionsPath, databaseUrl);\n\n // Exit non-zero if there are errors. A policy run that could not happen is\n // reported loudly above but does not fail the interactive command — the\n // same treatment the *chosen* skip of the database phase gets. `--policies`\n // is the gate, and that one fails closed.\n //\n // A phase that was skipped by a fault is different, and `blocked` is what\n // separates the two: \"no DATABASE_URL\" is a local situation somebody may be\n // content with, while \"the DATABASE_URL you set refuses connections\" would\n // otherwise let `rebase doctor` go green in CI against a database it never\n // reached.\n const blocked = [report.collectionsToSchema, report.schemaToDatabase, report.collectionsToSdk]\n .some(phase => phase.blocked);\n if (report.summary.errors > 0 || policyStatus === \"problems\" || blocked) {\n process.exit(1);\n }\n}\n\nmain().catch((err) => {\n // A known database failure has a diagnosis; a stack trace through pg and\n // drizzle internals is not it. This is the last resort — `runDoctor`\n // already catches the connection failure it can name — so it exists for\n // everything reached after the report, the policy checks in particular.\n const banner = diagnoseDbError(err, process.env.DATABASE_URL || process.env.ADMIN_CONNECTION_STRING);\n if (banner) {\n console.error(banner);\n } else {\n console.error(chalk.red(\" ✗ Doctor failed\"), err instanceof Error ? (err.stack ?? err.message) : String(err));\n }\n process.exit(1);\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAmCA,SAAgB,sBAAsB,QAAkC;CACpE,OAAO,WAAW,OAAO,IAAI;AACjC;;;;;;;AAQA,eAAsB,gBAAgB,iBAAyB,aAAkD;CAC7G,IAAI,CAAC,aAAa;EACd,QAAQ,MAAM,MAAM,OAAO,qDAAqD,CAAC;EACjF,OAAO;CACX;CAEA,MAAM,eAAe,KAAK,QAAQ,QAAQ,IAAI,GAAG,eAAe;CAChE,IAAI,WAAW;CACf,MAAM,EAAE,SAAS,MAAM,OAAO;CAC9B,MAAM,OAAO,IAAI,KAA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O;CAChC,IAAI,OAAO,QAAQ,SAAS,KAAK,iBAAiB,cAAc,SAC5D,QAAQ,KAAK,CAAC;AAEtB;AAEA,KAAK,CAAC,CAAC,OAAO,QAAQ;CAKlB,MAAM,SAAS,gBAAgB,KAAK,QAAQ,IAAI,gBAAgB,QAAQ,IAAI,uBAAuB;CACnG,IAAI,QACA,QAAQ,MAAM,MAAM;MAEpB,QAAQ,MAAM,MAAM,IAAI,mBAAmB,GAAG,eAAe,QAAS,IAAI,SAAS,IAAI,UAAW,OAAO,GAAG,CAAC;CAEjH,QAAQ,KAAK,CAAC;AAClB,CAAC"}
1
+ {"version":3,"file":"doctor-cli.js","names":[],"sources":["../../src/schema/doctor-policy-checks.ts","../../src/schema/doctor-search-scale.ts","../../src/schema/doctor-cli.ts"],"sourcesContent":["/**\n * The RLS half of `rebase doctor`, and the exit code `--policies` gates CI on.\n *\n * Lives beside `doctor-cli.ts` rather than inside it because that file runs\n * `main()` on import: a gate whose failure modes cannot be unit-tested is how\n * this one shipped reporting success for work it never did.\n */\nimport path from \"path\";\nimport chalk from \"chalk\";\nimport { CollectionConfig } from \"@rebasepro/types\";\nimport { loadCollections } from \"./doctor\";\nimport { checkPolicyDrift, formatPolicyDrift, hasDrift } from \"../security/policy-drift\";\nimport { validatePolicyPgRoles, warnOnAnonymousGrants } from \"../security/rls-enforcement\";\n\n/**\n * What the RLS checks concluded.\n *\n * `unchecked` exists because \"we could not look\" and \"we looked and it is fine\"\n * used to be reported identically: a collections path that did not resolve made\n * the loader return `[]` (it warns, it does not throw), `checkPolicyDrift`\n * early-returned an empty diff, and the gate printed\n * `✓ RLS policies match your collections` having compared zero policies against\n * zero collections. Any exception at all — a collection file that throws on\n * import, a `pg_policies` read the CI role is not granted, a connection reset —\n * did the same thing through a `warn`, and exited 0.\n */\nexport type PolicyCheckStatus = \"ok\" | \"problems\" | \"unchecked\";\n\n/**\n * The exit code for `rebase doctor --policies`.\n *\n * A gate that could not run has not passed. Only a completed, clean check\n * exits 0 — anything else, including \"we never opened a connection\", is a\n * failure, or the flag certifies a database nobody looked at.\n */\nexport function exitCodeForPolicyGate(status: PolicyCheckStatus): 0 | 1 {\n return status === \"ok\" ? 0 : 1;\n}\n\n/**\n * Policies actually deployed vs the ones the collections describe, plus policy\n * roles this server could never satisfy.\n *\n * Never reports `ok` for work it did not do — see {@link PolicyCheckStatus}.\n */\nexport async function runPolicyChecks(collectionsPath: string, databaseUrl?: string): Promise<PolicyCheckStatus> {\n if (!databaseUrl) {\n console.error(chalk.yellow(\" ⚠ No DATABASE_URL — RLS policies were NOT checked\"));\n return \"unchecked\";\n }\n\n const resolvedPath = path.resolve(process.cwd(), collectionsPath);\n let problems = false;\n const { Pool } = await import(\"pg\");\n const pool = new Pool({ connectionString: databaseUrl });\n try {\n const collections: CollectionConfig[] = await loadCollections(resolvedPath);\n\n // Zero collections is not \"no drift\", it is nothing to compare. The\n // loader returns `[]` for a path that does not exist, so the commonest\n // way to get here is a `--collections` path resolved against the wrong\n // directory — which used to render as a green tick.\n if (collections.length === 0) {\n console.error(chalk.red(` ✗ No collections found in ${resolvedPath}`));\n console.error(chalk.gray(\" RLS policies were NOT checked. Pass --collections=<dir> if your collections live elsewhere.\"));\n return \"unchecked\";\n }\n\n const runSql = async (text: string) => (await pool.query(text)).rows as Record<string, unknown>[];\n\n // Prove the connection before anything is ticked.\n //\n // `validatePolicyPgRoles` returns without a query when no collection\n // names a `pgRoles` — which is most projects — so it \"passed\" against a\n // database that was never reached, and the tick printed above an\n // ECONNREFUSED that only surfaced two checks later. Ten lines up, this\n // file says that reporting success for work it never did is the reason\n // it exists. This is the same shape, one function along.\n try {\n await runSql(\"SELECT 1\");\n } catch (err) {\n console.error(chalk.gray(\" ⏭ Policy roles: not checked\"));\n console.error(chalk.red(\" ✗ Could not check RLS policies:\"), err instanceof Error ? err.message : String(err));\n return \"unchecked\";\n }\n\n // A policy naming a role the server never runs as filters every row, so\n // the collection reads as empty. Report it without booting a server.\n try {\n await validatePolicyPgRoles(runSql, collections as never);\n console.log(chalk.green(\" ✓ Policy roles are usable by this server\"));\n } catch (err) {\n problems = true;\n console.log(\"\");\n console.error(chalk.red(err instanceof Error ? err.message : String(err)));\n }\n\n // A rule that reads as a lockdown but is true for every caller compiles\n // to a grant. Report it without booting a server.\n warnOnAnonymousGrants(collections as never);\n\n const drift = await checkPolicyDrift(pool as never, collections);\n console.log(\"\");\n if (hasDrift(drift)) {\n problems = true;\n console.log(chalk.yellow(\" RLS policies: database does not match your collections\"));\n console.log(formatPolicyDrift(drift));\n } else {\n console.log(chalk.green(` ✓ RLS policies match your collections (${collections.length} collection(s) checked)`));\n }\n } catch (err) {\n // Fail closed. This catch covers every query and every collection import\n // above, so returning the pre-catch verdict made the documented CI gate\n // green on exactly the runs it exists to catch.\n console.error(chalk.red(\" ✗ Could not check RLS policies:\"), err instanceof Error ? err.message : String(err));\n return \"unchecked\";\n } finally {\n await pool.end();\n }\n return problems ? \"problems\" : \"ok\";\n}\n","/**\n * `rebase doctor`: collections searched by a full scan of a large table.\n *\n * Without a `search` block, a search — the admin's search box, `?searchString=`,\n * `.search()` — is `ILIKE '%term%'` over every text column. A leading `%`\n * defeats a B-tree, so each one reads the whole table: on 100k posts a search\n * took 326 ms, half for the page and half for its count. Nothing reports it;\n * the box just gets slower as the table grows. This names the collections\n * where that has started to matter, and points at the block that fixes it.\n *\n * A hint, not a failure: it never changes doctor's exit code, and the database\n * is not changed — Rebase does not add a search index on its own.\n *\n * Lives beside `doctor-cli.ts` rather than inside it because that file runs\n * `main()` on import.\n */\nimport path from \"path\";\nimport chalk from \"chalk\";\nimport { getTableName, relationalCollections } from \"@rebasepro/common\";\nimport { isPostgresCollectionConfig, type CollectionConfig } from \"@rebasepro/types\";\nimport { loadCollections } from \"./doctor\";\nimport { buildSearchColumnSpec } from \"./search-column\";\n\n/** Above this many rows an unindexed search is worth a word. */\nexport const UNINDEXED_SEARCH_ROWS = 10_000;\n\n/** A collection whose search would be `ILIKE` over these fields. */\nexport interface IlikeSearchedCollection {\n slug: string;\n schema: string;\n table: string;\n /** The string properties the search reads. */\n fields: string[];\n}\n\n/**\n * The collections a search reaches without an index: no `search` block (or\n * one that does not build), and at least one property the default search\n * reads — a `string`, not an enum, not a uuid key (the rule in\n * `DrizzleConditionBuilder.buildSearchConditions`).\n */\nexport function ilikeSearchedCollections(collections: CollectionConfig[]): IlikeSearchedCollection[] {\n const found: IlikeSearchedCollection[] = [];\n for (const collection of relationalCollections(collections)) {\n if (!isPostgresCollectionConfig(collection)) continue;\n let indexed: boolean;\n try {\n indexed = buildSearchColumnSpec(collection) !== undefined;\n } catch {\n // A block that does not build is reported by the boot; the search\n // falls back to ILIKE meanwhile, so it counts as none here.\n indexed = false;\n }\n if (indexed) continue;\n const fields = Object.entries(collection.properties ?? {})\n .filter(([, property]) => property.type === \"string\" && !property.enum && property.isId !== \"uuid\")\n .map(([key]) => key);\n const table = getTableName(collection);\n if (fields.length === 0 || !table) continue;\n found.push({ slug: collection.slug, schema: collection.schema ?? \"public\", table, fields });\n }\n return found;\n}\n\n/** Those of `candidates` whose table holds more than `threshold` rows, by estimate. */\nexport function unindexedSearchFindings(\n candidates: IlikeSearchedCollection[],\n rowEstimates: ReadonlyMap<string, number>,\n threshold: number = UNINDEXED_SEARCH_ROWS\n): (IlikeSearchedCollection & { rows: number })[] {\n return candidates\n .map(candidate => ({ ...candidate, rows: rowEstimates.get(`${candidate.schema}.${candidate.table}`) ?? 0 }))\n .filter(candidate => candidate.rows > threshold);\n}\n\n/**\n * Row counts by `schema.table`, estimated — `pg_class.reltuples`, or the\n * statistics collector's live-tuple count when the table was never analysed.\n * Nothing is scanned: counting the tables this is about would be the cost it\n * warns of.\n */\nexport async function estimateRowCounts(\n query: (text: string, values: unknown[]) => Promise<{ rows: { schema: string; table: string; rows: string | number }[] }>,\n schemas: string[]\n): Promise<Map<string, number>> {\n const { rows } = await query(\n `SELECT n.nspname AS schema, c.relname AS table,\n GREATEST(c.reltuples, COALESCE(s.n_live_tup, 0), 0)::bigint AS rows\n FROM pg_class c\n JOIN pg_namespace n ON n.oid = c.relnamespace\n LEFT JOIN pg_stat_user_tables s ON s.relid = c.oid\n WHERE c.relkind IN ('r', 'p') AND n.nspname = ANY($1)`,\n [schemas]\n );\n return new Map(rows.map(row => [`${row.schema}.${row.table}`, Number(row.rows)]));\n}\n\n/** The report, one warning per collection. */\nexport function formatUnindexedSearches(findings: (IlikeSearchedCollection & { rows: number })[]): string[] {\n if (findings.length === 0) return [];\n const lines = [\"\", chalk.yellow(` ⚠ ${findings.length} collection(s) searched by reading the whole table:`)];\n for (const finding of findings) {\n lines.push(chalk.yellow(\n ` • ${finding.slug} — about ${finding.rows.toLocaleString(\"en-US\")} rows, no \\`search\\` block: a search is ` +\n `ILIKE '%term%' over ${finding.fields.join(\", \")}`\n ));\n }\n lines.push(chalk.gray(\n \" Each search reads every row, and its count reads them again. A `search` block indexes it\\n\" +\n \" (full-text, or `mode: \\\"hybrid\\\"` for substrings too): https://rebase.pro/docs/backend/search\"\n ));\n return lines;\n}\n\n/**\n * Print the warning for the project at `collectionsPath`, against\n * `databaseUrl`. Never throws and never fails the run: it is a hint, and a\n * check that could not run says so in one grey line.\n */\nexport async function runSearchScaleCheck(collectionsPath: string, databaseUrl?: string): Promise<void> {\n if (!databaseUrl) return;\n const { Client } = await import(\"pg\");\n const client = new Client({ connectionString: databaseUrl });\n try {\n const collections = await loadCollections(path.resolve(process.cwd(), collectionsPath));\n const candidates = ilikeSearchedCollections(collections);\n if (candidates.length === 0) return;\n await client.connect();\n const estimates = await estimateRowCounts(\n (text, values) => client.query(text, values),\n [...new Set(candidates.map(candidate => candidate.schema))]\n );\n for (const line of formatUnindexedSearches(unindexedSearchFindings(candidates, estimates))) console.log(line);\n } catch (err) {\n console.log(chalk.gray(` ⏭ Search on large tables: not checked (${err instanceof Error ? err.message : String(err)})`));\n } finally {\n await client.end().catch(() => undefined);\n }\n}\n","#!/usr/bin/env node\n/**\n * CLI entry point for `rebase doctor`.\n * Invoked via tsx by the server-postgres CLI plugin.\n */\nimport path from \"path\";\nimport chalk from \"chalk\";\nimport { runDoctor } from \"./doctor\";\nimport { exitCodeForPolicyGate, runPolicyChecks } from \"./doctor-policy-checks\";\nimport { runSearchScaleCheck } from \"./doctor-search-scale\";\nimport { diagnoseDbError } from \"../cli-errors\";\nimport { describeFailure } from \"../cli-output\";\n\nasync function main() {\n const collectionsArg = process.argv.find((a) => a.startsWith(\"--collections=\"));\n const schemaArg = process.argv.find((a) => a.startsWith(\"--schema=\"));\n const sdkArg = process.argv.find((a) => a.startsWith(\"--sdk=\"));\n // --policies runs only the RLS checks: no schema diff, no SDK types. Useful\n // as a CI gate against a deployed database, where the collection files and\n // generated schema are not what you are asking about.\n const policiesOnly = process.argv.includes(\"--policies\");\n\n const collectionsPath = collectionsArg?.split(\"=\")[1] ?? path.join(\"..\", \"config\", \"collections\");\n const schemaPath = schemaArg?.split(\"=\")[1] ?? path.join(\"src\", \"schema.generated.ts\");\n const sdkPath = sdkArg?.split(\"=\")[1] ?? path.join(\"..\", \"generated\", \"sdk\", \"database.types.ts\");\n\n // Load .env\n try {\n const dotenv = await import(\"dotenv\");\n const envPath = process.env.DOTENV_CONFIG_PATH;\n if (envPath) {\n dotenv.config({ path: envPath, quiet: true });\n } else {\n dotenv.config({ quiet: true });\n }\n } catch {\n // dotenv may not be installed\n }\n\n const databaseUrl = process.env.DATABASE_URL || process.env.ADMIN_CONNECTION_STRING;\n\n if (policiesOnly) {\n // Non-zero so this can gate CI — the whole point of the flag, and a\n // check that could not run has not passed. See exitCodeForPolicyGate.\n process.exit(exitCodeForPolicyGate(await runPolicyChecks(collectionsPath, databaseUrl)));\n }\n\n const report = await runDoctor({\n collectionsPath: path.resolve(process.cwd(), collectionsPath),\n schemaPath: path.resolve(process.cwd(), schemaPath),\n sdkPath: path.resolve(process.cwd(), sdkPath),\n databaseUrl: databaseUrl ?? undefined\n });\n\n const policyStatus = await runPolicyChecks(collectionsPath, databaseUrl);\n\n // A hint about speed, not drift: it never changes the exit code.\n await runSearchScaleCheck(collectionsPath, databaseUrl);\n\n // Exit non-zero if there are errors. A policy run that could not happen is\n // reported loudly above but does not fail the interactive command — the\n // same treatment the *chosen* skip of the database phase gets. `--policies`\n // is the gate, and that one fails closed.\n //\n // A phase that was skipped by a fault is different, and `blocked` is what\n // separates the two: \"no DATABASE_URL\" is a local situation somebody may be\n // content with, while \"the DATABASE_URL you set refuses connections\" would\n // otherwise let `rebase doctor` go green in CI against a database it never\n // reached.\n const blocked = [report.collectionsToSchema, report.schemaToDatabase, report.collectionsToSdk]\n .some(phase => phase.blocked);\n if (report.summary.errors > 0 || policyStatus === \"problems\" || blocked) {\n process.exit(1);\n }\n}\n\nmain().catch((err) => {\n // A known database failure has a diagnosis; a stack trace through pg and\n // drizzle internals is not it. This is the last resort — `runDoctor`\n // already catches the connection failure it can name — so it exists for\n // everything reached after the report, the policy checks in particular.\n const banner = diagnoseDbError(err, process.env.DATABASE_URL || process.env.ADMIN_CONNECTION_STRING);\n if (banner) {\n console.error(banner);\n } else {\n console.error(chalk.red(` ✗ Doctor failed: ${describeFailure(err)}`));\n }\n process.exit(1);\n});\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,SAAgB,sBAAsB,QAAkC;CACpE,OAAO,WAAW,OAAO,IAAI;AACjC;;;;;;;AAQA,eAAsB,gBAAgB,iBAAyB,aAAkD;CAC7G,IAAI,CAAC,aAAa;EACd,QAAQ,MAAM,MAAM,OAAO,qDAAqD,CAAC;EACjF,OAAO;CACX;CAEA,MAAM,eAAe,KAAK,QAAQ,QAAQ,IAAI,GAAG,eAAe;CAChE,IAAI,WAAW;CACf,MAAM,EAAE,SAAS,MAAM,OAAO;CAC9B,MAAM,OAAO,IAAI,KAAK,EAAE,kBAAkB,YAAY,CAAC;CACvD,IAAI;EACA,MAAM,cAAkC,MAAM,gBAAgB,YAAY;EAM1E,IAAI,YAAY,WAAW,GAAG;GAC1B,QAAQ,MAAM,MAAM,I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 -0,0 +1,45 @@
1
+ import { type CollectionConfig } from "@rebasepro/types";
2
+ /** Above this many rows an unindexed search is worth a word. */
3
+ export declare const UNINDEXED_SEARCH_ROWS = 10000;
4
+ /** A collection whose search would be `ILIKE` over these fields. */
5
+ export interface IlikeSearchedCollection {
6
+ slug: string;
7
+ schema: string;
8
+ table: string;
9
+ /** The string properties the search reads. */
10
+ fields: string[];
11
+ }
12
+ /**
13
+ * The collections a search reaches without an index: no `search` block (or
14
+ * one that does not build), and at least one property the default search
15
+ * reads — a `string`, not an enum, not a uuid key (the rule in
16
+ * `DrizzleConditionBuilder.buildSearchConditions`).
17
+ */
18
+ export declare function ilikeSearchedCollections(collections: CollectionConfig[]): IlikeSearchedCollection[];
19
+ /** Those of `candidates` whose table holds more than `threshold` rows, by estimate. */
20
+ export declare function unindexedSearchFindings(candidates: IlikeSearchedCollection[], rowEstimates: ReadonlyMap<string, number>, threshold?: number): (IlikeSearchedCollection & {
21
+ rows: number;
22
+ })[];
23
+ /**
24
+ * Row counts by `schema.table`, estimated — `pg_class.reltuples`, or the
25
+ * statistics collector's live-tuple count when the table was never analysed.
26
+ * Nothing is scanned: counting the tables this is about would be the cost it
27
+ * warns of.
28
+ */
29
+ export declare function estimateRowCounts(query: (text: string, values: unknown[]) => Promise<{
30
+ rows: {
31
+ schema: string;
32
+ table: string;
33
+ rows: string | number;
34
+ }[];
35
+ }>, schemas: string[]): Promise<Map<string, number>>;
36
+ /** The report, one warning per collection. */
37
+ export declare function formatUnindexedSearches(findings: (IlikeSearchedCollection & {
38
+ rows: number;
39
+ })[]): string[];
40
+ /**
41
+ * Print the warning for the project at `collectionsPath`, against
42
+ * `databaseUrl`. Never throws and never fails the run: it is a hint, and a
43
+ * check that could not run says so in one grey line.
44
+ */
45
+ export declare function runSearchScaleCheck(collectionsPath: string, databaseUrl?: string): Promise<void>;
@@ -1,5 +1,5 @@
1
1
  import { CollectionConfig, Property } from "@rebasepro/types";
2
- import type { PgType, TablePlan } from "./plan/types.js";
2
+ import type { PgType, SchemaPlan, TablePlan } from "./plan/types.js";
3
3
  export type IssueSeverity = "error" | "warning" | "info";
4
4
  export interface DoctorIssue {
5
5
  severity: IssueSeverity;
@@ -148,6 +148,25 @@ export declare function primaryKeyIssues(tablePlan: TablePlan, dbPk: string[], d
148
148
  */
149
149
  export declare function junctionKeyIssues(junctionPlan: TablePlan, dbColumns: DbColumn[], dbPk: string[], displayName: string): DoctorIssue[];
150
150
  export declare function checkCollectionsVsDatabase(collections: CollectionConfig[], databaseUrl: string): Promise<DoctorPhase>;
151
+ /**
152
+ * A `public` table in the junction shape that no collection declares.
153
+ *
154
+ * The shape is the planner's: exactly two columns, together the primary key,
155
+ * each a single-column foreign key to a collection's table. Renaming or
156
+ * removing a many-to-many leaves such a table behind — push and boot only
157
+ * create — with every link row still in it and nothing reading it.
158
+ *
159
+ * Info, never more: the table may be data somebody still wants. The remedy is
160
+ * shown, never run.
161
+ */
162
+ export declare function orphanedJunctionIssues(plan: SchemaPlan, columnsByTable: Map<string, {
163
+ column_name: string;
164
+ }[]>, pksByTable: Map<string, string[]>, fksByTable: Map<string, {
165
+ constraint_name: string;
166
+ column_name: string;
167
+ foreign_table_schema: string;
168
+ foreign_table_name: string;
169
+ }[]>): DoctorIssue[];
151
170
  export declare function renderReport(report: DoctorReport): void;
152
171
  export declare function runDoctor(options: {
153
172
  collectionsPath: string;
@@ -1,43 +1,17 @@
1
- /**
2
- * Applying a bundle's RLS policies to a database at boot, idempotently.
3
- *
4
- * ## Why this exists
5
- *
6
- * {@link ensureCollectionTables} creates the collection *tables* a managed
7
- * runtime boots against, but a table with row-level security disabled and no
8
- * policies is not servable: authenticated requests run as the restricted
9
- * `rebase_user` role, so a read with no `SELECT` policy returns nothing (a
10
- * public collection answered 401) and a write with no `INSERT`/`UPDATE` policy
11
- * is denied. The policies live in the collections' `securityRules`; nothing at
12
- * boot applied them. `rebase db push` does — but it drives Atlas against a
13
- * local `DATABASE_URL`, and a managed tenant's database is reachable only from
14
- * inside the cluster, by the runtime that is already connected to it. So the
15
- * runtime is the only thing that *can* apply them, and this is where it does.
16
- *
17
- * ## Why this is safe to run on every boot
18
- *
19
- * Every statement is idempotent: `ENABLE ROW LEVEL SECURITY` is a no-op once
20
- * enabled, and each policy is a `DROP POLICY IF EXISTS` immediately followed by
21
- * a `CREATE POLICY`, so re-applying asserts exactly the declared state. It adds
22
- * and replaces; it never drops data. (It does not *reconcile* — a policy a
23
- * previous push left behind under an old name is not removed here; that stays a
24
- * `db push` / `db migrate` concern, alongside destructive schema changes.)
25
- *
26
- * Unlike table creation, a failure here is not fatal: RLS stays enabled, so a
27
- * table whose policies could not be applied fails **closed** (denies) rather
28
- * than leaking rows. One collection's policy failing (e.g. a rule that
29
- * references a table a real migration has not created yet) must not crash-loop
30
- * the whole deployment and take the other collections' working routes down with
31
- * it. Failures are reported loudly and per-table so the operator can see
32
- * exactly which collection is not yet servable and why.
33
- */
34
1
  import { type CollectionConfig } from "@rebasepro/types";
2
+ import { type CollectionPolicyPlan } from "./generate-postgres-ddl-logic.js";
35
3
  import { type Queryable } from "./ensure-collection-tables.js";
4
+ import { type BootLockOptions } from "./boot-ddl.js";
36
5
  export interface PolicyEnsureResult {
37
6
  /** `CREATE POLICY` statements that ran successfully. */
38
7
  policiesApplied: number;
39
- /** Tables that had RLS enabled. */
8
+ /** Tables that had RLS enabled — including the {@link unchanged} ones, which already had it. */
40
9
  tablesSecured: number;
10
+ /**
11
+ * Tables whose policies were already exactly the declared ones, RLS on, by
12
+ * the stamp each policy carries — left alone: no statement, no lock.
13
+ */
14
+ unchanged: number;
41
15
  /** Declared tables absent from the database — left to a real migration. */
42
16
  skipped: {
43
17
  table: string;
@@ -80,7 +54,34 @@ export interface PolicyEnsureResult {
80
54
  * database — which is the reason this module exists. So boot has to do it.
81
55
  */
82
56
  orphansDropped: number;
57
+ /**
58
+ * Tables whose policies were not re-applied because another session held
59
+ * the table's lock for every attempt — a long transaction, a report, a
60
+ * dump. Each table's policy statements run in one transaction, so a table
61
+ * here was left exactly as it was: RLS on, its existing policies in force.
62
+ * `rulesChanged` says whether those differ from the declared ones by name
63
+ * (a rule whose policy is missing, or an orphan still granting), in which
64
+ * case the new rules are not in force on this table until a later boot.
65
+ */
66
+ deferred: {
67
+ table: string;
68
+ error: string;
69
+ rulesChanged: boolean;
70
+ }[];
83
71
  }
72
+ /** What every policy stamp starts with. See {@link policyStampSql}. */
73
+ export declare const POLICY_STAMP_PREFIX = "rebase:";
74
+ /**
75
+ * One statement that stamps every policy the plan just created on its table:
76
+ * `COMMENT ON POLICY … IS 'rebase:<hash>'`, the hash taken over the rendered
77
+ * statement's hash and the definition Postgres stored — read back here, after
78
+ * the `CREATE`s, in the same transaction. A `DO` block because the stored
79
+ * definition is only known once the policy exists, and a `COMMENT` takes a
80
+ * literal. One line, and it names only this table.
81
+ *
82
+ * `undefined` for a table the plan gives no policies.
83
+ */
84
+ export declare function policyStampSql(plan: CollectionPolicyPlan): string | undefined;
84
85
  export interface TableLockResult {
85
86
  /** Tables RLS is now on for. */
86
87
  locked: number;
@@ -103,4 +104,41 @@ export interface TableLockResult {
103
104
  * a table that does not exist, and the step that creates it locks it.
104
105
  */
105
106
  export declare function lockCollectionTables(client: Queryable, collections: CollectionConfig[]): Promise<TableLockResult>;
106
- export declare function ensureCollectionPolicies(client: Queryable, collections: CollectionConfig[], log?: (message: string) => void): Promise<PolicyEnsureResult>;
107
+ /**
108
+ * Bring the declared collections' RLS policies up to date. Returns what it did.
109
+ *
110
+ * Only tables that already exist are touched: the boot-time table creator runs
111
+ * first, so anything still missing is a table this additive path did not
112
+ * create (it creates junctions too) — one a failed boot DDL left out, or a
113
+ * project whose schema a migration owns. Enabling RLS on a
114
+ * non-existent table would error, so those are recorded as skipped, not failed.
115
+ *
116
+ * ## One transaction per table
117
+ *
118
+ * Each table's statements — `ENABLE` when RLS is off, every `DROP POLICY IF
119
+ * EXISTS` / `CREATE POLICY` pair, the orphan drops — go to Postgres as one
120
+ * string, which it runs as one transaction (see `boot-ddl`). Before, each was
121
+ * its own autocommit: at 240 tables, 4,080 commits and 4,080 ACCESS EXCLUSIVE
122
+ * lock acquisitions per boot, 6–22 s on a disk that really flushes. And between
123
+ * a policy's DROP and its CREATE, every other replica evaluated the table with
124
+ * that policy missing — a user's continuous reads of their own row came back
125
+ * empty once per boot (measured, Postgres 18). Now a concurrent request sees the
126
+ * old policies or the new ones, never neither.
127
+ *
128
+ * If the transaction fails for any reason but a lock timeout, the table is
129
+ * applied again one statement at a time, exactly as before — so a rule that
130
+ * cannot be created (a function a migration has not added yet) leaves the
131
+ * table in the same deny-leaning partial state it always did, rather than
132
+ * rolling back to the old, possibly looser, policies.
133
+ *
134
+ * ## Lock budget
135
+ *
136
+ * The transaction runs under `lock_timeout` (`boot-ddl`), so a table that a
137
+ * long transaction, a report or a dump is holding costs live traffic one
138
+ * second, not the statement timeout. A contended table is retried after the
139
+ * others, with backoff, within a retry budget for the whole step; one still
140
+ * held after that is reported in `deferred` — its transaction rolled back, so
141
+ * it is exactly as it was before this boot — or, if RLS was off on it, closed
142
+ * by revoking the user role's grant, as any table RLS cannot be enabled on is.
143
+ */
144
+ export declare function ensureCollectionPolicies(client: Queryable, collections: CollectionConfig[], log?: (message: string) => void, options?: BootLockOptions): Promise<PolicyEnsureResult>;