@rebasepro/server-postgres 0.17.3-canary.gdd23447 → 0.18.0

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 (149) hide show
  1. package/LICENSE +0 -1
  2. package/README.md +31 -0
  3. package/dist/backup-argv.d.ts +19 -0
  4. package/dist/branch-argv.d.ts +28 -0
  5. package/dist/branch-prune.d.ts +77 -0
  6. package/dist/cli-collections-path.d.ts +31 -0
  7. package/dist/cli-errors.d.ts +123 -0
  8. package/dist/cli-flags.d.ts +101 -0
  9. package/dist/cli-helpers.d.ts +94 -10
  10. package/dist/collection-index-C7LopMyX.js +247 -0
  11. package/dist/collection-index-C7LopMyX.js.map +1 -0
  12. package/dist/databasePoolManager.d.ts +27 -0
  13. package/dist/{ensure-collection-policies-UCqgv_8c.js → ensure-collection-policies-CagOzEPS.js} +6 -6
  14. package/dist/{ensure-collection-policies-UCqgv_8c.js.map → ensure-collection-policies-CagOzEPS.js.map} +1 -1
  15. package/dist/{ensure-collection-tables-DgVixhX3.js → ensure-collection-tables-DbAhrjnG.js} +614 -52
  16. package/dist/ensure-collection-tables-DbAhrjnG.js.map +1 -0
  17. package/dist/index.d.ts +12 -0
  18. package/dist/index.es.js +19307 -3059
  19. package/dist/index.es.js.map +1 -1
  20. package/dist/{rls-enforcement-DvAbL9YJ.js → rls-enforcement-ClJoTboT.js} +53 -189
  21. package/dist/rls-enforcement-ClJoTboT.js.map +1 -0
  22. package/dist/schema/column-type-drift.d.ts +79 -0
  23. package/dist/schema/doctor.d.ts +42 -0
  24. package/dist/schema/ensure-collection-tables.d.ts +90 -0
  25. package/dist/schema/generate-next-step.d.ts +30 -0
  26. package/dist/schema/generate-postgres-ddl-logic.d.ts +28 -0
  27. package/dist/schema/generated-schema-staleness.d.ts +37 -0
  28. package/dist/schema/introspect-db-queries.d.ts +34 -0
  29. package/dist/security/rls-enforcement.d.ts +63 -2
  30. package/dist/services/BranchService.d.ts +86 -1
  31. package/dist/services/collection-helpers.d.ts +1 -1
  32. package/dist/services/realtimeService.d.ts +30 -2
  33. package/dist/services/row-pipeline.d.ts +12 -1
  34. package/dist/utils/pg-error-utils.d.ts +3 -1
  35. package/dist/utils/pg-numeric-number-patch.d.ts +31 -0
  36. package/package.json +32 -27
  37. package/dist/backup-service-DCk7KhhL.js +0 -9044
  38. package/dist/backup-service-DCk7KhhL.js.map +0 -1
  39. package/dist/collection-index-BRUg10H5.js +0 -5690
  40. package/dist/collection-index-BRUg10H5.js.map +0 -1
  41. package/dist/connection-GOKU3Hu5.js +0 -277
  42. package/dist/connection-GOKU3Hu5.js.map +0 -1
  43. package/dist/ensure-collection-tables-DgVixhX3.js.map +0 -1
  44. package/dist/rls-bootstrap-sql-B5C9LoJ6.js +0 -244
  45. package/dist/rls-bootstrap-sql-B5C9LoJ6.js.map +0 -1
  46. package/dist/rls-enforcement-DvAbL9YJ.js.map +0 -1
  47. package/dist/src-DiB5RP2Z.js +0 -541
  48. package/dist/src-DiB5RP2Z.js.map +0 -1
  49. package/dist/websocket-BZ4H5wUz.js +0 -8198
  50. package/dist/websocket-BZ4H5wUz.js.map +0 -1
  51. package/src/PostgresAdapter.ts +0 -91
  52. package/src/PostgresBackendDriver.ts +0 -1857
  53. package/src/PostgresBootstrapper.ts +0 -1157
  54. package/src/auth/ensure-tables.ts +0 -896
  55. package/src/auth/schema-version.ts +0 -260
  56. package/src/auth/services.ts +0 -1610
  57. package/src/backup/backup-cli.ts +0 -520
  58. package/src/backup/backup-cron.ts +0 -212
  59. package/src/backup/backup-logic.ts +0 -93
  60. package/src/backup/backup-service.ts +0 -485
  61. package/src/backup/index.ts +0 -12
  62. package/src/backup/pg-tools.ts +0 -403
  63. package/src/backup/retention.ts +0 -86
  64. package/src/cli-errors.ts +0 -265
  65. package/src/cli-helpers.ts +0 -569
  66. package/src/cli-output.ts +0 -43
  67. package/src/cli.ts +0 -1339
  68. package/src/collections/PostgresCollectionRegistry.ts +0 -108
  69. package/src/collections/buildRegistry.ts +0 -68
  70. package/src/collections/validate-relations.ts +0 -402
  71. package/src/connection.ts +0 -333
  72. package/src/data-transformer.ts +0 -845
  73. package/src/databasePoolManager.ts +0 -95
  74. package/src/history/HistoryService.ts +0 -254
  75. package/src/history/ensure-history-table.ts +0 -53
  76. package/src/index.ts +0 -19
  77. package/src/interfaces.ts +0 -60
  78. package/src/module-dir.ts +0 -7
  79. package/src/schema/atlas-argv.ts +0 -94
  80. package/src/schema/auth-schema.ts +0 -298
  81. package/src/schema/auth-users-columns.ts +0 -131
  82. package/src/schema/carved-out-migration.ts +0 -404
  83. package/src/schema/classify-change.ts +0 -436
  84. package/src/schema/collection-index.ts +0 -427
  85. package/src/schema/collections-schema-version.ts +0 -103
  86. package/src/schema/destructive-sql.ts +0 -94
  87. package/src/schema/doctor-cli.ts +0 -66
  88. package/src/schema/doctor-policy-checks.ts +0 -105
  89. package/src/schema/doctor.ts +0 -837
  90. package/src/schema/drizzle-ddl.ts +0 -33
  91. package/src/schema/dynamic-tables.test.ts +0 -302
  92. package/src/schema/dynamic-tables.ts +0 -293
  93. package/src/schema/ensure-collection-policies.ts +0 -198
  94. package/src/schema/ensure-collection-tables.test.ts +0 -518
  95. package/src/schema/ensure-collection-tables.ts +0 -1267
  96. package/src/schema/generate-drizzle-schema-logic.ts +0 -957
  97. package/src/schema/generate-drizzle-schema.ts +0 -143
  98. package/src/schema/generate-postgres-ddl-logic.ts +0 -1339
  99. package/src/schema/generate-postgres-ddl.ts +0 -180
  100. package/src/schema/generate-schema-commit.ts +0 -267
  101. package/src/schema/generated-schema-staleness.ts +0 -284
  102. package/src/schema/introspect-db-constraints.ts +0 -385
  103. package/src/schema/introspect-db-inference.ts +0 -255
  104. package/src/schema/introspect-db-logic.ts +0 -1387
  105. package/src/schema/introspect-db-naming.ts +0 -15
  106. package/src/schema/introspect-db-project.ts +0 -78
  107. package/src/schema/introspect-db-queries.ts +0 -326
  108. package/src/schema/introspect-db-structure.ts +0 -670
  109. package/src/schema/introspect-db-types.ts +0 -56
  110. package/src/schema/introspect-db.ts +0 -257
  111. package/src/schema/introspect-runtime.test.ts +0 -260
  112. package/src/schema/introspect-runtime.ts +0 -327
  113. package/src/schema/non-sql-collections.test.ts +0 -131
  114. package/src/schema/rls-bootstrap-sql.ts +0 -288
  115. package/src/schema/search-column.ts +0 -643
  116. package/src/schema/test-schema.ts +0 -11
  117. package/src/schema/vector-index.ts +0 -491
  118. package/src/security/anonymous-grants.test.ts +0 -73
  119. package/src/security/policy-drift.test.ts +0 -435
  120. package/src/security/policy-drift.ts +0 -473
  121. package/src/security/rls-enforcement.ts +0 -505
  122. package/src/services/BranchService.ts +0 -326
  123. package/src/services/FetchService.ts +0 -2115
  124. package/src/services/PersistService.ts +0 -557
  125. package/src/services/RelationService.ts +0 -978
  126. package/src/services/RelationWriteService.ts +0 -653
  127. package/src/services/cdc/CdcListener.ts +0 -103
  128. package/src/services/cdc/junction-tables.ts +0 -91
  129. package/src/services/cdc/trigger-cdc.ts +0 -173
  130. package/src/services/channel-bus/ChannelBus.ts +0 -44
  131. package/src/services/channel-bus/PostgresChannelBus.ts +0 -299
  132. package/src/services/channel-bus/index.ts +0 -114
  133. package/src/services/channel-history.ts +0 -424
  134. package/src/services/channel-presence.ts +0 -185
  135. package/src/services/collection-helpers.ts +0 -420
  136. package/src/services/dataService.ts +0 -256
  137. package/src/services/index.ts +0 -14
  138. package/src/services/junction-writes.ts +0 -295
  139. package/src/services/nested-path.ts +0 -145
  140. package/src/services/pg-notify-listener.ts +0 -151
  141. package/src/services/realtimeService.ts +0 -2545
  142. package/src/services/row-pipeline.ts +0 -240
  143. package/src/services/write-denial.ts +0 -55
  144. package/src/types.ts +0 -4
  145. package/src/utils/connection-string.ts +0 -58
  146. package/src/utils/drizzle-conditions.ts +0 -2743
  147. package/src/utils/pg-array-null-patch.ts +0 -42
  148. package/src/utils/pg-error-utils.ts +0 -391
  149. package/src/websocket.ts +0 -855
