@rebasepro/server-postgres 0.24.1-canary.g56af4a4 → 0.25.0-canary.g70dad01
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.
- package/README.md +16 -0
- package/dist/{BranchService-Bsn2wfaj.js → BranchService-DO_3_CXt.js} +118 -18
- package/dist/BranchService-DO_3_CXt.js.map +1 -0
- package/dist/PostgresBackendDriver.d.ts +26 -5
- package/dist/PostgresBootstrapper.d.ts +111 -2
- package/dist/backup/backup-cli.d.ts +120 -2
- package/dist/backup/backup-service.d.ts +74 -1
- package/dist/backup/pg-binary.d.ts +2 -2
- package/dist/backup/pg-tools.d.ts +65 -0
- package/dist/backup-cli-Ap2WX2uW.js +647 -0
- package/dist/backup-cli-Ap2WX2uW.js.map +1 -0
- package/dist/{backup-service-BTBB7RXn.js → backup-service-DytMimsL.js} +414 -24
- package/dist/backup-service-DytMimsL.js.map +1 -0
- package/dist/{boot-ddl-DVpsFN6r.js → boot-ddl-D1lMCZLd.js} +3 -3
- package/dist/{boot-ddl-DVpsFN6r.js.map → boot-ddl-D1lMCZLd.js.map} +1 -1
- package/dist/branch-argv.d.ts +18 -6
- package/dist/branch-prune.d.ts +18 -0
- package/dist/cli-console-CQGsBzSY.js +262 -0
- package/dist/cli-console-CQGsBzSY.js.map +1 -0
- package/dist/cli-console.d.ts +50 -0
- package/dist/{cli-errors-CaI4ntyD.js → cli-errors-CJTKAKax.js} +499 -107
- package/dist/cli-errors-CJTKAKax.js.map +1 -0
- package/dist/cli-errors.d.ts +42 -5
- package/dist/{cli-flags-B-Dawocl.js → cli-flags-D-b0qL9T.js} +71 -6
- package/dist/cli-flags-D-b0qL9T.js.map +1 -0
- package/dist/cli-flags.d.ts +21 -0
- package/dist/cli-helpers.d.ts +53 -24
- package/dist/{arg-Dni7MzLB.js → cli-prompt-egtNKkZl.js} +49 -3
- package/dist/cli-prompt-egtNKkZl.js.map +1 -0
- package/dist/cli-prompt.d.ts +19 -0
- package/dist/cli-scratch-database.d.ts +47 -8
- package/dist/cli.js +618 -100
- package/dist/cli.js.map +1 -1
- package/dist/collections/validate-relations.d.ts +8 -2
- package/dist/{column-plan-helpers-drZIsSNf.js → column-plan-helpers-D_V4oaZh.js} +2 -2
- package/dist/{column-plan-helpers-drZIsSNf.js.map → column-plan-helpers-D_V4oaZh.js.map} +1 -1
- package/dist/{connection-BPoNILnD.js → connection-C938nR3E.js} +7 -4
- package/dist/{connection-BPoNILnD.js.map → connection-C938nR3E.js.map} +1 -1
- package/dist/connection.d.ts +4 -1
- package/dist/{databasePoolManager-BkEy2w18.js → databasePoolManager-Yqdw14wY.js} +4 -4
- package/dist/{databasePoolManager-BkEy2w18.js.map → databasePoolManager-Yqdw14wY.js.map} +1 -1
- package/dist/{deferred-index-build-DP1_SNFo.js → deferred-index-build-BiTqxqdl.js} +8 -8
- package/dist/{deferred-index-build-DP1_SNFo.js.map → deferred-index-build-BiTqxqdl.js.map} +1 -1
- package/dist/{doctor-C4WiftHm.js → doctor-N3J88BN_.js} +8 -8
- package/dist/doctor-N3J88BN_.js.map +1 -0
- package/dist/{ensure-collection-policies-CXpbeNag.js → ensure-collection-policies-CVP6kIPT.js} +125 -18
- package/dist/ensure-collection-policies-CVP6kIPT.js.map +1 -0
- package/dist/{ensure-collection-tables-BhsU6qUE.js → ensure-collection-tables-1WVNALOW.js} +10 -10
- package/dist/{ensure-collection-tables-BhsU6qUE.js.map → ensure-collection-tables-1WVNALOW.js.map} +1 -1
- package/dist/{ensure-tables-CHsgyLqg.js → ensure-tables-D-Nm2k3S.js} +6 -6
- package/dist/{ensure-tables-CHsgyLqg.js.map → ensure-tables-D-Nm2k3S.js.map} +1 -1
- package/dist/{enum-labels-kept-D8fI1MoX.js → enum-labels-kept-e_93v2Ic.js} +3 -3
- package/dist/{enum-labels-kept-D8fI1MoX.js.map → enum-labels-kept-e_93v2Ic.js.map} +1 -1
- package/dist/{generate-drizzle-schema-Bz52jH2h.js → generate-drizzle-schema-C3EAP9HE.js} +3 -3
- package/dist/{generate-drizzle-schema-Bz52jH2h.js.map → generate-drizzle-schema-C3EAP9HE.js.map} +1 -1
- package/dist/{generate-drizzle-schema-logic-B74WN37S.js → generate-drizzle-schema-logic-DA3zM8zc.js} +4 -4
- package/dist/{generate-drizzle-schema-logic-B74WN37S.js.map → generate-drizzle-schema-logic-DA3zM8zc.js.map} +1 -1
- package/dist/{generate-files-CdEckX6c.js → generate-files-ClmOUHXj.js} +6 -47
- package/dist/generate-files-ClmOUHXj.js.map +1 -0
- package/dist/generate-next-step-DCeS4LrJ.js +74 -0
- package/dist/generate-next-step-DCeS4LrJ.js.map +1 -0
- package/dist/{generate-postgres-ddl-logic-B64O2oZF.js → generate-postgres-ddl-logic-4eUP4t8w.js} +4 -4
- package/dist/{generate-postgres-ddl-logic-B64O2oZF.js.map → generate-postgres-ddl-logic-4eUP4t8w.js.map} +1 -1
- package/dist/{generated-schema-staleness-Di9c4ZxN.js → generated-schema-staleness-FlxM2i4i.js} +102 -12
- package/dist/{generated-schema-staleness-Di9c4ZxN.js.map → generated-schema-staleness-FlxM2i4i.js.map} +1 -1
- package/dist/history/HistoryService.d.ts +28 -1
- package/dist/history/ensure-history-table.d.ts +21 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.es.js +139 -14683
- package/dist/index.es.js.map +1 -1
- package/dist/{introspect-db-logic-C-Di8dA9.js → introspect-db-logic-CI5CU_n0.js} +145 -9
- package/dist/introspect-db-logic-CI5CU_n0.js.map +1 -0
- package/dist/{introspect-db-queries-D6kAqReI.js → introspect-db-queries-DevUrzll.js} +41 -6
- package/dist/introspect-db-queries-DevUrzll.js.map +1 -0
- package/dist/open-rebase-Cwnqo5LU.js +20923 -0
- package/dist/open-rebase-Cwnqo5LU.js.map +1 -0
- package/dist/open-rebase.d.ts +94 -0
- package/dist/{pg-error-utils-vJezlXdI.js → pg-error-utils-Cmo8wZPy.js} +19 -5
- package/dist/pg-error-utils-Cmo8wZPy.js.map +1 -0
- package/dist/{plan-schema-B1YF_tYq.js → plan-schema-CXOucCw-.js} +4 -4
- package/dist/{plan-schema-B1YF_tYq.js.map → plan-schema-CXOucCw-.js.map} +1 -1
- package/dist/{policy-drift-Dm8i-s1J.js → policy-drift-vm0BcvcE.js} +28 -14
- package/dist/{policy-drift-Dm8i-s1J.js.map → policy-drift-vm0BcvcE.js.map} +1 -1
- package/dist/{push-index-prebuild-Db4AQF4i.js → push-index-prebuild-SXRlWBjc.js} +4 -4
- package/dist/{push-index-prebuild-Db4AQF4i.js.map → push-index-prebuild-SXRlWBjc.js.map} +1 -1
- package/dist/{push-legacy-renames-BK6ilQq2.js → push-legacy-renames-B37yKDac.js} +4 -4
- package/dist/{push-legacy-renames-BK6ilQq2.js.map → push-legacy-renames-B37yKDac.js.map} +1 -1
- package/dist/push-policy-plan-jXzHAWSu.js +127 -0
- package/dist/push-policy-plan-jXzHAWSu.js.map +1 -0
- package/dist/{render-ddl-CtC3y9Ol.js → render-ddl-CQNwZdse.js} +10 -9
- package/dist/render-ddl-CQNwZdse.js.map +1 -0
- package/dist/{rls-bootstrap-sql-RQUljnJ1.js → rls-bootstrap-sql-Bd-hJk08.js} +3 -58
- package/dist/rls-bootstrap-sql-Bd-hJk08.js.map +1 -0
- package/dist/{rls-enforcement-DvzqlaL1.js → rls-enforcement-D7P4-ZYX.js} +2 -2
- package/dist/{rls-enforcement-DvzqlaL1.js.map → rls-enforcement-D7P4-ZYX.js.map} +1 -1
- package/dist/{__vite-browser-external-BnuHet1e.js → rolldown-runtime-V7rFbMRe.js} +1 -8
- package/dist/schema/atlas-apply-report.d.ts +23 -0
- package/dist/schema/atlas-argv.d.ts +24 -1
- package/dist/schema/atlas-exclude.d.ts +42 -0
- package/dist/schema/atlas-revisions.d.ts +4 -0
- package/dist/schema/carved-out-migration.d.ts +5 -4
- package/dist/schema/destructive-sql.d.ts +23 -1
- package/dist/schema/doctor-cli.js +5 -5
- package/dist/schema/ensure-collection-policies.d.ts +74 -0
- package/dist/schema/generate-drizzle-schema.js +1 -1
- package/dist/schema/generate-next-step.d.ts +23 -0
- package/dist/schema/generate-postgres-ddl.js +2 -2
- package/dist/schema/generated-schema-diff.d.ts +7 -0
- package/dist/schema/generated-schema-staleness.d.ts +7 -1
- package/dist/schema/introspect-db-logic.d.ts +32 -1
- package/dist/schema/introspect-db-queries.d.ts +18 -5
- package/dist/schema/introspect-db.js +55 -53
- package/dist/schema/introspect-db.js.map +1 -1
- package/dist/schema/migrate-baseline.d.ts +51 -0
- package/dist/schema/plan/render-ddl.d.ts +2 -1
- package/dist/schema/push-policy-plan.d.ts +70 -0
- package/dist/schema/undeclarable-constraints.d.ts +59 -0
- package/dist/security/policy-drift.d.ts +5 -0
- package/dist/services/BranchService.d.ts +38 -0
- package/dist/services/channel-bus/index.d.ts +1 -1
- package/dist/{source-DBDbBTL4.js → source-Bv5KPeIx.js} +109 -4
- package/dist/source-Bv5KPeIx.js.map +1 -0
- package/dist/utils/connection-string.d.ts +47 -0
- package/dist/{vector-index-CMJKb3C9.js → vector-index-BFhyEaiz.js} +2 -2
- package/dist/{vector-index-CMJKb3C9.js.map → vector-index-BFhyEaiz.js.map} +1 -1
- package/dist/websocket-BT85Yv_G.js +1632 -0
- package/dist/websocket-BT85Yv_G.js.map +1 -0
- package/package.json +7 -7
- package/dist/BranchService-Bsn2wfaj.js.map +0 -1
- package/dist/__vite-browser-external-BnuHet1e.js.map +0 -1
- package/dist/arg-Dni7MzLB.js.map +0 -1
- package/dist/backup-cli-aUfRv16h.js +0 -420
- package/dist/backup-cli-aUfRv16h.js.map +0 -1
- package/dist/backup-service-BTBB7RXn.js.map +0 -1
- package/dist/cli-errors-CaI4ntyD.js.map +0 -1
- package/dist/cli-flags-B-Dawocl.js.map +0 -1
- package/dist/doctor-C4WiftHm.js.map +0 -1
- package/dist/ensure-collection-policies-CXpbeNag.js.map +0 -1
- package/dist/generate-files-CdEckX6c.js.map +0 -1
- package/dist/introspect-db-logic-C-Di8dA9.js.map +0 -1
- package/dist/introspect-db-queries-D6kAqReI.js.map +0 -1
- package/dist/pg-error-utils-vJezlXdI.js.map +0 -1
- package/dist/render-ddl-CtC3y9Ol.js.map +0 -1
- package/dist/rls-bootstrap-sql-RQUljnJ1.js.map +0 -1
- package/dist/socket-liveness-D3UD5_BW.js +0 -7570
- package/dist/socket-liveness-D3UD5_BW.js.map +0 -1
- package/dist/source-DBDbBTL4.js.map +0 -1
package/README.md
CHANGED
|
@@ -50,6 +50,9 @@ Implements the Rebase `DatabaseAdapter` / `BackendBootstrapper` interfaces for P
|
|
|
50
50
|
|--------|-------------|
|
|
51
51
|
| `createPostgresAdapter(config)` | Creates a `DatabaseAdapter` for use with `initializeRebaseBackend({ database: ... })`. Recommended API. |
|
|
52
52
|
| `createPostgresBootstrapper(config)` | Lower-level `BackendBootstrapper` factory. Used internally by the adapter. |
|
|
53
|
+
| `openPostgresDriver(pgConfig, init)` | Opens the data driver from explicit settings, reading no environment variable; `provision: false` opens it with no DDL. What the adapter runs at boot. New in 0.25. |
|
|
54
|
+
| `postgresDriverConfigFromEnv(config, env?)` | Reads `DATABASE_READ_URL`, `DATABASE_DIRECT_URL` and the `REALTIME_*` variables into a driver config, as the server's boot does. New in 0.25. |
|
|
55
|
+
| `openRebase({ databaseUrl, collections })` | A project's data from a script, a test or a REPL: `dataAsAdmin`, `sql`, `withAuth(user)` and `close()`. No server, no DDL, no environment reads. `rebase console` is built on it. New in 0.25. |
|
|
53
56
|
| `createPostgresDatabaseConnection(url, schema?, poolConfig?)` | Creates a production-grade pooled Drizzle connection. Returns `{ db, pool, connectionString }`. |
|
|
54
57
|
| `createDirectDatabaseConnection(url, schema?, poolConfig?)` | Non-pooled connection for LISTEN/NOTIFY and advisory locks (bypasses PgBouncer). |
|
|
55
58
|
| `createReadReplicaConnection(url, schema?, poolConfig?)` | Read-only connection for routing reads to replicas. |
|
|
@@ -98,6 +101,19 @@ process.on("SIGTERM", async () => {
|
|
|
98
101
|
});
|
|
99
102
|
```
|
|
100
103
|
|
|
104
|
+
## From a script
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
import { openRebase } from "@rebasepro/server-postgres";
|
|
108
|
+
|
|
109
|
+
const rebase = await openRebase({ databaseUrl: process.env.DATABASE_URL!, collections });
|
|
110
|
+
const { data } = await rebase.dataAsAdmin.posts.find({ limit: 10 });
|
|
111
|
+
const ada = await rebase.withAuth({ uid: "ada", roles: ["user"] }); // what Ada's RLS lets her see
|
|
112
|
+
await rebase.close();
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
See [Scripts and the Console](https://rebase.pro/docs/backend/scripts).
|
|
116
|
+
|
|
101
117
|
## Connection Pool Defaults
|
|
102
118
|
|
|
103
119
|
| Option | Default |
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { createRequire as __createRequire } from "module";
|
|
2
2
|
__createRequire(import.meta.url);
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { n as extractPgError } from "./pg-error-utils-
|
|
3
|
+
import { n as __exportAll } from "./rolldown-runtime-V7rFbMRe.js";
|
|
4
|
+
import { tt as extractCauseMessage } from "./cli-errors-CJTKAKax.js";
|
|
5
|
+
import { n as extractPgError } from "./pg-error-utils-Cmo8wZPy.js";
|
|
6
6
|
import { sql } from "drizzle-orm";
|
|
7
7
|
import { revokeInternalTableSql } from "@rebasepro/common";
|
|
8
8
|
//#region src/services/BranchService.ts
|
|
@@ -24,6 +24,10 @@ var BRANCH_DB_PREFIX = "rb_";
|
|
|
24
24
|
var PG_DUPLICATE_DATABASE = "42P04";
|
|
25
25
|
/** `object_in_use` — the database still has connections attached. */
|
|
26
26
|
var PG_OBJECT_IN_USE = "55006";
|
|
27
|
+
/** How many times `--force` disconnects the other sessions before it reports one still there. */
|
|
28
|
+
var FORCE_ATTEMPTS = 3;
|
|
29
|
+
/** The database every Postgres server has, from which another one can be closed and opened. */
|
|
30
|
+
var MAINTENANCE_DATABASE = "postgres";
|
|
27
31
|
/**
|
|
28
32
|
* Describe a failed branch DDL statement in terms a user can act on.
|
|
29
33
|
*
|
|
@@ -138,6 +142,16 @@ var BranchService = class {
|
|
|
138
142
|
if (await this.poolManager.servesOneDatabase()) throw new BranchingUnsupportedError(BRANCHING_UNSUPPORTED_MESSAGE);
|
|
139
143
|
}
|
|
140
144
|
/**
|
|
145
|
+
* Whether the `rebase.branches` registry exists. A database that has never
|
|
146
|
+
* had a branch made on it may not have it, and reading it there is an
|
|
147
|
+
* empty answer, not an error — nor a reason to create it. The CLI's `list`
|
|
148
|
+
* and `info` ask this rather than running the DDL that creates it.
|
|
149
|
+
*/
|
|
150
|
+
async hasBranchRegistry() {
|
|
151
|
+
const [row] = (await (await this.home.registry()).execute(sql`SELECT to_regclass('rebase.branches') IS NOT NULL AS present`)).rows;
|
|
152
|
+
return row !== void 0 && Reflect.get(row, "present") === true;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
141
155
|
* Ensure the `rebase.branches` metadata table exists in the default database.
|
|
142
156
|
* Idempotent — safe to call on every startup.
|
|
143
157
|
*/
|
|
@@ -171,15 +185,16 @@ var BranchService = class {
|
|
|
171
185
|
const sourceDb = options?.source || await this.home.database();
|
|
172
186
|
if ((await (await this.home.registry()).execute(sql`SELECT name FROM rebase.branches WHERE name = ${name} OR db_name = ${dbName}`)).rows.length > 0) throw new Error(`Branch "${name}" already exists.`);
|
|
173
187
|
await this.poolManager.disconnectDatabase(sourceDb);
|
|
174
|
-
if (options?.force) {
|
|
175
|
-
if (await this.terminateConnections(sourceDb) > 0) await new Promise((resolve) => setTimeout(resolve, 250));
|
|
176
|
-
}
|
|
177
188
|
const safeDbName = dbName.replace(/"/g, "\"\"");
|
|
178
189
|
const safeSourceDb = sourceDb.replace(/"/g, "\"\"");
|
|
179
|
-
|
|
190
|
+
const copy = async () => {
|
|
180
191
|
await this.db.execute(sql.raw(`CREATE DATABASE "${safeDbName}" TEMPLATE "${safeSourceDb}"`));
|
|
192
|
+
};
|
|
193
|
+
try {
|
|
194
|
+
if (options?.force) await this.whileClosedToNewConnections(sourceDb, () => this.disconnectingOthers(sourceDb, copy));
|
|
195
|
+
else await copy();
|
|
181
196
|
} catch (err) {
|
|
182
|
-
if (extractPgError(err)?.code === PG_OBJECT_IN_USE) throw new Error(`Cannot create branch: the source database "${sourceDb}" has active connections.\n` + await this.describeBlockingConnections(sourceDb));
|
|
197
|
+
if (extractPgError(err)?.code === PG_OBJECT_IN_USE) throw new Error(`Cannot create branch: the source database "${sourceDb}" ${options?.force ? "still had connections after they were disconnected" : "has active connections"}.\n` + await this.describeBlockingConnections(sourceDb, { forced: options?.force === true }));
|
|
183
198
|
throw describeBranchDdlError(err, dbName);
|
|
184
199
|
}
|
|
185
200
|
const now = /* @__PURE__ */ new Date();
|
|
@@ -206,14 +221,15 @@ var BranchService = class {
|
|
|
206
221
|
const dbName = existingRows[0].db_name;
|
|
207
222
|
if (dbName === mainDatabase) throw new Error("Cannot delete the main database.");
|
|
208
223
|
await this.poolManager.disconnectDatabase(dbName);
|
|
209
|
-
if (options?.force) {
|
|
210
|
-
if (await this.terminateConnections(dbName) > 0) await new Promise((resolve) => setTimeout(resolve, 250));
|
|
211
|
-
}
|
|
212
224
|
const safeDbName = dbName.replace(/"/g, "\"\"");
|
|
213
|
-
|
|
225
|
+
const drop = async () => {
|
|
214
226
|
await this.db.execute(sql.raw(`DROP DATABASE "${safeDbName}"`));
|
|
227
|
+
};
|
|
228
|
+
try {
|
|
229
|
+
if (options?.force) await this.disconnectingOthers(dbName, drop);
|
|
230
|
+
else await drop();
|
|
215
231
|
} catch (err) {
|
|
216
|
-
if (extractPgError(err)?.code === PG_OBJECT_IN_USE) throw new Error(`Cannot delete branch "${name}": the database has active connections.\n` + await this.describeBlockingConnections(dbName));
|
|
232
|
+
if (extractPgError(err)?.code === PG_OBJECT_IN_USE) throw new Error(`Cannot delete branch "${name}": the database ${options?.force ? "still had connections after they were disconnected" : "has active connections"}.\n` + await this.describeBlockingConnections(dbName, { forced: options?.force === true }));
|
|
217
233
|
throw describeBranchDdlError(err, dbName);
|
|
218
234
|
}
|
|
219
235
|
await (await this.home.registry()).execute(sql`DELETE FROM rebase.branches WHERE name = ${name}`);
|
|
@@ -237,7 +253,7 @@ var BranchService = class {
|
|
|
237
253
|
* Best-effort by construction: this runs while reporting a failure, and a
|
|
238
254
|
* diagnostic that throws would replace a real error with its own.
|
|
239
255
|
*/
|
|
240
|
-
async describeBlockingConnections(dbName) {
|
|
256
|
+
async describeBlockingConnections(dbName, options = {}) {
|
|
241
257
|
try {
|
|
242
258
|
const rows = (await this.db.execute(sql`
|
|
243
259
|
SELECT coalesce(nullif(application_name, ''), '(unnamed)') AS app,
|
|
@@ -249,10 +265,94 @@ var BranchService = class {
|
|
|
249
265
|
ORDER BY 2 DESC, 1
|
|
250
266
|
`)).rows;
|
|
251
267
|
if (rows.length === 0) return " Retry — whatever was connected has since disconnected.";
|
|
252
|
-
return ` Connected right now:\n${rows.map((row) => ` ${row.n} × ${String(row.app)}`).join("\n")}\n A running
|
|
253
|
-
disconnect them for you.`;
|
|
268
|
+
return ` Connected right now:\n${rows.map((row) => ` ${row.n} × ${String(row.app)}`).join("\n")}\n` + (options.forced ? " They reconnected as fast as they were disconnected. A running `rebase dev` is the\n usual one — stop it, then run this again." : " A running `rebase dev` is the usual one — stop it, or re-run with --force to\n disconnect them for you.");
|
|
254
269
|
} catch {
|
|
255
|
-
return " Close other clients and connections, or re-run with --force.";
|
|
270
|
+
return options.forced ? " Stop the clients connected to it — a running `rebase dev` is the usual one — then run this again." : " Close other clients and connections, or re-run with --force.";
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Run `operation` with no other session on `dbName`, disconnecting them
|
|
275
|
+
* first: what `--force` asks for.
|
|
276
|
+
*
|
|
277
|
+
* Disconnected, a client that reconnects — a running `rebase dev`'s
|
|
278
|
+
* realtime listener does — can be back before `CREATE DATABASE … TEMPLATE`
|
|
279
|
+
* or `DROP DATABASE` counts the sessions, and it failed on one about once
|
|
280
|
+
* in seven tries, saying to pass the `--force` that had been passed. So a
|
|
281
|
+
* refusal on a session still connected is retried, disconnecting again, a
|
|
282
|
+
* few times before it is reported.
|
|
283
|
+
*/
|
|
284
|
+
async disconnectingOthers(dbName, operation) {
|
|
285
|
+
for (let attempt = 1;; attempt++) {
|
|
286
|
+
if (await this.terminateConnections(dbName) > 0) await new Promise((resolve) => setTimeout(resolve, 250));
|
|
287
|
+
try {
|
|
288
|
+
await operation();
|
|
289
|
+
return;
|
|
290
|
+
} catch (err) {
|
|
291
|
+
if (attempt >= FORCE_ATTEMPTS || extractPgError(err)?.code !== PG_OBJECT_IN_USE) throw err;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Run `operation` with `dbName` refusing new connections, and open it
|
|
297
|
+
* again after, whatever happens.
|
|
298
|
+
*
|
|
299
|
+
* A client disconnected from the source — a running `rebase dev` holds a
|
|
300
|
+
* pool and a realtime listener — cannot then reconnect before the copy has
|
|
301
|
+
* counted the sessions on it. Postgres refuses `ALLOW_CONNECTIONS false` on
|
|
302
|
+
* the database a session is connected to, so both statements run from the
|
|
303
|
+
* server's `postgres` database, and they need the database's owner. Where
|
|
304
|
+
* either is missing, the retries of {@link disconnectingOthers} are what is
|
|
305
|
+
* left. A database that already refused connections is left as it was.
|
|
306
|
+
*
|
|
307
|
+
* Interrupted (Ctrl-C) while closed, it opens the database again before
|
|
308
|
+
* the process exits; one it could not open is named in the error, with the
|
|
309
|
+
* statement that does.
|
|
310
|
+
*/
|
|
311
|
+
async whileClosedToNewConnections(dbName, operation) {
|
|
312
|
+
const safe = dbName.replace(/"/g, "\"\"");
|
|
313
|
+
const maintenance = dbName === MAINTENANCE_DATABASE ? null : this.poolManager.getDrizzle(MAINTENANCE_DATABASE);
|
|
314
|
+
let closed = false;
|
|
315
|
+
try {
|
|
316
|
+
if (maintenance) {
|
|
317
|
+
const [row] = (await maintenance.execute(sql`SELECT datallowconn FROM pg_database WHERE datname = ${dbName}`)).rows;
|
|
318
|
+
if (row?.datallowconn === true) {
|
|
319
|
+
await maintenance.execute(sql.raw(`ALTER DATABASE "${safe}" ALLOW_CONNECTIONS false`));
|
|
320
|
+
closed = true;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
} catch {}
|
|
324
|
+
if (!closed) {
|
|
325
|
+
await this.poolManager.disconnectDatabase(MAINTENANCE_DATABASE);
|
|
326
|
+
await operation();
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
let reopening;
|
|
330
|
+
const reopen = () => {
|
|
331
|
+
reopening ??= this.reopenToConnections(dbName);
|
|
332
|
+
return reopening;
|
|
333
|
+
};
|
|
334
|
+
const onSignal = (signal) => {
|
|
335
|
+
reopen().finally(() => process.kill(process.pid, signal));
|
|
336
|
+
};
|
|
337
|
+
process.once("SIGINT", onSignal);
|
|
338
|
+
process.once("SIGTERM", onSignal);
|
|
339
|
+
try {
|
|
340
|
+
await operation();
|
|
341
|
+
} finally {
|
|
342
|
+
process.removeListener("SIGINT", onSignal);
|
|
343
|
+
process.removeListener("SIGTERM", onSignal);
|
|
344
|
+
await reopen();
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
/** `ALLOW_CONNECTIONS true` again, from the maintenance database, or an error that says how to do it by hand. */
|
|
348
|
+
async reopenToConnections(dbName) {
|
|
349
|
+
const statement = `ALTER DATABASE "${dbName.replace(/"/g, "\"\"")}" ALLOW_CONNECTIONS true`;
|
|
350
|
+
try {
|
|
351
|
+
await this.poolManager.getDrizzle(MAINTENANCE_DATABASE).execute(sql.raw(statement));
|
|
352
|
+
} catch (err) {
|
|
353
|
+
throw new Error(`"${dbName}" was closed to new connections for the copy and could not be opened again (${extractPgError(err)?.message ?? extractCauseMessage(err) ?? String(err)}). Nothing can connect to it until you run, from another database: ${statement};`);
|
|
354
|
+
} finally {
|
|
355
|
+
await this.poolManager.disconnectDatabase(MAINTENANCE_DATABASE);
|
|
256
356
|
}
|
|
257
357
|
}
|
|
258
358
|
/**
|
|
@@ -375,4 +475,4 @@ var BranchService = class {
|
|
|
375
475
|
//#endregion
|
|
376
476
|
export { BranchService_exports as n, BranchingUnsupportedError as r, BranchService as t };
|
|
377
477
|
|
|
378
|
-
//# sourceMappingURL=BranchService-
|
|
478
|
+
//# sourceMappingURL=BranchService-DO_3_CXt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BranchService-DO_3_CXt.js","names":[],"sources":["../src/services/BranchService.ts"],"sourcesContent":["/**\n * BranchService\n *\n * Manages database branching by creating/deleting PostgreSQL databases\n * using `CREATE DATABASE ... TEMPLATE`. Branch metadata is stored in the\n * `rebase.branches` table in the default (main) database, following the\n * same `rebase` schema convention used by entity_history, auth, etc.\n */\n\nimport { sql } from \"drizzle-orm\";\nimport { BranchInfo } from \"@rebasepro/types\";\nimport { revokeInternalTableSql } from \"@rebasepro/common\";\nimport { DrizzleClient } from \"../interfaces\";\nimport { BranchRow } from \"../branch-prune\";\nimport { DatabasePoolManager } from \"../databasePoolManager\";\nimport { extractPgError, extractCauseMessage } from \"../utils/pg-error-utils\";\n\n/** Internal prefix applied to branch database names to avoid collisions. */\nconst BRANCH_DB_PREFIX = \"rb_\";\n\n/** `duplicate_database` — the target database name is already taken. */\nconst PG_DUPLICATE_DATABASE = \"42P04\";\n\n/** `object_in_use` — the database still has connections attached. */\nconst PG_OBJECT_IN_USE = \"55006\";\n\n/** How many times `--force` disconnects the other sessions before it reports one still there. */\nconst FORCE_ATTEMPTS = 3;\n\n/** The database every Postgres server has, from which another one can be closed and opened. */\nconst MAINTENANCE_DATABASE = \"postgres\";\n\n/**\n * Describe a failed branch DDL statement in terms a user can act on.\n *\n * Drizzle reports failures as `Failed query: <sql> params:` and hides the real\n * PostgreSQL error in the `cause` chain, so matching on `err.message` never sees\n * the actual problem. Match on the PG error code instead — it survives wrapping\n * and, unlike the message text, is not locale-dependent.\n */\nfunction describeBranchDdlError(err: unknown, fallbackContext: string): Error {\n const pgError = extractPgError(err);\n\n if (pgError?.code === PG_DUPLICATE_DATABASE) {\n return new Error(`Database \"${fallbackContext}\" already exists on the server. Choose a different branch name.`);\n }\n if (pgError?.code === PG_OBJECT_IN_USE) {\n return new Error(\n `Cannot complete the operation: the database \"${fallbackContext}\" has active connections. ` +\n \"Close other clients or connections and try again.\"\n );\n }\n\n // Unknown failure: surface the real PG message rather than the Drizzle\n // wrapper, which would otherwise show the raw SQL and no reason at all.\n const detail = pgError?.message ?? extractCauseMessage(err);\n if (detail) return new Error(detail);\n return err instanceof Error ? err : new Error(String(err));\n}\n\n/** Fully-qualified metadata table in the rebase schema. */\nconst BRANCHES_TABLE = \"rebase.branches\";\n\n/**\n * The connected server cannot branch, so nothing was done.\n *\n * A refusal, not a fault: typed so the transports can forward it intact.\n * `websocket.ts` answers it with its own code and message rather than letting\n * it fall into the generic handler, which suppresses the text in production —\n * and the text is the only part that says what to do instead.\n */\nexport class BranchingUnsupportedError extends Error {\n /** Wire code, forwarded verbatim to the client. */\n readonly code = \"BRANCHING_UNSUPPORTED\";\n\n constructor(message: string) {\n super(message);\n this.name = \"BranchingUnsupportedError\";\n }\n}\n\n/**\n * What to say when the server serves one database.\n *\n * Deliberately the same argument the CLI's `refuseBranchOnManagedDatabase`\n * makes, because it is the same refusal — it just used to be available only to\n * whoever came in through argv. One paragraph, because this one has to fit in a\n * toast as well as a terminal.\n */\nconst BRANCHING_UNSUPPORTED_MESSAGE =\n \"Branching does not work on the managed development database. It copies a database with \"\n + \"CREATE DATABASE ... TEMPLATE, and the managed database is PGlite, which serves exactly one — \"\n + \"the copy would be the original, and every write you meant to sandbox would land in it. \"\n + \"Branching needs a real Postgres: run `rebase dev --docker`, or uncomment DATABASE_URL in .env \"\n + \"to point at your own.\";\n\n/**\n * Validate that a user-provided identifier only contains safe characters.\n * Throws if the value contains characters outside [a-zA-Z0-9_-].\n */\nfunction validateIdentifier(value: string, label: string): void {\n if (!/^[a-zA-Z0-9_-]+$/.test(value)) {\n throw new Error(`Invalid ${label}: only letters, digits, underscores, and hyphens are allowed.`);\n }\n}\n\n/**\n * Postgres truncates identifiers at NAMEDATALEN-1 = 63 bytes, and does it\n * silently. The prefix comes out of the same budget.\n */\nconst MAX_BRANCH_NAME_LENGTH = 63 - BRANCH_DB_PREFIX.length;\n\n/**\n * Check a user-provided branch name, and otherwise leave it exactly as given.\n *\n * This used to strip everything outside [a-zA-Z0-9_], so `my-feature` was\n * quietly created as `myfeature`: the name you typed was not the name `list`\n * gave back. Nothing ever needed that. Every identifier this service builds is\n * double-quoted (see `CREATE DATABASE` below), which is what makes a hyphen\n * safe, and `validateIdentifier` has always accepted hyphens for the `--from`\n * source database — the two disagreed about the same character class.\n *\n * Refusing a name we cannot represent is better than representing a different\n * one, which is also why the length is checked here rather than left to\n * Postgres, whose answer to an over-long identifier is a silent rename.\n */\nfunction assertValidBranchName(name: string): void {\n validateIdentifier(name, \"branch name\");\n if (name.length > MAX_BRANCH_NAME_LENGTH) {\n throw new Error(\n `Branch name \"${name}\" is too long: ${name.length} characters, maximum ${MAX_BRANCH_NAME_LENGTH}. ` +\n \"Postgres truncates identifiers past 63 bytes, which would give the branch a name you did not choose.\"\n );\n }\n}\n\n/**\n * Convert a user-facing branch name to the actual PostgreSQL database name.\n */\nfunction toBranchDbName(name: string): string {\n assertValidBranchName(name);\n return `${BRANCH_DB_PREFIX}${name}`;\n}\n\n/**\n * Where a server's branches are kept: the main database — what a branch is\n * copied from unless told otherwise, and what may never be dropped — and the\n * connection whose `rebase.branches` records them.\n */\nexport interface BranchHome {\n /** The main database's name. */\n database(): Promise<string>;\n /**\n * A connection on the database the registry is in. Asked for at each use:\n * a pool to the main database is closed before it is copied.\n */\n registry(): Promise<DrizzleClient>;\n}\n\nexport class BranchService {\n private readonly home: BranchHome;\n\n /**\n * @param db A connection on the server the branches are made on. Branch\n * DDL runs here.\n * @param home Where branches are kept. Without one, the database the\n * pool manager's connection string names, and `db`'s registry.\n */\n constructor(\n private db: DrizzleClient,\n private poolManager: DatabasePoolManager,\n home?: BranchHome\n ) {\n this.home = home ?? {\n database: async () => poolManager.defaultDatabaseName,\n registry: async () => db\n };\n }\n\n /**\n * Refuse a branch mutation the connected server cannot honour.\n *\n * The check lives here rather than in the CLI's argv parsing because the\n * CLI is not the only caller: Studio reaches `createBranch` over the\n * websocket, and before this it got \"Branch created successfully\" and a\n * detail pane promising isolation for a database that is the parent.\n *\n * Reads only, and only mutations are guarded: `listBranches` on a managed\n * database is an honest empty list, and refusing it would turn the Branches\n * pane into a failure with a \"Try again\" that can never succeed.\n */\n private async assertBranchingSupported(): Promise<void> {\n if (await this.poolManager.servesOneDatabase()) {\n throw new BranchingUnsupportedError(BRANCHING_UNSUPPORTED_MESSAGE);\n }\n }\n\n /**\n * Whether the `rebase.branches` registry exists. A database that has never\n * had a branch made on it may not have it, and reading it there is an\n * empty answer, not an error — nor a reason to create it. The CLI's `list`\n * and `info` ask this rather than running the DDL that creates it.\n */\n async hasBranchRegistry(): Promise<boolean> {\n const result = await (await this.home.registry()).execute(\n sql`SELECT to_regclass('rebase.branches') IS NOT NULL AS present`\n );\n const [row] = result.rows;\n return row !== undefined && Reflect.get(row, \"present\") === true;\n }\n\n /**\n * Ensure the `rebase.branches` metadata table exists in the default database.\n * Idempotent — safe to call on every startup.\n */\n async ensureBranchMetadataTable(): Promise<void> {\n // Create the rebase schema (idempotent — may already exist from auth/history init)\n const registry = await this.home.registry();\n await registry.execute(sql`CREATE SCHEMA IF NOT EXISTS rebase`);\n\n await registry.execute(sql.raw(`\n CREATE TABLE IF NOT EXISTS ${BRANCHES_TABLE} (\n name TEXT PRIMARY KEY,\n db_name TEXT NOT NULL UNIQUE,\n parent_db TEXT NOT NULL,\n created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),\n metadata JSONB DEFAULT '{}'\n );\n `));\n\n // Not a collection, so no RLS — and it names every branch database on\n // this server. The driver's schema-wide grant reaches it, so revoke.\n await registry.execute(sql.raw(revokeInternalTableSql(\"rebase\", \"branches\")));\n }\n\n /**\n * Create a new branch database by templating the source database.\n *\n * Uses `CREATE DATABASE ... TEMPLATE` for an instant, full-fidelity copy\n * of both schema and data.\n *\n * @param name User-facing branch name (e.g., \"feature_auth\")\n * @param options.source Source database to clone; defaults to the main database.\n */\n async createBranch(\n name: string,\n options?: { source?: string; force?: boolean }\n ): Promise<BranchInfo> {\n await this.assertBranchingSupported();\n\n if (options?.source) {\n validateIdentifier(options.source, \"source database name\");\n }\n\n const dbName = toBranchDbName(name);\n const sourceDb = options?.source || await this.home.database();\n\n // Check if branch already exists\n const existing = await (await this.home.registry()).execute(\n sql`SELECT name FROM rebase.branches WHERE name = ${name} OR db_name = ${dbName}`\n );\n if ((existing.rows as unknown[]).length > 0) {\n throw new Error(`Branch \"${name}\" already exists.`);\n }\n\n // Disconnect any idle pools to the source DB so TEMPLATE works.\n // CREATE DATABASE ... TEMPLATE requires no other connections to the template.\n //\n // This only reaches pools inside *this* process. The CLI runs as its own\n // process, so a `rebase dev` holding the database open is untouched by\n // it — which is why the failure below has to name what is connected, and\n // why `--force` exists.\n await this.poolManager.disconnectDatabase(sourceDb);\n\n // Create the database using the source as a template.\n // Note: Identifiers must be double-quoted, not parameterized.\n const safeDbName = dbName.replace(/\"/g, '\"\"');\n const safeSourceDb = sourceDb.replace(/\"/g, '\"\"');\n const copy = async (): Promise<void> => {\n await this.db.execute(sql.raw(`CREATE DATABASE \"${safeDbName}\" TEMPLATE \"${safeSourceDb}\"`));\n };\n try {\n if (options?.force) {\n await this.whileClosedToNewConnections(sourceDb, () => this.disconnectingOthers(sourceDb, copy));\n } else {\n await copy();\n }\n } catch (err) {\n const pgError = extractPgError(err);\n if (pgError?.code === PG_OBJECT_IN_USE) {\n // The template — not the new database — is the one still in use.\n throw new Error(\n `Cannot create branch: the source database \"${sourceDb}\" ${options?.force\n ? \"still had connections after they were disconnected\"\n : \"has active connections\"}.\\n`\n + await this.describeBlockingConnections(sourceDb, { forced: options?.force === true })\n );\n }\n throw describeBranchDdlError(err, dbName);\n }\n\n // Record metadata in the registry — asked for again, as copying the\n // main database closed any pool on it.\n const now = new Date();\n await (await this.home.registry()).execute(\n sql`INSERT INTO rebase.branches (name, db_name, parent_db, created_at)\n VALUES (${name}, ${dbName}, ${sourceDb}, ${now.toISOString()})`\n );\n\n return {\n name,\n database: dbName,\n parentDatabase: sourceDb,\n createdAt: now\n };\n }\n\n /**\n * Delete a branch database and remove its metadata.\n * Cannot delete the main/default database.\n */\n async deleteBranch(name: string, options?: { force?: boolean }): Promise<void> {\n await this.assertBranchingSupported();\n\n assertValidBranchName(name);\n\n // Safety, first pass: a request that would target the default database\n // is refused before any metadata is read, so the answer costs nothing\n // and cannot depend on what a row happens to say.\n const mainDatabase = await this.home.database();\n if (toBranchDbName(name) === mainDatabase) {\n throw new Error(\"Cannot delete the main database.\");\n }\n\n // Verify the branch exists, and take the database name from the row\n // rather than deriving it again. A branch created before names stopped\n // being stripped is recorded as `rb_myfeature` while `my-feature` now\n // derives `rb_my-feature`; re-deriving would drop a database this row\n // never named — or, if that name happened to exist, somebody else's.\n // The stored value is the only one that is true by construction.\n const existing = await (await this.home.registry()).execute(\n sql`SELECT db_name FROM rebase.branches WHERE name = ${name}`\n );\n const existingRows = existing.rows as Record<string, unknown>[];\n if (existingRows.length === 0) {\n throw new Error(`Branch \"${name}\" not found.`);\n }\n const dbName = existingRows[0].db_name as string;\n\n // Safety, second pass: the first checked a name we derived, this checks\n // the one we are about to drop. They differ for any row written under\n // the old scheme, and it is this one that governs.\n if (dbName === mainDatabase) {\n throw new Error(\"Cannot delete the main database.\");\n }\n\n // Disconnect any pools to this branch before dropping. Same limit as\n // `createBranch`: in-process pools only.\n await this.poolManager.disconnectDatabase(dbName);\n\n // Drop the database\n const safeDbName = dbName.replace(/\"/g, '\"\"');\n const drop = async (): Promise<void> => {\n await this.db.execute(sql.raw(`DROP DATABASE \"${safeDbName}\"`));\n };\n try {\n if (options?.force) await this.disconnectingOthers(dbName, drop);\n else await drop();\n } catch (err) {\n const pgError = extractPgError(err);\n if (pgError?.code === PG_OBJECT_IN_USE) {\n throw new Error(\n `Cannot delete branch \"${name}\": the database ${options?.force\n ? \"still had connections after they were disconnected\"\n : \"has active connections\"}.\\n`\n + await this.describeBlockingConnections(dbName, { forced: options?.force === true })\n );\n }\n throw describeBranchDdlError(err, dbName);\n }\n\n // Remove metadata\n await (await this.home.registry()).execute(\n sql`DELETE FROM rebase.branches WHERE name = ${name}`\n );\n }\n\n /**\n * Who is holding the database open, in words a developer can act on.\n *\n * `CREATE DATABASE ... TEMPLATE` and `DROP DATABASE` both require that no\n * other session is connected, and the one thing the old message could not\n * say is the only thing worth knowing: *which* session. \"Close other\n * clients or connections and try again\" is advice you cannot follow when\n * you do not know there is a `rebase dev` in another terminal — and that is\n * the common case, because wanting a branch and running the app are the\n * same moment.\n *\n * `application_name` is the useful column: node-postgres sends none by\n * default, so a Rebase process shows as `(unnamed)` while DBeaver, pgAdmin\n * and psql all name themselves. Counting by it separates \"my own dev\n * server\" from \"the GUI I forgot about\" without any guessing.\n *\n * Best-effort by construction: this runs while reporting a failure, and a\n * diagnostic that throws would replace a real error with its own.\n */\n private async describeBlockingConnections(dbName: string, options: { forced?: boolean } = {}): Promise<string> {\n try {\n const result = await this.db.execute(sql`\n SELECT coalesce(nullif(application_name, ''), '(unnamed)') AS app,\n count(*)::int AS n\n FROM pg_stat_activity\n WHERE datname = ${dbName}\n AND pid <> pg_backend_pid()\n GROUP BY 1\n ORDER BY 2 DESC, 1\n `);\n const rows = result.rows as Record<string, unknown>[];\n if (rows.length === 0) {\n // The blocker went away between the failure and this query, or\n // it is a session Postgres does not attribute to this database.\n return \" Retry — whatever was connected has since disconnected.\";\n }\n\n const listed = rows\n .map((row) => ` ${row.n} × ${String(row.app)}`)\n .join(\"\\n\");\n\n // With --force they were disconnected already, and came back: the\n // one thing left to say is what to stop, not to pass --force.\n return ` Connected right now:\\n${listed}\\n`\n + (options.forced\n ? \" They reconnected as fast as they were disconnected. A running `rebase dev` is the\\n\"\n + \" usual one — stop it, then run this again.\"\n : \" A running `rebase dev` is the usual one — stop it, or re-run with --force to\\n\"\n + \" disconnect them for you.\");\n } catch {\n return options.forced\n ? \" Stop the clients connected to it — a running `rebase dev` is the usual one — then run this again.\"\n : \" Close other clients and connections, or re-run with --force.\";\n }\n }\n\n /**\n * Run `operation` with no other session on `dbName`, disconnecting them\n * first: what `--force` asks for.\n *\n * Disconnected, a client that reconnects — a running `rebase dev`'s\n * realtime listener does — can be back before `CREATE DATABASE … TEMPLATE`\n * or `DROP DATABASE` counts the sessions, and it failed on one about once\n * in seven tries, saying to pass the `--force` that had been passed. So a\n * refusal on a session still connected is retried, disconnecting again, a\n * few times before it is reported.\n */\n private async disconnectingOthers(dbName: string, operation: () => Promise<void>): Promise<void> {\n for (let attempt = 1; ; attempt++) {\n const closed = await this.terminateConnections(dbName);\n if (closed > 0) {\n // Postgres reports the backend as gone before its slot is\n // actually free, and the operation then fails on a connection\n // that no longer exists. A short settle first.\n await new Promise((resolve) => setTimeout(resolve, 250));\n }\n try {\n await operation();\n return;\n } catch (err) {\n if (attempt >= FORCE_ATTEMPTS || extractPgError(err)?.code !== PG_OBJECT_IN_USE) throw err;\n }\n }\n }\n\n /**\n * Run `operation` with `dbName` refusing new connections, and open it\n * again after, whatever happens.\n *\n * A client disconnected from the source — a running `rebase dev` holds a\n * pool and a realtime listener — cannot then reconnect before the copy has\n * counted the sessions on it. Postgres refuses `ALLOW_CONNECTIONS false` on\n * the database a session is connected to, so both statements run from the\n * server's `postgres` database, and they need the database's owner. Where\n * either is missing, the retries of {@link disconnectingOthers} are what is\n * left. A database that already refused connections is left as it was.\n *\n * Interrupted (Ctrl-C) while closed, it opens the database again before\n * the process exits; one it could not open is named in the error, with the\n * statement that does.\n */\n private async whileClosedToNewConnections(dbName: string, operation: () => Promise<void>): Promise<void> {\n const safe = dbName.replace(/\"/g, '\"\"');\n const maintenance = dbName === MAINTENANCE_DATABASE ? null : this.poolManager.getDrizzle(MAINTENANCE_DATABASE);\n let closed = false;\n try {\n if (maintenance) {\n const state = await maintenance.execute(sql`SELECT datallowconn FROM pg_database WHERE datname = ${dbName}`);\n const [row] = state.rows as Record<string, unknown>[];\n if (row?.datallowconn === true) {\n await maintenance.execute(sql.raw(`ALTER DATABASE \"${safe}\" ALLOW_CONNECTIONS false`));\n closed = true;\n }\n }\n } catch {\n // No `postgres` database to run it from, or not the owner.\n }\n if (!closed) {\n await this.poolManager.disconnectDatabase(MAINTENANCE_DATABASE);\n await operation();\n return;\n }\n\n let reopening: Promise<void> | undefined;\n const reopen = (): Promise<void> => {\n reopening ??= this.reopenToConnections(dbName);\n return reopening;\n };\n const onSignal = (signal: NodeJS.Signals): void => {\n void reopen().finally(() => process.kill(process.pid, signal));\n };\n process.once(\"SIGINT\", onSignal);\n process.once(\"SIGTERM\", onSignal);\n try {\n await operation();\n } finally {\n process.removeListener(\"SIGINT\", onSignal);\n process.removeListener(\"SIGTERM\", onSignal);\n await reopen();\n }\n }\n\n /** `ALLOW_CONNECTIONS true` again, from the maintenance database, or an error that says how to do it by hand. */\n private async reopenToConnections(dbName: string): Promise<void> {\n const statement = `ALTER DATABASE \"${dbName.replace(/\"/g, '\"\"')}\" ALLOW_CONNECTIONS true`;\n try {\n await this.poolManager.getDrizzle(MAINTENANCE_DATABASE).execute(sql.raw(statement));\n } catch (err) {\n throw new Error(\n `\"${dbName}\" was closed to new connections for the copy and could not be opened again ` +\n `(${extractPgError(err)?.message ?? extractCauseMessage(err) ?? String(err)}). ` +\n `Nothing can connect to it until you run, from another database: ${statement};`\n );\n } finally {\n await this.poolManager.disconnectDatabase(MAINTENANCE_DATABASE);\n }\n }\n\n /**\n * Disconnect every other session on a database, and say how many.\n *\n * `pg_terminate_backend` rather than `pg_cancel_backend`: cancelling a\n * query leaves the session connected, and a connected session is exactly\n * what blocks the operation.\n *\n * The current backend is excluded — terminating the connection running this\n * statement would abort the command that asked for it.\n */\n private async terminateConnections(dbName: string): Promise<number> {\n const result = await this.db.execute(sql`\n SELECT pg_terminate_backend(pid)\n FROM pg_stat_activity\n WHERE datname = ${dbName}\n AND pid <> pg_backend_pid()\n `);\n\n return (result.rows as unknown[]).length;\n }\n\n /**\n * Everything prune needs to decide, in two queries.\n *\n * Reads the rows and the server's database list rather than joining them,\n * so the *disagreements* between the two are visible — a row whose database\n * is gone and a database with no row are the two things prune exists to\n * find, and a join hides both.\n */\n async pruneCandidates(): Promise<{ rows: BranchRow[]; databases: string[] }> {\n const rowResult = await (await this.home.registry()).execute(sql.raw(`\n SELECT name, db_name, created_at FROM ${BRANCHES_TABLE} ORDER BY created_at\n `));\n const rows = (rowResult.rows as Record<string, unknown>[]).map((row) => ({\n name: row.name as string,\n dbName: row.db_name as string,\n createdAt: new Date(row.created_at as string)\n }));\n\n const dbResult = await this.db.execute(sql`\n SELECT datname FROM pg_database WHERE NOT datistemplate\n `);\n const databases = (dbResult.rows as Record<string, unknown>[]).map((row) => String(row.datname));\n\n return { rows, databases };\n }\n\n /** Remove a metadata row whose database is already gone. */\n async forgetBranchRow(name: string): Promise<void> {\n await (await this.home.registry()).execute(sql`DELETE FROM rebase.branches WHERE name = ${name}`);\n }\n\n /**\n * Drop a database by name, with no metadata row required.\n *\n * `deleteBranch` deliberately takes its database name from the row, because\n * the row is the only value that is true by construction. Prune has the\n * opposite job — the cases it handles are exactly the ones where row and\n * database disagree — so it needs to name a database directly. Guarded the\n * same way regardless: the main database is never droppable.\n */\n async dropDatabase(dbName: string): Promise<void> {\n validateIdentifier(dbName, \"database name\");\n if (dbName === await this.home.database()) {\n throw new Error(\"Cannot delete the main database.\");\n }\n\n await this.poolManager.disconnectDatabase(dbName);\n try {\n await this.db.execute(sql.raw(`DROP DATABASE \"${dbName.replace(/\"/g, '\"\"')}\"`));\n } catch (err) {\n throw describeBranchDdlError(err, dbName);\n }\n }\n\n /**\n * List all branches recorded in the metadata table.\n * Optionally fetches database sizes from pg_database.\n */\n async listBranches(): Promise<BranchInfo[]> {\n const result = await (await this.home.registry()).execute(sql.raw(`\n SELECT \n b.name,\n b.db_name,\n b.parent_db,\n b.created_at,\n pg_database_size(b.db_name) as size_bytes\n FROM ${BRANCHES_TABLE} b\n JOIN pg_database d ON d.datname = b.db_name\n ORDER BY b.created_at DESC\n `));\n\n return (result.rows as Record<string, unknown>[]).map((row) => ({\n name: row.name as string,\n database: row.db_name as string,\n parentDatabase: row.parent_db as string,\n createdAt: new Date(row.created_at as string),\n sizeBytes: row.size_bytes != null ? Number(row.size_bytes) : undefined\n }));\n }\n\n /**\n * Get info about a specific branch.\n */\n async getBranchInfo(name: string): Promise<BranchInfo | undefined> {\n assertValidBranchName(name);\n\n const result = await (await this.home.registry()).execute(sql`\n SELECT\n b.name,\n b.db_name,\n b.parent_db,\n b.created_at\n FROM rebase.branches b\n WHERE b.name = ${name}\n `);\n\n const rows = result.rows as Record<string, unknown>[];\n if (rows.length === 0) return undefined;\n\n const row = rows[0];\n\n // Attempt to get size — may fail if the DB was externally dropped.\n // Same reason as `deleteBranch`: the size of a re-derived name is the\n // size of some other database, or of nothing.\n let sizeBytes: number | undefined;\n try {\n const dbName = row.db_name as string;\n const sizeResult = await this.db.execute(\n sql`SELECT pg_database_size(${dbName}) as size_bytes`\n );\n const sizeRows = sizeResult.rows as Record<string, unknown>[];\n if (sizeRows.length > 0 && sizeRows[0].size_bytes != null) {\n sizeBytes = Number(sizeRows[0].size_bytes);\n }\n } catch {\n // Database might not exist anymore\n }\n\n return {\n name: row.name as string,\n database: row.db_name as string,\n parentDatabase: row.parent_db as string,\n createdAt: new Date(row.created_at as string),\n sizeBytes\n };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAkBA,IAAM,mBAAmB;;AAGzB,IAAM,wBAAwB;;AAG9B,IAAM,mBAAmB;;AAGzB,IAAM,iBAAiB;;AAGvB,IAAM,uBAAuB;;;;;;;;;AAU7B,SAAS,uBAAuB,KAAc,iBAAgC;CAC1E,MAAM,UAAU,eAAe,GAAG;CAElC,IAA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AQ,GAAG;;;;;;;6BAOxC,KAAK;SACzB,EAAA,CAEmB;EACpB,IAAI,KAAK,WAAW,GAAG,OAAO,KAAA;EAE9B,MAAM,MAAM,KAAK;EAKjB,IAAI;EACJ,IAAI;GACA,MAAM,SAAS,IAAI;GAInB,MAAM,YAAW,MAHQ,KAAK,GAAG,QAC7B,GAAG,2BAA2B,OAAO,gBACzC,EAAA,CAC4B;GAC5B,IAAI,SAAS,SAAS,KAAK,SAAS,EAAE,CAAC,cAAc,MACjD,YAAY,OAAO,SAAS,EAAE,CAAC,UAAU;EAEjD,QAAQ,CAER;EAEA,OAAO;GACH,MAAM,IAAI;GACV,UAAU,IAAI;GACd,gBAAgB,IAAI;GACpB,WAAW,IAAI,KAAK,IAAI,UAAoB;GAC5C;EACJ;CACJ;AACJ"}
|
|
@@ -110,6 +110,13 @@ export declare class PostgresBackendDriver implements DataDriver {
|
|
|
110
110
|
* migrations, `dataAsAdmin`).
|
|
111
111
|
*/
|
|
112
112
|
rlsUserRole?: string;
|
|
113
|
+
/**
|
|
114
|
+
* The direct (non-pooled) URL {@link listen} holds its `LISTEN` on — this
|
|
115
|
+
* source's, set by the bootstrapper. Never read from `DATABASE_DIRECT_URL`
|
|
116
|
+
* here: that variable is the default source's, and a second database
|
|
117
|
+
* listening on it hears none of its own notifications.
|
|
118
|
+
*/
|
|
119
|
+
listenConnectionString?: string;
|
|
113
120
|
/**
|
|
114
121
|
* When true, realtime notifications are deferred until after the
|
|
115
122
|
* wrapping transaction commits. Set by `withAuth` → `withTransaction`.
|
|
@@ -132,11 +139,12 @@ export declare class PostgresBackendDriver implements DataDriver {
|
|
|
132
139
|
* `SQLAdmin.listen`: a `LISTEN` on a connection of its own, through the
|
|
133
140
|
* listener CDC and the channel bus use, which reconnects and heartbeats.
|
|
134
141
|
*
|
|
135
|
-
* The connection is
|
|
136
|
-
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
* first connect that fails:
|
|
142
|
+
* The connection is the one the realtime listener uses: this source's
|
|
143
|
+
* direct URL ({@link listenConnectionString}) when the bootstrapper
|
|
144
|
+
* resolved one — a transaction-mode pooler such as PgBouncer does not keep
|
|
145
|
+
* a `LISTEN` — else the pool's own URL. The in-process development
|
|
146
|
+
* database has neither, and is refused, as is a first connect that fails:
|
|
147
|
+
* the caller polls instead.
|
|
140
148
|
*/
|
|
141
149
|
listen(channel: string, onNotify: (payload: string) => void, options?: {
|
|
142
150
|
onReconnect?: () => void;
|
|
@@ -275,6 +283,19 @@ export declare class PostgresBackendDriver implements DataDriver {
|
|
|
275
283
|
* path, such a row is absent, so it is read from its own collection.
|
|
276
284
|
*/
|
|
277
285
|
private storedRowPath;
|
|
286
|
+
/**
|
|
287
|
+
* The row an update addresses, as this caller reads it there: what a
|
|
288
|
+
* caller's precondition (the REST routes' 404 and `If-Match`) judges.
|
|
289
|
+
*
|
|
290
|
+
* `stored` is the row read from {@link storedRowPath}. Off a many-to-many
|
|
291
|
+
* path that is the address itself. Through one, the row is judged as a
|
|
292
|
+
* member of the parent's set when it is one. When it is not yet, this
|
|
293
|
+
* update is the link that makes it one (the persistence layer writes the
|
|
294
|
+
* junction row), so the row is judged as it is stored — provided the caller
|
|
295
|
+
* can read the parent it is being linked to. A parent that is not there, or
|
|
296
|
+
* that their policies hide, is no set to join, and the row reads as absent.
|
|
297
|
+
*/
|
|
298
|
+
private rowAnUpdateAddresses;
|
|
278
299
|
/**
|
|
279
300
|
* The stored row an upsert's key names, if this caller can address it —
|
|
280
301
|
* with its address and the values to update it with.
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Implements the `BackendBootstrapper` interface for PostgreSQL.
|
|
5
5
|
*/
|
|
6
6
|
import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
7
|
-
import { BackendBootstrapper, CollectionConfig, type RealtimeChannelsConfig } from "@rebasepro/types";
|
|
7
|
+
import { BackendBootstrapper, CollectionConfig, InitializedDriver, type DatabaseAdapterInitConfig, type RealtimeChannelsConfig } from "@rebasepro/types";
|
|
8
8
|
import { PostgresBackendDriver } from "./PostgresBackendDriver.js";
|
|
9
9
|
import { RealtimeService } from "./services/realtimeService.js";
|
|
10
10
|
import { DatabasePoolManager } from "./databasePoolManager.js";
|
|
@@ -14,7 +14,27 @@ import { type UnknownFilterFieldsMode } from "./utils/drizzle-conditions.js";
|
|
|
14
14
|
export interface PostgresDriverConfig {
|
|
15
15
|
connectionString?: string;
|
|
16
16
|
adminConnectionString?: string;
|
|
17
|
+
/**
|
|
18
|
+
* This database's read replica: reads are routed there, writes stay on
|
|
19
|
+
* the primary. Unset, the boot ({@link postgresDriverConfigFromEnv}) fills
|
|
20
|
+
* it from `DATABASE_READ_URL` for the default source only; any other
|
|
21
|
+
* source has none unless its config names one — the managed runtime
|
|
22
|
+
* resolves `DATABASE_READ_URL__<KEY>` into this for each source.
|
|
23
|
+
*/
|
|
17
24
|
readConnectionString?: string;
|
|
25
|
+
/**
|
|
26
|
+
* A direct (non-pooled) URL to this database, for the connections that
|
|
27
|
+
* hold a `LISTEN`: change capture, cross-instance realtime, the Postgres
|
|
28
|
+
* channel bus and `admin.listen`. A transaction-mode pooler such as
|
|
29
|
+
* PgBouncer does not keep a `LISTEN`, so behind one this has to name the
|
|
30
|
+
* database itself.
|
|
31
|
+
*
|
|
32
|
+
* Unset, the boot fills it from `DATABASE_DIRECT_URL` for the default
|
|
33
|
+
* source only, and any other source listens on its own
|
|
34
|
+
* `connectionString` — see {@link resolveSourceUrls}. The managed runtime
|
|
35
|
+
* resolves `DATABASE_DIRECT_URL__<KEY>` into this for each source.
|
|
36
|
+
*/
|
|
37
|
+
directConnectionString?: string;
|
|
18
38
|
connection?: unknown;
|
|
19
39
|
/**
|
|
20
40
|
* The project's generated Drizzle module, for **diagnostics only**.
|
|
@@ -37,13 +57,17 @@ export interface PostgresDriverConfig {
|
|
|
37
57
|
*/
|
|
38
58
|
introspectionSchema?: string;
|
|
39
59
|
/**
|
|
40
|
-
* Realtime options,
|
|
60
|
+
* Realtime options, among them:
|
|
41
61
|
*
|
|
42
62
|
* - `channels` — retention. Without rules no channel keeps any history and
|
|
43
63
|
* broadcast stays fire-and-forget. See {@link ChannelRetentionRule}.
|
|
44
64
|
* - `bus` — the cross-instance transport for channel broadcast and
|
|
45
65
|
* presence. Defaults to in-process only, which is correct for a single
|
|
46
66
|
* instance and wrong for two. See {@link ChannelBusConfig}.
|
|
67
|
+
* - `cdc` — how changes are captured (`auto` by default).
|
|
68
|
+
*
|
|
69
|
+
* At boot each has an environment variable that wins over it — see
|
|
70
|
+
* {@link postgresDriverConfigFromEnv}.
|
|
47
71
|
*/
|
|
48
72
|
realtime?: RealtimeChannelsConfig;
|
|
49
73
|
/**
|
|
@@ -65,6 +89,23 @@ export interface PostgresDriverInternals {
|
|
|
65
89
|
realtimeService: RealtimeService;
|
|
66
90
|
driver: PostgresBackendDriver;
|
|
67
91
|
poolManager?: DatabasePoolManager;
|
|
92
|
+
/**
|
|
93
|
+
* Whether the driver was opened with provisioning (`provision`, default
|
|
94
|
+
* true). The hooks that run after it — auth, history, the final security
|
|
95
|
+
* posture — read it, so `provision: false` means no DDL anywhere on the
|
|
96
|
+
* driver's path, not only while it opens.
|
|
97
|
+
*/
|
|
98
|
+
provisioned: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* How authenticated requests are isolated on this connection, in one
|
|
101
|
+
* line: run as the restricted role, or filtered natively.
|
|
102
|
+
*
|
|
103
|
+
* Returned rather than logged by {@link openPostgresDriver}. Announcing it
|
|
104
|
+
* is the server boot's business — `initializeDriver` logs it at info — and
|
|
105
|
+
* a script's `openRebase` logs it at debug, so a script's stdout holds what
|
|
106
|
+
* the script prints.
|
|
107
|
+
*/
|
|
108
|
+
rlsEnforcement?: string;
|
|
68
109
|
/**
|
|
69
110
|
* Attach CDC triggers to tables that did not exist when the driver
|
|
70
111
|
* bootstrapped. Only set when database-level capture is actually active.
|
|
@@ -118,6 +159,27 @@ export declare function describeSchemaDriftCause(provisioning: {
|
|
|
118
159
|
attempted: boolean;
|
|
119
160
|
reason?: string;
|
|
120
161
|
} | undefined): string[];
|
|
162
|
+
/**
|
|
163
|
+
* The URLs one source's driver opens besides its pool: the read replica, and
|
|
164
|
+
* the direct connection its `LISTEN`s are held on.
|
|
165
|
+
*
|
|
166
|
+
* The bare `DATABASE_READ_URL` and `DATABASE_DIRECT_URL` belong to the default
|
|
167
|
+
* source. Both used to be read here for every source, so a second database
|
|
168
|
+
* (`database("analytics")`) held its change-capture `LISTEN` on the default
|
|
169
|
+
* database — where none of its notifications are sent, so its realtime feed
|
|
170
|
+
* was silent — and read through the default's replica. A keyed source gets
|
|
171
|
+
* the URLs its config names, and otherwise its own connection and no replica.
|
|
172
|
+
*
|
|
173
|
+
* The default source still falls back to the environment: an application that
|
|
174
|
+
* builds this adapter itself passes a `connectionString` and nothing else, and
|
|
175
|
+
* has always had those two variables honoured.
|
|
176
|
+
*
|
|
177
|
+
* Exported for its own test; the caller needs a live database.
|
|
178
|
+
*/
|
|
179
|
+
export declare function resolveSourceUrls(pgConfig: Pick<PostgresDriverConfig, "connectionString" | "readConnectionString" | "directConnectionString">, sourceKey: string, env?: Record<string, string | undefined>): {
|
|
180
|
+
readUrl?: string;
|
|
181
|
+
directUrl?: string;
|
|
182
|
+
};
|
|
121
183
|
/**
|
|
122
184
|
* Is this the local database `rebase init` scaffolds — i.e. the one case where
|
|
123
185
|
* "you are connected as a superuser" is not news?
|
|
@@ -144,4 +206,51 @@ export declare function describeSchemaDriftCause(provisioning: {
|
|
|
144
206
|
* ships `NODE_ENV=development` and some deployments inherit it.
|
|
145
207
|
*/
|
|
146
208
|
export declare function isScaffoldedLocalDatabase(connectionString: string | undefined): boolean;
|
|
209
|
+
/**
|
|
210
|
+
* `config` with the environment read into it, the way the server's boot reads
|
|
211
|
+
* it. The one place this driver reads environment variables:
|
|
212
|
+
*
|
|
213
|
+
* | variable | setting |
|
|
214
|
+
* |----------------------------------------|----------------------------------------|
|
|
215
|
+
* | `DATABASE_READ_URL` | `readConnectionString`, when unset (*) |
|
|
216
|
+
* | `DATABASE_DIRECT_URL` | `directConnectionString`, when unset (*)|
|
|
217
|
+
* | `REALTIME_CDC` | `realtime.cdc` |
|
|
218
|
+
* | `REALTIME_CHANNEL_BUS` | `realtime.bus` (a named transport) |
|
|
219
|
+
* | `REALTIME_MAX_SUBSCRIPTIONS_PER_SOCKET`| `realtime.maxSubscriptionsPerSocket` |
|
|
220
|
+
* | `REALTIME_MAX_SOCKETS_PER_USER` | `realtime.maxSocketsPerUser` |
|
|
221
|
+
* | `REALTIME_REFETCH_MAX_WAIT_MS` | `realtime.refetchMaxWaitMs` |
|
|
222
|
+
* | `REALTIME_RECOUNT_INTERVAL_MS` | `realtime.recountIntervalMs` |
|
|
223
|
+
* | `REALTIME_RECOUNT_MIN_ROWS` | `realtime.recountMinRows` |
|
|
224
|
+
*
|
|
225
|
+
* A `REALTIME_*` variable that is set wins over the option, so a deployment
|
|
226
|
+
* can change it without a rebuild; the URLs fill in only what the config left
|
|
227
|
+
* empty. A mistyped number throws, naming the variable.
|
|
228
|
+
*
|
|
229
|
+
* (*) For the default source only. The bare URL variables name the default
|
|
230
|
+
* database; read for a second source (`database("analytics")`), they put its
|
|
231
|
+
* change-capture `LISTEN` on the default database, where none of its
|
|
232
|
+
* notifications arrive, and its reads on the default's replica. A keyed
|
|
233
|
+
* source's URLs come from its own config — the managed runtime passes
|
|
234
|
+
* `DATABASE_*_URL__<KEY>` there — see {@link resolveSourceUrls}.
|
|
235
|
+
*
|
|
236
|
+
* `createPostgresAdapter` applies this when the server boots, and
|
|
237
|
+
* {@link openPostgresDriver} does not — a script that opens the driver itself
|
|
238
|
+
* gets exactly the config it passed.
|
|
239
|
+
*/
|
|
240
|
+
export declare function postgresDriverConfigFromEnv(config: PostgresDriverConfig, env?: NodeJS.ProcessEnv, sourceKey?: string): PostgresDriverConfig;
|
|
241
|
+
/**
|
|
242
|
+
* Open the Postgres data driver from explicit settings.
|
|
243
|
+
*
|
|
244
|
+
* What `createPostgresAdapter` runs when the server boots, once it has read
|
|
245
|
+
* the environment into `pgConfig` (see {@link postgresDriverConfigFromEnv});
|
|
246
|
+
* this function reads no environment variable itself. `config` says what the
|
|
247
|
+
* caller wants opened: which collections, whether opening may change the
|
|
248
|
+
* database (`provision`, default true), and what part this process takes in
|
|
249
|
+
* realtime (`realtime`, or `false` for none at all).
|
|
250
|
+
*
|
|
251
|
+
* `openRebase` is this with `provision: false` and `realtime: false`.
|
|
252
|
+
*/
|
|
253
|
+
export declare function openPostgresDriver(pgConfig: PostgresDriverConfig, config: DatabaseAdapterInitConfig): Promise<InitializedDriver & {
|
|
254
|
+
internals: PostgresDriverInternals;
|
|
255
|
+
}>;
|
|
147
256
|
export declare function createPostgresBootstrapper(pgConfig: PostgresDriverConfig): BackendBootstrapper;
|