@rebasepro/server-postgres 0.17.3 → 0.18.1

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 (204) hide show
  1. package/LICENSE +0 -1
  2. package/README.md +31 -0
  3. package/dist/BranchService-ByLfM_Tt.js +360 -0
  4. package/dist/BranchService-ByLfM_Tt.js.map +1 -0
  5. package/dist/__vite-browser-external-BnuHet1e.js +46 -0
  6. package/dist/__vite-browser-external-BnuHet1e.js.map +1 -0
  7. package/dist/arg-Dni7MzLB.js +87 -0
  8. package/dist/arg-Dni7MzLB.js.map +1 -0
  9. package/dist/auth-users-columns-D2LBFrMH.js +116 -0
  10. package/dist/auth-users-columns-D2LBFrMH.js.map +1 -0
  11. package/dist/backup-argv.d.ts +19 -0
  12. package/dist/backup-cli-oM9WQVlV.js +408 -0
  13. package/dist/backup-cli-oM9WQVlV.js.map +1 -0
  14. package/dist/backup-service-Br3tqwsy.js +715 -0
  15. package/dist/backup-service-Br3tqwsy.js.map +1 -0
  16. package/dist/branch-argv.d.ts +28 -0
  17. package/dist/branch-prune.d.ts +77 -0
  18. package/dist/cli-collections-path.d.ts +31 -0
  19. package/dist/cli-errors-DxN4yzdH.js +657 -0
  20. package/dist/cli-errors-DxN4yzdH.js.map +1 -0
  21. package/dist/cli-errors.d.ts +123 -0
  22. package/dist/cli-flags.d.ts +101 -0
  23. package/dist/cli-helpers.d.ts +94 -10
  24. package/dist/cli-output-CNdMql-L.js +47 -0
  25. package/dist/cli-output-CNdMql-L.js.map +1 -0
  26. package/dist/cli.js +2020 -0
  27. package/dist/cli.js.map +1 -0
  28. package/dist/collection-index-BHgT_TKd.js +276 -0
  29. package/dist/collection-index-BHgT_TKd.js.map +1 -0
  30. package/dist/{collections-schema-version-BMeu3cgv.js → collections-schema-version-T6cGvrDi.js} +1 -2
  31. package/dist/{collections-schema-version-BMeu3cgv.js.map → collections-schema-version-T6cGvrDi.js.map} +1 -1
  32. package/dist/{connection-GOKU3Hu5.js → databasePoolManager-Bj5FbeAs.js} +112 -40
  33. package/dist/databasePoolManager-Bj5FbeAs.js.map +1 -0
  34. package/dist/databasePoolManager.d.ts +27 -0
  35. package/dist/doctor-D7HWmGzs.js +974 -0
  36. package/dist/doctor-D7HWmGzs.js.map +1 -0
  37. package/dist/{ensure-collection-policies-UCqgv_8c.js → ensure-collection-policies-2c2NV8zX.js} +7 -27
  38. package/dist/ensure-collection-policies-2c2NV8zX.js.map +1 -0
  39. package/dist/ensure-collection-tables-D4wJzM4y.js +820 -0
  40. package/dist/ensure-collection-tables-D4wJzM4y.js.map +1 -0
  41. package/dist/ensure-tables-CIojK-Rx.js +650 -0
  42. package/dist/ensure-tables-CIojK-Rx.js.map +1 -0
  43. package/dist/generate-drizzle-schema-CwEgVyjO.js +123 -0
  44. package/dist/generate-drizzle-schema-CwEgVyjO.js.map +1 -0
  45. package/dist/generate-drizzle-schema-logic-so2Z_lyC.js +528 -0
  46. package/dist/generate-drizzle-schema-logic-so2Z_lyC.js.map +1 -0
  47. package/dist/{ensure-collection-tables-DgVixhX3.js → generate-postgres-ddl-logic-CtnpIBVv.js} +126 -984
  48. package/dist/generate-postgres-ddl-logic-CtnpIBVv.js.map +1 -0
  49. package/dist/generated-schema-staleness-DQdfJl2A.js +157 -0
  50. package/dist/generated-schema-staleness-DQdfJl2A.js.map +1 -0
  51. package/dist/index.d.ts +12 -0
  52. package/dist/index.es.js +7423 -1133
  53. package/dist/index.es.js.map +1 -1
  54. package/dist/introspect-db-logic-C6LQdTxj.js +1378 -0
  55. package/dist/introspect-db-logic-C6LQdTxj.js.map +1 -0
  56. package/dist/policy-drift-kNJ6X2Uj.js +313 -0
  57. package/dist/policy-drift-kNJ6X2Uj.js.map +1 -0
  58. package/dist/{backup-service-DCk7KhhL.js → rls-bootstrap-sql-DLDnUjtx.js} +737 -2689
  59. package/dist/rls-bootstrap-sql-DLDnUjtx.js.map +1 -0
  60. package/dist/{rls-enforcement-DvAbL9YJ.js → rls-enforcement-DeYEjYLt.js} +65 -190
  61. package/dist/rls-enforcement-DeYEjYLt.js.map +1 -0
  62. package/dist/schema/bin/generate-drizzle-schema.d.ts +1 -0
  63. package/dist/schema/bin/generate-postgres-ddl.d.ts +1 -0
  64. package/dist/schema/column-type-drift.d.ts +79 -0
  65. package/dist/schema/doctor-cli.js +129 -0
  66. package/dist/schema/doctor-cli.js.map +1 -0
  67. package/dist/schema/doctor.d.ts +42 -0
  68. package/dist/schema/ensure-collection-tables.d.ts +90 -0
  69. package/dist/schema/generate-drizzle-schema.d.ts +1 -1
  70. package/dist/schema/generate-drizzle-schema.js +24 -0
  71. package/dist/schema/generate-drizzle-schema.js.map +1 -0
  72. package/dist/schema/generate-next-step.d.ts +30 -0
  73. package/dist/schema/generate-postgres-ddl-logic.d.ts +28 -0
  74. package/dist/schema/generate-postgres-ddl.d.ts +1 -1
  75. package/dist/schema/generate-postgres-ddl.js +138 -0
  76. package/dist/schema/generate-postgres-ddl.js.map +1 -0
  77. package/dist/schema/generated-schema-staleness.d.ts +37 -0
  78. package/dist/schema/introspect-db-queries.d.ts +34 -0
  79. package/dist/schema/introspect-db.js +866 -0
  80. package/dist/schema/introspect-db.js.map +1 -0
  81. package/dist/security/rls-enforcement.d.ts +63 -2
  82. package/dist/services/BranchService.d.ts +86 -1
  83. package/dist/services/collection-helpers.d.ts +1 -1
  84. package/dist/services/realtimeService.d.ts +30 -2
  85. package/dist/services/row-pipeline.d.ts +12 -1
  86. package/dist/source-Br7L7GOI.js +410 -0
  87. package/dist/source-Br7L7GOI.js.map +1 -0
  88. package/dist/utils/pg-error-utils.d.ts +3 -1
  89. package/dist/utils/pg-numeric-number-patch.d.ts +31 -0
  90. package/dist/vector-index-DkWxm2de.js +309 -0
  91. package/dist/vector-index-DkWxm2de.js.map +1 -0
  92. package/package.json +32 -27
  93. package/dist/backup-service-DCk7KhhL.js.map +0 -1
  94. package/dist/collection-index-BRUg10H5.js +0 -5690
  95. package/dist/collection-index-BRUg10H5.js.map +0 -1
  96. package/dist/connection-GOKU3Hu5.js.map +0 -1
  97. package/dist/ensure-collection-policies-UCqgv_8c.js.map +0 -1
  98. package/dist/ensure-collection-tables-DgVixhX3.js.map +0 -1
  99. package/dist/rls-bootstrap-sql-B5C9LoJ6.js +0 -244
  100. package/dist/rls-bootstrap-sql-B5C9LoJ6.js.map +0 -1
  101. package/dist/rls-enforcement-DvAbL9YJ.js.map +0 -1
  102. package/dist/src-DiB5RP2Z.js +0 -541
  103. package/dist/src-DiB5RP2Z.js.map +0 -1
  104. package/dist/websocket-BZ4H5wUz.js +0 -8198
  105. package/dist/websocket-BZ4H5wUz.js.map +0 -1
  106. package/src/PostgresAdapter.ts +0 -91
  107. package/src/PostgresBackendDriver.ts +0 -1857
  108. package/src/PostgresBootstrapper.ts +0 -1157
  109. package/src/auth/ensure-tables.ts +0 -896
  110. package/src/auth/schema-version.ts +0 -260
  111. package/src/auth/services.ts +0 -1610
  112. package/src/backup/backup-cli.ts +0 -520
  113. package/src/backup/backup-cron.ts +0 -212
  114. package/src/backup/backup-logic.ts +0 -93
  115. package/src/backup/backup-service.ts +0 -485
  116. package/src/backup/index.ts +0 -12
  117. package/src/backup/pg-tools.ts +0 -403
  118. package/src/backup/retention.ts +0 -86
  119. package/src/cli-errors.ts +0 -265
  120. package/src/cli-helpers.ts +0 -569
  121. package/src/cli-output.ts +0 -43
  122. package/src/cli.ts +0 -1339
  123. package/src/collections/PostgresCollectionRegistry.ts +0 -108
  124. package/src/collections/buildRegistry.ts +0 -68
  125. package/src/collections/validate-relations.ts +0 -402
  126. package/src/connection.ts +0 -333
  127. package/src/data-transformer.ts +0 -845
  128. package/src/databasePoolManager.ts +0 -95
  129. package/src/history/HistoryService.ts +0 -254
  130. package/src/history/ensure-history-table.ts +0 -53
  131. package/src/index.ts +0 -19
  132. package/src/interfaces.ts +0 -60
  133. package/src/module-dir.ts +0 -7
  134. package/src/schema/atlas-argv.ts +0 -94
  135. package/src/schema/auth-schema.ts +0 -298
  136. package/src/schema/auth-users-columns.ts +0 -131
  137. package/src/schema/carved-out-migration.ts +0 -404
  138. package/src/schema/classify-change.ts +0 -436
  139. package/src/schema/collection-index.ts +0 -427
  140. package/src/schema/collections-schema-version.ts +0 -103
  141. package/src/schema/destructive-sql.ts +0 -94
  142. package/src/schema/doctor-cli.ts +0 -66
  143. package/src/schema/doctor-policy-checks.ts +0 -105
  144. package/src/schema/doctor.ts +0 -837
  145. package/src/schema/drizzle-ddl.ts +0 -33
  146. package/src/schema/dynamic-tables.test.ts +0 -302
  147. package/src/schema/dynamic-tables.ts +0 -293
  148. package/src/schema/ensure-collection-policies.ts +0 -198
  149. package/src/schema/ensure-collection-tables.test.ts +0 -518
  150. package/src/schema/ensure-collection-tables.ts +0 -1267
  151. package/src/schema/generate-drizzle-schema-logic.ts +0 -957
  152. package/src/schema/generate-drizzle-schema.ts +0 -143
  153. package/src/schema/generate-postgres-ddl-logic.ts +0 -1339
  154. package/src/schema/generate-postgres-ddl.ts +0 -180
  155. package/src/schema/generate-schema-commit.ts +0 -267
  156. package/src/schema/generated-schema-staleness.ts +0 -284
  157. package/src/schema/introspect-db-constraints.ts +0 -385
  158. package/src/schema/introspect-db-inference.ts +0 -255
  159. package/src/schema/introspect-db-logic.ts +0 -1387
  160. package/src/schema/introspect-db-naming.ts +0 -15
  161. package/src/schema/introspect-db-project.ts +0 -78
  162. package/src/schema/introspect-db-queries.ts +0 -326
  163. package/src/schema/introspect-db-structure.ts +0 -670
  164. package/src/schema/introspect-db-types.ts +0 -56
  165. package/src/schema/introspect-db.ts +0 -257
  166. package/src/schema/introspect-runtime.test.ts +0 -260
  167. package/src/schema/introspect-runtime.ts +0 -327
  168. package/src/schema/non-sql-collections.test.ts +0 -131
  169. package/src/schema/rls-bootstrap-sql.ts +0 -288
  170. package/src/schema/search-column.ts +0 -643
  171. package/src/schema/test-schema.ts +0 -11
  172. package/src/schema/vector-index.ts +0 -491
  173. package/src/security/anonymous-grants.test.ts +0 -73
  174. package/src/security/policy-drift.test.ts +0 -435
  175. package/src/security/policy-drift.ts +0 -473
  176. package/src/security/rls-enforcement.ts +0 -505
  177. package/src/services/BranchService.ts +0 -326
  178. package/src/services/FetchService.ts +0 -2115
  179. package/src/services/PersistService.ts +0 -557
  180. package/src/services/RelationService.ts +0 -978
  181. package/src/services/RelationWriteService.ts +0 -653
  182. package/src/services/cdc/CdcListener.ts +0 -103
  183. package/src/services/cdc/junction-tables.ts +0 -91
  184. package/src/services/cdc/trigger-cdc.ts +0 -173
  185. package/src/services/channel-bus/ChannelBus.ts +0 -44
  186. package/src/services/channel-bus/PostgresChannelBus.ts +0 -299
  187. package/src/services/channel-bus/index.ts +0 -114
  188. package/src/services/channel-history.ts +0 -424
  189. package/src/services/channel-presence.ts +0 -185
  190. package/src/services/collection-helpers.ts +0 -420
  191. package/src/services/dataService.ts +0 -256
  192. package/src/services/index.ts +0 -14
  193. package/src/services/junction-writes.ts +0 -295
  194. package/src/services/nested-path.ts +0 -145
  195. package/src/services/pg-notify-listener.ts +0 -151
  196. package/src/services/realtimeService.ts +0 -2545
  197. package/src/services/row-pipeline.ts +0 -240
  198. package/src/services/write-denial.ts +0 -55
  199. package/src/types.ts +0 -4
  200. package/src/utils/connection-string.ts +0 -58
  201. package/src/utils/drizzle-conditions.ts +0 -2743
  202. package/src/utils/pg-array-null-patch.ts +0 -42
  203. package/src/utils/pg-error-utils.ts +0 -391
  204. package/src/websocket.ts +0 -855