package/src/cli-errors.ts DELETED
@@ -1,265 +0,0 @@
1
- import chalk from "chalk";
2
- import { outWarn, outError } from "./cli-output";
3
-
4
- /**
5
- * Detect whether an error (or AggregateError wrapping multiple attempts)
6
- * represents an ECONNREFUSED — i.e. the database is simply not running.
7
- *
8
- * Handles:
9
- * - Direct `{ code: "ECONNREFUSED" }` errors from Node `net`
10
- * - `AggregateError` from dual-stack IPv4+IPv6 connection attempts
11
- * - Drizzle's `cause`-wrapped pg errors
12
- */
13
- export function isEconnrefused(err: unknown): boolean {
14
- if (!err || typeof err !== "object") return false;
15
- const e = err as { code?: string; cause?: unknown; errors?: unknown[] };
16
- if (e.code === "ECONNREFUSED") return true;
17
- // AggregateError from Node net (dual-stack IPv4 + IPv6)
18
- if (Array.isArray(e.errors)) {
19
- return e.errors.some(inner =>
20
- inner && typeof inner === "object" && (inner as { code?: string }).code === "ECONNREFUSED"
21
- );
22
- }
23
- // Drizzle wraps the pg error in `cause`
24
- if (e.cause && typeof e.cause === "object") {
25
- return isEconnrefused(e.cause);
26
- }
27
- return false;
28
- }
29
-
30
- /**
31
- * Detect PostgreSQL authentication failures.
32
- * PG error codes: 28P01 (invalid_password), 28000 (invalid_authorization_specification)
33
- */
34
- export function isAuthFailure(err: unknown): boolean {
35
- if (!err || typeof err !== "object") return false;
36
- const e = err as { code?: string; cause?: unknown };
37
- if (e.code === "28P01" || e.code === "28000") return true;
38
- if (e.cause && typeof e.cause === "object") {
39
- return isAuthFailure(e.cause);
40
- }
41
- // Also check the message for common pg auth failure text
42
- if ("message" in e && typeof (e as { message?: string }).message === "string") {
43
- const msg = (e as { message: string }).message.toLowerCase();
44
- if (msg.includes("password authentication failed") || msg.includes("no pg_hba.conf entry")) {
45
- return true;
46
- }
47
- }
48
- return false;
49
- }
50
-
51
- /**
52
- * Detect the "SSL is not enabled on the server" failure — the client attempted
53
- * an SSL handshake against a Postgres server that doesn't support it (common
54
- * with a plain local dev database). The fix is `?sslmode=disable` on the URL.
55
- */
56
- export function isSslNotEnabled(err: unknown): boolean {
57
- if (!err || typeof err !== "object") return false;
58
- const e = err as { message?: string; cause?: unknown };
59
- if (typeof e.message === "string" && e.message.toLowerCase().includes("ssl is not enabled on the server")) {
60
- return true;
61
- }
62
- if (e.cause && typeof e.cause === "object") {
63
- return isSslNotEnabled(e.cause);
64
- }
65
- return false;
66
- }
67
-
68
- /**
69
- * Detect PostgreSQL "cannot drop ... because other objects depend on it"
70
- * (error code 2BP01, dependent_objects_still_exist). This is the failure that
71
- * strands a declarative `db push` half-applied when a collection is removed but
72
- * an enum type it defined is still referenced by another object.
73
- */
74
- export function isDependencyDropError(err: unknown): boolean {
75
- if (!err || typeof err !== "object") return false;
76
- const e = err as { code?: string; message?: string; cause?: unknown };
77
- if (e.code === "2BP01") return true;
78
- if (typeof e.message === "string") {
79
- const msg = e.message.toLowerCase();
80
- if (msg.includes("other objects depend on it") || msg.includes("cannot drop type")) {
81
- return true;
82
- }
83
- }
84
- if (e.cause && typeof e.cause === "object") {
85
- return isDependencyDropError(e.cause);
86
- }
87
- return false;
88
- }
89
-
90
- /**
91
- * Parse host:port from a DATABASE_URL for display purposes.
92
- */
93
- function parseHostInfo(databaseUrl: string): string {
94
- try {
95
- const parsed = new URL(databaseUrl);
96
- return `${parsed.hostname}:${parsed.port || 5432}`;
97
- } catch {
98
- return "unknown";
99
- }
100
- }
101
-
102
- /**
103
- * Format a diagnostic banner for ECONNREFUSED errors.
104
- */
105
- function formatConnectionRefusedBanner(databaseUrl: string): string {
106
- const hostInfo = parseHostInfo(databaseUrl);
107
- return (
108
- `\n` +
109
- `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n` +
110
- ` ❌ Cannot connect to PostgreSQL at ${hostInfo}\n` +
111
- `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n` +
112
- `\n` +
113
- ` The database server is not running or is not accepting\n` +
114
- ` connections. Common fixes:\n` +
115
- `\n` +
116
- ` • docker compose up -d db (the service a Rebase scaffold ships)\n` +
117
- ` • brew services start postgresql@18\n` +
118
- ` • Verify DATABASE_URL in your .env file\n` +
119
- `\n` +
120
- `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n`
121
- );
122
- }
123
-
124
- /**
125
- * Format a diagnostic banner for authentication failures.
126
- */
127
- function formatAuthFailureBanner(databaseUrl: string): string {
128
- const hostInfo = parseHostInfo(databaseUrl);
129
- let username = "unknown";
130
- try {
131
- username = new URL(databaseUrl).username || "unknown";
132
- } catch { /* ignore */ }
133
-
134
- return (
135
- `\n` +
136
- `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n` +
137
- ` ❌ Authentication failed for user "${username}" at ${hostInfo}\n` +
138
- `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n` +
139
- `\n` +
140
- ` PostgreSQL rejected the credentials. Common fixes:\n` +
141
- `\n` +
142
- ` • Check the username and password in DATABASE_URL\n` +
143
- ` • Verify the user exists: psql -c "\\du"\n` +
144
- ` • Reset the password: ALTER USER ${username} PASSWORD 'new_password';\n` +
145
- `\n` +
146
- `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n`
147
- );
148
- }
149
-
150
- /**
151
- * Format a diagnostic banner for "SSL is not enabled on the server".
152
- */
153
- function formatSslNotEnabledBanner(databaseUrl: string): string {
154
- const hostInfo = parseHostInfo(databaseUrl);
155
- const suggestion = databaseUrl.includes("?") ? "&sslmode=disable" : "?sslmode=disable";
156
- return (
157
- `\n` +
158
- `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n` +
159
- ` ❌ SSL is not enabled on the PostgreSQL server at ${hostInfo}\n` +
160
- `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n` +
161
- `\n` +
162
- ` The client tried to connect over SSL, but the server does not\n` +
163
- ` support it. This is normal for a plain local dev database.\n` +
164
- `\n` +
165
- ` Fix: append ${chalk.bold("sslmode=disable")} to DATABASE_URL, e.g.\n` +
166
- `\n` +
167
- ` DATABASE_URL=...${suggestion}\n` +
168
- `\n` +
169
- `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n`
170
- );
171
- }
172
-
173
- /**
174
- * Format a diagnostic banner for a dependency-drop failure during `db push`.
175
- * Explains that the database may be left partially migrated and how to recover.
176
- */
177
- function formatDependencyDropBanner(): string {
178
- return (
179
- `\n` +
180
- `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n` +
181
- ` ❌ Schema push failed: a type/table could not be dropped\n` +
182
- ` because other objects still depend on it.\n` +
183
- `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n` +
184
- `\n` +
185
- ` ${chalk.yellow("The database may now be partially migrated.")} Atlas applies\n` +
186
- ` statements individually, so earlier changes in this push may\n` +
187
- ` already be committed while later ones failed.\n` +
188
- `\n` +
189
- ` This commonly happens when a collection is removed but an enum\n` +
190
- ` type it defined is still referenced. To recover:\n` +
191
- `\n` +
192
- ` 1. Inspect the leftover object named in the error above.\n` +
193
- ` 2. Drop it with CASCADE, e.g.:\n` +
194
- ` psql "$DATABASE_URL" -c 'DROP TYPE "<name>" CASCADE;'\n` +
195
- ` 3. Re-run: ${chalk.bold.green("rebase db push")}\n` +
196
- `\n` +
197
- ` Prefer a safe, versioned workflow? Use ${chalk.bold("rebase db generate")}\n` +
198
- ` + ${chalk.bold("rebase db migrate")} instead of push for destructive changes.\n` +
199
- `\n` +
200
- `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n`
201
- );
202
- }
203
-
204
- /**
205
- * Pre-flight check: verify that the database is reachable before running
206
- * a heavy subprocess (Atlas, migrations, etc.).
207
- *
208
- * Exits with code 1 and a friendly banner on known failure modes.
209
- * On unknown errors, logs a warning and allows the caller to proceed.
210
- */
211
- export async function checkDatabaseConnectivity(databaseUrl: string): Promise<void> {
212
- let client: import("pg").Client | undefined;
213
- try {
214
- const { Client } = await import("pg");
215
- client = new Client({
216
- connectionString: databaseUrl,
217
- connectionTimeoutMillis: 5000
218
- });
219
- await client.connect();
220
- await client.query("SELECT 1");
221
- } catch (err: unknown) {
222
- if (isEconnrefused(err)) {
223
- outError(formatConnectionRefusedBanner(databaseUrl));
224
- process.exit(1);
225
- }
226
- if (isAuthFailure(err)) {
227
- outError(formatAuthFailureBanner(databaseUrl));
228
- process.exit(1);
229
- }
230
- if (isSslNotEnabled(err)) {
231
- outError(formatSslNotEnabledBanner(databaseUrl));
232
- process.exit(1);
233
- }
234
- // Unknown error — warn but don't block; let the downstream tool surface details
235
- outWarn(chalk.yellow(` ⚠ Could not verify database connectivity: ${err instanceof Error ? err.message : String(err)}`));
236
- outWarn(chalk.gray(" Proceeding anyway — the command may fail if the database is unreachable."));
237
- } finally {
238
- try {
239
- await client?.end();
240
- } catch {
241
- // ignore cleanup errors
242
- }
243
- }
244
- }
245
-
246
- /**
247
- * Post-hoc error diagnosis for direct database operations (e.g. applyPolicies).
248
- * Returns a formatted diagnostic string if the error matches a known pattern,
249
- * or null if unrecognized.
250
- */
251
- export function diagnoseDbError(err: unknown, databaseUrl?: string): string | null {
252
- if (isEconnrefused(err)) {
253
- return formatConnectionRefusedBanner(databaseUrl || "");
254
- }
255
- if (isAuthFailure(err)) {
256
- return formatAuthFailureBanner(databaseUrl || "");
257
- }
258
- if (isSslNotEnabled(err)) {
259
- return formatSslNotEnabledBanner(databaseUrl || "");
260
- }
261
- if (isDependencyDropError(err)) {
262
- return formatDependencyDropBanner();
263
- }
264
- return null;
265
- }