@@ -1,326 +0,0 @@
1
- /**
2
- * BranchService
3
- *
4
- * Manages database branching by creating/deleting PostgreSQL databases
5
- * using `CREATE DATABASE ... TEMPLATE`. Branch metadata is stored in the
6
- * `rebase.branches` table in the default (main) database, following the
7
- * same `rebase` schema convention used by entity_history, auth, etc.
8
- */
9
-
10
- import { sql } from "drizzle-orm";
11
- import { BranchInfo } from "@rebasepro/types";
12
- import { revokeInternalTableSql } from "@rebasepro/common";
13
- import { DrizzleClient } from "../interfaces";
14
- import { DatabasePoolManager } from "../databasePoolManager";
15
- import { extractPgError, extractCauseMessage } from "../utils/pg-error-utils";
16
-
17
- /** Internal prefix applied to branch database names to avoid collisions. */
18
- const BRANCH_DB_PREFIX = "rb_";
19
-
20
- /** `duplicate_database` — the target database name is already taken. */
21
- const PG_DUPLICATE_DATABASE = "42P04";
22
-
23
- /** `object_in_use` — the database still has connections attached. */
24
- const PG_OBJECT_IN_USE = "55006";
25
-
26
- /**
27
- * Describe a failed branch DDL statement in terms a user can act on.
28
- *
29
- * Drizzle reports failures as `Failed query: <sql> params:` and hides the real
30
- * PostgreSQL error in the `cause` chain, so matching on `err.message` never sees
31
- * the actual problem. Match on the PG error code instead — it survives wrapping
32
- * and, unlike the message text, is not locale-dependent.
33
- */
34
- function describeBranchDdlError(err: unknown, fallbackContext: string): Error {
35
- const pgError = extractPgError(err);
36
-
37
- if (pgError?.code === PG_DUPLICATE_DATABASE) {
38
- return new Error(`Database "${fallbackContext}" already exists on the server. Choose a different branch name.`);
39
- }
40
- if (pgError?.code === PG_OBJECT_IN_USE) {
41
- return new Error(
42
- `Cannot complete the operation: the database "${fallbackContext}" has active connections. ` +
43
- "Close other clients or connections and try again."
44
- );
45
- }
46
-
47
- // Unknown failure: surface the real PG message rather than the Drizzle
48
- // wrapper, which would otherwise show the raw SQL and no reason at all.
49
- const detail = pgError?.message ?? extractCauseMessage(err);
50
- if (detail) return new Error(detail);
51
- return err instanceof Error ? err : new Error(String(err));
52
- }
53
-
54
- /** Fully-qualified metadata table in the rebase schema. */
55
- const BRANCHES_TABLE = "rebase.branches";
56
-
57
- /**
58
- * Validate that a user-provided identifier only contains safe characters.
59
- * Throws if the value contains characters outside [a-zA-Z0-9_-].
60
- */
61
- function validateIdentifier(value: string, label: string): void {
62
- if (!/^[a-zA-Z0-9_-]+$/.test(value)) {
63
- throw new Error(`Invalid ${label}: only letters, digits, underscores, and hyphens are allowed.`);
64
- }
65
- }
66
-
67
- /**
68
- * Postgres truncates identifiers at NAMEDATALEN-1 = 63 bytes, and does it
69
- * silently. The prefix comes out of the same budget.
70
- */
71
- const MAX_BRANCH_NAME_LENGTH = 63 - BRANCH_DB_PREFIX.length;
72
-
73
- /**
74
- * Check a user-provided branch name, and otherwise leave it exactly as given.
75
- *
76
- * This used to strip everything outside [a-zA-Z0-9_], so `my-feature` was
77
- * quietly created as `myfeature`: the name you typed was not the name `list`
78
- * gave back. Nothing ever needed that. Every identifier this service builds is
79
- * double-quoted (see `CREATE DATABASE` below), which is what makes a hyphen
80
- * safe, and `validateIdentifier` has always accepted hyphens for the `--from`
81
- * source database — the two disagreed about the same character class.
82
- *
83
- * Refusing a name we cannot represent is better than representing a different
84
- * one, which is also why the length is checked here rather than left to
85
- * Postgres, whose answer to an over-long identifier is a silent rename.
86
- */
87
- function assertValidBranchName(name: string): void {
88
- validateIdentifier(name, "branch name");
89
- if (name.length > MAX_BRANCH_NAME_LENGTH) {
90
- throw new Error(
91
- `Branch name "${name}" is too long: ${name.length} characters, maximum ${MAX_BRANCH_NAME_LENGTH}. ` +
92
- "Postgres truncates identifiers past 63 bytes, which would give the branch a name you did not choose."
93
- );
94
- }
95
- }
96
-
97
- /**
98
- * Convert a user-facing branch name to the actual PostgreSQL database name.
99
- */
100
- function toBranchDbName(name: string): string {
101
- assertValidBranchName(name);
102
- return `${BRANCH_DB_PREFIX}${name}`;
103
- }
104
-
105
- export class BranchService {
106
- constructor(
107
- private db: DrizzleClient,
108
- private poolManager: DatabasePoolManager
109
- ) {}
110
-
111
- /**
112
- * Ensure the `rebase.branches` metadata table exists in the default database.
113
- * Idempotent — safe to call on every startup.
114
- */
115
- async ensureBranchMetadataTable(): Promise<void> {
116
- // Create the rebase schema (idempotent — may already exist from auth/history init)
117
- await this.db.execute(sql`CREATE SCHEMA IF NOT EXISTS rebase`);
118
-
119
- await this.db.execute(sql.raw(`
120
- CREATE TABLE IF NOT EXISTS ${BRANCHES_TABLE} (
121
- name TEXT PRIMARY KEY,
122
- db_name TEXT NOT NULL UNIQUE,
123
- parent_db TEXT NOT NULL,
124
- created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
125
- metadata JSONB DEFAULT '{}'
126
- );
127
- `));
128
-
129
- // Not a collection, so no RLS — and it names every branch database on
130
- // this server. The driver's schema-wide grant reaches it, so revoke.
131
- await this.db.execute(sql.raw(revokeInternalTableSql("rebase", "branches")));
132
- }
133
-
134
- /**
135
- * Create a new branch database by templating the source database.
136
- *
137
- * Uses `CREATE DATABASE ... TEMPLATE` for an instant, full-fidelity copy
138
- * of both schema and data.
139
- *
140
- * @param name User-facing branch name (e.g., "feature_auth")
141
- * @param options.source Source database to clone; defaults to the main database.
142
- */
143
- async createBranch(name: string, options?: { source?: string }): Promise<BranchInfo> {
144
- if (options?.source) {
145
- validateIdentifier(options.source, "source database name");
146
- }
147
-
148
- const dbName = toBranchDbName(name);
149
- const sourceDb = options?.source || this.poolManager.defaultDatabaseName;
150
-
151
- // Check if branch already exists
152
- const existing = await this.db.execute(
153
- sql`SELECT name FROM rebase.branches WHERE name = ${name} OR db_name = ${dbName}`
154
- );
155
- if ((existing.rows as unknown[]).length > 0) {
156
- throw new Error(`Branch "${name}" already exists.`);
157
- }
158
-
159
- // Disconnect any idle pools to the source DB so TEMPLATE works.
160
- // CREATE DATABASE ... TEMPLATE requires no other connections to the template.
161
- await this.poolManager.disconnectDatabase(sourceDb);
162
-
163
- // Create the database using the source as a template.
164
- // Note: Identifiers must be double-quoted, not parameterized.
165
- const safeDbName = dbName.replace(/"/g, '""');
166
- const safeSourceDb = sourceDb.replace(/"/g, '""');
167
- try {
168
- await this.db.execute(
169
- sql.raw(`CREATE DATABASE "${safeDbName}" TEMPLATE "${safeSourceDb}"`)
170
- );
171
- } catch (err) {
172
- const pgError = extractPgError(err);
173
- if (pgError?.code === PG_OBJECT_IN_USE) {
174
- // The template — not the new database — is the one still in use.
175
- throw new Error(
176
- `Cannot create branch: the source database "${sourceDb}" has active connections. ` +
177
- "Close other clients or connections and try again."
178
- );
179
- }
180
- throw describeBranchDdlError(err, dbName);
181
- }
182
-
183
- // Record metadata in the default database
184
- const now = new Date();
185
- await this.db.execute(
186
- sql`INSERT INTO rebase.branches (name, db_name, parent_db, created_at)
187
- VALUES (${name}, ${dbName}, ${sourceDb}, ${now.toISOString()})`
188
- );
189
-
190
- return {
191
- name,
192
- parentDatabase: sourceDb,
193
- createdAt: now
194
- };
195
- }
196
-
197
- /**
198
- * Delete a branch database and remove its metadata.
199
- * Cannot delete the main/default database.
200
- */
201
- async deleteBranch(name: string): Promise<void> {
202
- assertValidBranchName(name);
203
-
204
- // Safety, first pass: a request that would target the default database
205
- // is refused before any metadata is read, so the answer costs nothing
206
- // and cannot depend on what a row happens to say.
207
- if (toBranchDbName(name) === this.poolManager.defaultDatabaseName) {
208
- throw new Error("Cannot delete the main database.");
209
- }
210
-
211
- // Verify the branch exists, and take the database name from the row
212
- // rather than deriving it again. A branch created before names stopped
213
- // being stripped is recorded as `rb_myfeature` while `my-feature` now
214
- // derives `rb_my-feature`; re-deriving would drop a database this row
215
- // never named — or, if that name happened to exist, somebody else's.
216
- // The stored value is the only one that is true by construction.
217
- const existing = await this.db.execute(
218
- sql`SELECT db_name FROM rebase.branches WHERE name = ${name}`
219
- );
220
- const existingRows = existing.rows as Record<string, unknown>[];
221
- if (existingRows.length === 0) {
222
- throw new Error(`Branch "${name}" not found.`);
223
- }
224
- const dbName = existingRows[0].db_name as string;
225
-
226
- // Safety, second pass: the first checked a name we derived, this checks
227
- // the one we are about to drop. They differ for any row written under
228
- // the old scheme, and it is this one that governs.
229
- if (dbName === this.poolManager.defaultDatabaseName) {
230
- throw new Error("Cannot delete the main database.");
231
- }
232
-
233
- // Disconnect any pools to this branch before dropping
234
- await this.poolManager.disconnectDatabase(dbName);
235
-
236
- // Drop the database
237
- const safeDbName = dbName.replace(/"/g, '""');
238
- try {
239
- await this.db.execute(sql.raw(`DROP DATABASE "${safeDbName}"`));
240
- } catch (err) {
241
- const pgError = extractPgError(err);
242
- if (pgError?.code === PG_OBJECT_IN_USE) {
243
- throw new Error(
244
- `Cannot delete branch "${name}": the database has active connections. ` +
245
- "Close other clients and try again."
246
- );
247
- }
248
- throw describeBranchDdlError(err, dbName);
249
- }
250
-
251
- // Remove metadata
252
- await this.db.execute(
253
- sql`DELETE FROM rebase.branches WHERE name = ${name}`
254
- );
255
- }
256
-
257
- /**
258
- * List all branches recorded in the metadata table.
259
- * Optionally fetches database sizes from pg_database.
260
- */
261
- async listBranches(): Promise<BranchInfo[]> {
262
- const result = await this.db.execute(sql.raw(`
263
- SELECT
264
- b.name,
265
- b.parent_db,
266
- b.created_at,
267
- pg_database_size(b.db_name) as size_bytes
268
- FROM ${BRANCHES_TABLE} b
269
- JOIN pg_database d ON d.datname = b.db_name
270
- ORDER BY b.created_at DESC
271
- `));
272
-
273
- return (result.rows as Record<string, unknown>[]).map((row) => ({
274
- name: row.name as string,
275
- parentDatabase: row.parent_db as string,
276
- createdAt: new Date(row.created_at as string),
277
- sizeBytes: row.size_bytes != null ? Number(row.size_bytes) : undefined
278
- }));
279
- }
280
-
281
- /**
282
- * Get info about a specific branch.
283
- */
284
- async getBranchInfo(name: string): Promise<BranchInfo | undefined> {
285
- assertValidBranchName(name);
286
-
287
- const result = await this.db.execute(sql`
288
- SELECT
289
- b.name,
290
- b.db_name,
291
- b.parent_db,
292
- b.created_at
293
- FROM rebase.branches b
294
- WHERE b.name = ${name}
295
- `);
296
-
297
- const rows = result.rows as Record<string, unknown>[];
298
- if (rows.length === 0) return undefined;
299
-
300
- const row = rows[0];
301
-
302
- // Attempt to get size — may fail if the DB was externally dropped.
303
- // Same reason as `deleteBranch`: the size of a re-derived name is the
304
- // size of some other database, or of nothing.
305
- let sizeBytes: number | undefined;
306
- try {
307
- const dbName = row.db_name as string;
308
- const sizeResult = await this.db.execute(
309
- sql`SELECT pg_database_size(${dbName}) as size_bytes`
310
- );
311
- const sizeRows = sizeResult.rows as Record<string, unknown>[];
312
- if (sizeRows.length > 0 && sizeRows[0].size_bytes != null) {
313
- sizeBytes = Number(sizeRows[0].size_bytes);
314
- }
315
- } catch {
316
- // Database might not exist anymore
317
- }
318
-
319
- return {
320
- name: row.name as string,
321
- parentDatabase: row.parent_db as string,
322
- createdAt: new Date(row.created_at as string),
323
- sizeBytes
324
- };
325
- }
326
- }