@rebasepro/server-postgres 0.9.1-canary.c0d4c07 → 0.9.1-canary.d906fb7
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/dist/auth/services.d.ts +0 -16
- package/dist/connection.d.ts +0 -21
- package/dist/index.es.js +29 -148
- package/dist/index.es.js.map +1 -1
- package/dist/schema/doctor.d.ts +1 -1
- package/dist/security/policy-drift.d.ts +0 -30
- package/package.json +6 -7
- package/src/auth/ensure-tables.ts +11 -73
- package/src/auth/services.ts +19 -49
- package/src/cli.ts +0 -60
- package/src/connection.ts +1 -61
- package/src/databasePoolManager.ts +0 -2
- package/src/schema/doctor.ts +20 -45
- package/src/security/policy-drift.test.ts +1 -106
- package/src/security/policy-drift.ts +0 -56
- package/src/services/row-pipeline.ts +3 -27
package/dist/schema/doctor.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { CollectionConfig, Property } from "@rebasepro/types";
|
|
|
2
2
|
export type IssueSeverity = "error" | "warning" | "info";
|
|
3
3
|
export interface DoctorIssue {
|
|
4
4
|
severity: IssueSeverity;
|
|
5
|
-
category: "missing_table" | "missing_column" | "type_mismatch" | "missing_constraint" | "schema_stale" | "missing_enum" | "enum_value_mismatch" | "missing_foreign_key" | "sdk_stale"
|
|
5
|
+
category: "missing_table" | "missing_column" | "type_mismatch" | "missing_constraint" | "schema_stale" | "missing_enum" | "enum_value_mismatch" | "missing_foreign_key" | "sdk_stale";
|
|
6
6
|
table?: string;
|
|
7
7
|
column?: string;
|
|
8
8
|
expected?: string;
|
|
@@ -63,36 +63,6 @@ export declare function parseExpectedPolicies(ddl: string): PolicyRef[];
|
|
|
63
63
|
* of reads. The same blindness would hide a policy that fails open.
|
|
64
64
|
*/
|
|
65
65
|
export declare function checkPolicyDrift(client: Queryable, collections: CollectionConfig[]): Promise<PolicyDrift>;
|
|
66
|
-
/**
|
|
67
|
-
* Does this name look like one the generator produced for this table?
|
|
68
|
-
*
|
|
69
|
-
* Unnamed rules compile to `<table>_<op>_<sha1[0:7]>` (plus `_<idx>` when one
|
|
70
|
-
* rule spans several operations), and the hash covers the rule's semantics — so
|
|
71
|
-
* *editing* a rule renames its policy. The policy under the old name is left
|
|
72
|
-
* behind by `db push`, which only DROPs the names it is about to CREATE, and
|
|
73
|
-
* Postgres ORs PERMISSIVE policies together: a superseded `USING (true)` keeps
|
|
74
|
-
* granting everything no matter how tight its replacement is.
|
|
75
|
-
*
|
|
76
|
-
* Matching the shape is what makes dropping them safe. A hand-written policy
|
|
77
|
-
* would have to collide with a 7-hex digest to be mistaken for generated one;
|
|
78
|
-
* a policy named anything else is left alone and merely reported, because a
|
|
79
|
-
* custom name is indistinguishable from one someone wrote in SQL on purpose.
|
|
80
|
-
*/
|
|
81
|
-
export declare function isGeneratedPolicyName(name: string, table: string): boolean;
|
|
82
|
-
export interface OrphanCleanup {
|
|
83
|
-
/** Superseded generated policies that were dropped. */
|
|
84
|
-
dropped: PolicyRef[];
|
|
85
|
-
/** Orphans left in place because their names are not generator-shaped. */
|
|
86
|
-
kept: PolicyRef[];
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Drop the policies an earlier push superseded but never removed.
|
|
90
|
-
*
|
|
91
|
-
* Only touches tables the collections describe — a table with no expected
|
|
92
|
-
* policy is not ours to reconcile, and scanning by schema alone would sweep up
|
|
93
|
-
* policies belonging to something else sharing the database.
|
|
94
|
-
*/
|
|
95
|
-
export declare function dropOrphanedPolicies(client: Queryable, drift: PolicyDrift, collections: CollectionConfig[]): Promise<OrphanCleanup>;
|
|
96
66
|
export declare const hasDrift: (d: PolicyDrift) => boolean;
|
|
97
67
|
/** Human-readable report; empty string when the database matches the config. */
|
|
98
68
|
export declare function formatPolicyDrift(drift: PolicyDrift): string;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rebasepro/server-postgres",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.9.1-canary.
|
|
4
|
+
"version": "0.9.1-canary.d906fb7",
|
|
5
5
|
"description": "PostgreSQL data source backend implementation for Rebase with Drizzle ORM",
|
|
6
6
|
"funding": {
|
|
7
7
|
"url": "https://github.com/sponsors/rebaseco"
|
|
@@ -71,15 +71,14 @@
|
|
|
71
71
|
"execa": "^9.6.1",
|
|
72
72
|
"pg": "^8.21.0",
|
|
73
73
|
"ws": "^8.21.0",
|
|
74
|
-
"@rebasepro/
|
|
75
|
-
"@rebasepro/
|
|
76
|
-
"@rebasepro/utils": "0.9.1-canary.
|
|
77
|
-
"@rebasepro/
|
|
78
|
-
"@rebasepro/types": "0.9.1-canary.
|
|
74
|
+
"@rebasepro/codegen": "0.9.1-canary.d906fb7",
|
|
75
|
+
"@rebasepro/server": "0.9.1-canary.d906fb7",
|
|
76
|
+
"@rebasepro/utils": "0.9.1-canary.d906fb7",
|
|
77
|
+
"@rebasepro/common": "0.9.1-canary.d906fb7",
|
|
78
|
+
"@rebasepro/types": "0.9.1-canary.d906fb7"
|
|
79
79
|
},
|
|
80
80
|
"devDependencies": {
|
|
81
81
|
"@hono/node-server": "^2.0.9",
|
|
82
|
-
"@jest/globals": "^30.4.1",
|
|
83
82
|
"@types/jest": "^30.0.0",
|
|
84
83
|
"@types/node": "^25.9.3",
|
|
85
84
|
"@types/pg": "^8.20.0",
|
|
@@ -251,32 +251,17 @@ export async function ensureAuthTablesExist(db: NodePgDatabase, collection?: Col
|
|
|
251
251
|
// Seed default roles if none exist
|
|
252
252
|
// (no-op: roles are now stored inline on the users table)
|
|
253
253
|
|
|
254
|
-
// ── Migration:
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
//
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
"password_hash VARCHAR(255)",
|
|
266
|
-
"email_verified BOOLEAN DEFAULT FALSE NOT NULL",
|
|
267
|
-
"email_verification_token VARCHAR(255)",
|
|
268
|
-
"email_verification_sent_at TIMESTAMP WITH TIME ZONE",
|
|
269
|
-
"is_anonymous BOOLEAN DEFAULT FALSE NOT NULL",
|
|
270
|
-
"metadata JSONB DEFAULT '{}' NOT NULL",
|
|
271
|
-
"created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL",
|
|
272
|
-
"updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL"
|
|
273
|
-
];
|
|
274
|
-
for (const columnDef of userColumnBackfills) {
|
|
275
|
-
await db.execute(sql`
|
|
276
|
-
ALTER TABLE ${sql.raw(usersTableName)}
|
|
277
|
-
ADD COLUMN IF NOT EXISTS ${sql.raw(columnDef)}
|
|
278
|
-
`);
|
|
279
|
-
}
|
|
254
|
+
// ── Migration: Add is_anonymous column (safe for existing tables) ────
|
|
255
|
+
await db.execute(sql`
|
|
256
|
+
ALTER TABLE ${sql.raw(usersTableName)}
|
|
257
|
+
ADD COLUMN IF NOT EXISTS is_anonymous BOOLEAN DEFAULT FALSE
|
|
258
|
+
`);
|
|
259
|
+
|
|
260
|
+
// ── Migration: Add inline roles column (safe for existing tables) ────
|
|
261
|
+
await db.execute(sql`
|
|
262
|
+
ALTER TABLE ${sql.raw(usersTableName)}
|
|
263
|
+
ADD COLUMN IF NOT EXISTS roles TEXT[] DEFAULT '{}' NOT NULL
|
|
264
|
+
`);
|
|
280
265
|
|
|
281
266
|
// ── Migration: Copy roles from legacy junction table to inline column ──
|
|
282
267
|
// If the old rebase.user_roles and rebase.roles tables exist, migrate
|
|
@@ -373,53 +358,6 @@ export async function ensureAuthTablesExist(db: NodePgDatabase, collection?: Col
|
|
|
373
358
|
ON ${sql.raw(recoveryCodesTableName)}(user_id)
|
|
374
359
|
`);
|
|
375
360
|
|
|
376
|
-
// ── Migration: clear stale FORCE ROW LEVEL SECURITY (older RLS model) ──
|
|
377
|
-
// The current model never emits FORCE: privileged auth writes run as
|
|
378
|
-
// the table owner and rely on the owner bypassing plain ENABLE RLS
|
|
379
|
-
// (see generate-postgres-ddl-logic). A table still carrying FORCE from
|
|
380
|
-
// an older framework era binds the owner too, so the first user
|
|
381
|
-
// registration after an upgrade fails with SQLSTATE 42501. Reconcile
|
|
382
|
-
// on boot; only tables actually flagged get the ALTER (and its lock).
|
|
383
|
-
try {
|
|
384
|
-
const authTablePairs: [string, string][] = [
|
|
385
|
-
[usersSchema, resolvedTable],
|
|
386
|
-
[authSchema, "user_identities"],
|
|
387
|
-
[authSchema, "refresh_tokens"],
|
|
388
|
-
[authSchema, "password_reset_tokens"],
|
|
389
|
-
[authSchema, "app_config"],
|
|
390
|
-
[authSchema, "mfa_factors"],
|
|
391
|
-
[authSchema, "mfa_challenges"],
|
|
392
|
-
[authSchema, "recovery_codes"]
|
|
393
|
-
];
|
|
394
|
-
for (const [schemaName, tableName] of authTablePairs) {
|
|
395
|
-
const forced = await db.execute(sql`
|
|
396
|
-
SELECT 1
|
|
397
|
-
FROM pg_class c
|
|
398
|
-
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
399
|
-
WHERE n.nspname = ${schemaName}
|
|
400
|
-
AND c.relname = ${tableName}
|
|
401
|
-
AND c.relforcerowsecurity
|
|
402
|
-
`);
|
|
403
|
-
if (forced.rows.length > 0) {
|
|
404
|
-
await db.execute(sql`
|
|
405
|
-
ALTER TABLE ${sql.raw(`"${schemaName}"."${tableName}"`)}
|
|
406
|
-
NO FORCE ROW LEVEL SECURITY
|
|
407
|
-
`);
|
|
408
|
-
logger.warn(
|
|
409
|
-
`🔧 Cleared stale FORCE ROW LEVEL SECURITY on "${schemaName}"."${tableName}" ` +
|
|
410
|
-
"(legacy RLS model — it binds the owner connection and breaks privileged auth writes)"
|
|
411
|
-
);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
} catch (rlsReconcileError: unknown) {
|
|
415
|
-
// Non-fatal: the connection may lack ownership on a pre-provisioned
|
|
416
|
-
// table; registration will still fail loudly (42501) if FORCE remains.
|
|
417
|
-
logger.warn(
|
|
418
|
-
`⚠️ Could not reconcile FORCE ROW LEVEL SECURITY on auth tables: ` +
|
|
419
|
-
`${rlsReconcileError instanceof Error ? rlsReconcileError.message : String(rlsReconcileError)}`
|
|
420
|
-
);
|
|
421
|
-
}
|
|
422
|
-
|
|
423
361
|
logger.info("✅ Auth tables ready");
|
|
424
362
|
} catch (error) {
|
|
425
363
|
logger.error("❌ Failed to create auth tables", { error });
|
package/src/auth/services.ts
CHANGED
|
@@ -82,32 +82,6 @@ export class UserService implements UserRepository {
|
|
|
82
82
|
return `"${schema}"."${name}"`;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
/**
|
|
86
|
-
* Run a privileged auth write with an explicitly cleared RLS context.
|
|
87
|
-
*
|
|
88
|
-
* The auth services run on the base/owner connection, which by design
|
|
89
|
-
* carries a NULL `app.user_id` so the `auth.uid() IS NULL` server-escape
|
|
90
|
-
* in the default policies applies. That NULL is normally guaranteed by
|
|
91
|
-
* `set_config(..., is_local = true)` resetting at transaction end — but a
|
|
92
|
-
* GUC that survives on a pooled connection (or a connection role that
|
|
93
|
-
* doesn't bypass RLS: FORCE ROW LEVEL SECURITY, or a non-owner role)
|
|
94
|
-
* turns the trusted write into an RLS-scoped one and denies it with
|
|
95
|
-
* SQLSTATE 42501. Clearing the GUCs here, transaction-locally at the
|
|
96
|
-
* single chokepoint, makes the server context deterministic instead of
|
|
97
|
-
* trusting whatever state the pool hands us. `auth.uid()` reads '' as
|
|
98
|
-
* NULL via NULLIF, so '' is the server context.
|
|
99
|
-
*/
|
|
100
|
-
private async withServerContext<T>(fn: (db: NodePgDatabase) => Promise<T>): Promise<T> {
|
|
101
|
-
return await this.db.transaction(async (tx) => {
|
|
102
|
-
await tx.execute(sql`
|
|
103
|
-
SELECT set_config('app.user_id', '', true),
|
|
104
|
-
set_config('app.user_roles', '', true),
|
|
105
|
-
set_config('app.jwt', '', true)
|
|
106
|
-
`);
|
|
107
|
-
return await fn(tx as unknown as NodePgDatabase);
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
85
|
private mapRowToUser(row: Record<string, unknown>): UserData {
|
|
112
86
|
if (!row) return row as UserData;
|
|
113
87
|
|
|
@@ -226,9 +200,7 @@ export class UserService implements UserRepository {
|
|
|
226
200
|
|
|
227
201
|
async createUser(data: CreateUserData): Promise<UserData> {
|
|
228
202
|
const payload = this.mapPayload(data);
|
|
229
|
-
const [row] = await this.
|
|
230
|
-
(await db.insert(this.usersTable).values(payload).returning()) as Record<string, unknown>[]
|
|
231
|
-
);
|
|
203
|
+
const [row] = (await this.db.insert(this.usersTable).values(payload).returning()) as Record<string, unknown>[];
|
|
232
204
|
return this.mapRowToUser(row);
|
|
233
205
|
}
|
|
234
206
|
|
|
@@ -283,12 +255,12 @@ export class UserService implements UserRepository {
|
|
|
283
255
|
}
|
|
284
256
|
|
|
285
257
|
async linkUserIdentity(userId: string, provider: string, providerId: string, profileData?: Record<string, unknown>): Promise<void> {
|
|
286
|
-
await this.
|
|
258
|
+
await this.db.insert(this.userIdentitiesTable).values({
|
|
287
259
|
userId,
|
|
288
260
|
provider,
|
|
289
261
|
providerId,
|
|
290
262
|
profileData: profileData || null
|
|
291
|
-
}).onConflictDoNothing({ target: [this.userIdentitiesTable.provider, this.userIdentitiesTable.providerId] })
|
|
263
|
+
}).onConflictDoNothing({ target: [this.userIdentitiesTable.provider, this.userIdentitiesTable.providerId] });
|
|
292
264
|
}
|
|
293
265
|
|
|
294
266
|
async updateUser(id: string, data: Partial<Omit<CreateUserData, "id">>): Promise<UserData | null> {
|
|
@@ -298,20 +270,18 @@ export class UserService implements UserRepository {
|
|
|
298
270
|
const updatedAtKey = getColumnKey(this.usersTable, "updatedAt", "updated_at") || "updatedAt";
|
|
299
271
|
payload[updatedAtKey] = new Date();
|
|
300
272
|
|
|
301
|
-
const [row] = await this.
|
|
302
|
-
(
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
.returning()) as Record<string, unknown>[]
|
|
307
|
-
);
|
|
273
|
+
const [row] = (await this.db
|
|
274
|
+
.update(this.usersTable)
|
|
275
|
+
.set(payload)
|
|
276
|
+
.where(eq(idCol, id))
|
|
277
|
+
.returning()) as Record<string, unknown>[];
|
|
308
278
|
return row ? this.mapRowToUser(row) : null;
|
|
309
279
|
}
|
|
310
280
|
|
|
311
281
|
async deleteUser(id: string): Promise<void> {
|
|
312
282
|
const idCol = getColumn(this.usersTable, "id");
|
|
313
283
|
if (!idCol) return;
|
|
314
|
-
await this.
|
|
284
|
+
await this.db.delete(this.usersTable).where(eq(idCol, id));
|
|
315
285
|
}
|
|
316
286
|
|
|
317
287
|
async listUsers(): Promise<UserData[]> {
|
|
@@ -387,13 +357,13 @@ export class UserService implements UserRepository {
|
|
|
387
357
|
const passwordHashColKey = getColumnKey(this.usersTable, "passwordHash", "password_hash") || "passwordHash";
|
|
388
358
|
const updatedAtColKey = getColumnKey(this.usersTable, "updatedAt", "updated_at") || "updatedAt";
|
|
389
359
|
|
|
390
|
-
await this.
|
|
360
|
+
await this.db
|
|
391
361
|
.update(this.usersTable)
|
|
392
362
|
.set({
|
|
393
363
|
[passwordHashColKey]: passwordHash,
|
|
394
364
|
[updatedAtColKey]: new Date()
|
|
395
365
|
})
|
|
396
|
-
.where(eq(idCol, id))
|
|
366
|
+
.where(eq(idCol, id));
|
|
397
367
|
}
|
|
398
368
|
|
|
399
369
|
/**
|
|
@@ -406,14 +376,14 @@ export class UserService implements UserRepository {
|
|
|
406
376
|
const emailVerificationTokenColKey = getColumnKey(this.usersTable, "emailVerificationToken", "email_verification_token") || "emailVerificationToken";
|
|
407
377
|
const updatedAtColKey = getColumnKey(this.usersTable, "updatedAt", "updated_at") || "updatedAt";
|
|
408
378
|
|
|
409
|
-
await this.
|
|
379
|
+
await this.db
|
|
410
380
|
.update(this.usersTable)
|
|
411
381
|
.set({
|
|
412
382
|
[emailVerifiedColKey]: verified,
|
|
413
383
|
[emailVerificationTokenColKey]: null,
|
|
414
384
|
[updatedAtColKey]: new Date()
|
|
415
385
|
})
|
|
416
|
-
.where(eq(idCol, id))
|
|
386
|
+
.where(eq(idCol, id));
|
|
417
387
|
}
|
|
418
388
|
|
|
419
389
|
/**
|
|
@@ -426,14 +396,14 @@ export class UserService implements UserRepository {
|
|
|
426
396
|
const emailVerificationSentAtColKey = getColumnKey(this.usersTable, "emailVerificationSentAt", "email_verification_sent_at") || "emailVerificationSentAt";
|
|
427
397
|
const updatedAtColKey = getColumnKey(this.usersTable, "updatedAt", "updated_at") || "updatedAt";
|
|
428
398
|
|
|
429
|
-
await this.
|
|
399
|
+
await this.db
|
|
430
400
|
.update(this.usersTable)
|
|
431
401
|
.set({
|
|
432
402
|
[emailVerificationTokenColKey]: token,
|
|
433
403
|
[emailVerificationSentAtColKey]: token ? new Date() : null,
|
|
434
404
|
[updatedAtColKey]: new Date()
|
|
435
405
|
})
|
|
436
|
-
.where(eq(idCol, id))
|
|
406
|
+
.where(eq(idCol, id));
|
|
437
407
|
}
|
|
438
408
|
|
|
439
409
|
/**
|
|
@@ -493,11 +463,11 @@ export class UserService implements UserRepository {
|
|
|
493
463
|
async setUserRoles(userId: string, roleIds: string[]): Promise<void> {
|
|
494
464
|
const usersTableName = this.getQualifiedUsersTableName();
|
|
495
465
|
const rolesArray = `{${roleIds.join(",")}}`;
|
|
496
|
-
await this.
|
|
466
|
+
await this.db.execute(sql`
|
|
497
467
|
UPDATE ${sql.raw(usersTableName)}
|
|
498
468
|
SET roles = ${rolesArray}::text[], updated_at = NOW()
|
|
499
469
|
WHERE id = ${userId}
|
|
500
|
-
`)
|
|
470
|
+
`);
|
|
501
471
|
}
|
|
502
472
|
|
|
503
473
|
/**
|
|
@@ -505,11 +475,11 @@ export class UserService implements UserRepository {
|
|
|
505
475
|
*/
|
|
506
476
|
async assignDefaultRole(userId: string, roleId: string): Promise<void> {
|
|
507
477
|
const usersTableName = this.getQualifiedUsersTableName();
|
|
508
|
-
await this.
|
|
478
|
+
await this.db.execute(sql`
|
|
509
479
|
UPDATE ${sql.raw(usersTableName)}
|
|
510
480
|
SET roles = array_append(roles, ${roleId}), updated_at = NOW()
|
|
511
481
|
WHERE id = ${userId} AND NOT (${roleId} = ANY(roles))
|
|
512
|
-
`)
|
|
482
|
+
`);
|
|
513
483
|
}
|
|
514
484
|
|
|
515
485
|
/**
|
package/src/cli.ts
CHANGED
|
@@ -176,7 +176,6 @@ async function dbCommand(subcommand: string, rawArgs: string[]): Promise<void> {
|
|
|
176
176
|
|
|
177
177
|
if (databaseUrl) {
|
|
178
178
|
await applyPolicies(databaseUrl);
|
|
179
|
-
await reconcilePolicies(databaseUrl, collectionsPath);
|
|
180
179
|
await ensureRlsUserRole(databaseUrl);
|
|
181
180
|
} else {
|
|
182
181
|
logger.warn(chalk.yellow(" ⚠️ DATABASE_URL not found in environment, skipping RLS policies application."));
|
|
@@ -270,65 +269,6 @@ async function applyPolicies(databaseUrl: string): Promise<void> {
|
|
|
270
269
|
}
|
|
271
270
|
}
|
|
272
271
|
|
|
273
|
-
/**
|
|
274
|
-
* Remove the policies an earlier push superseded but never dropped.
|
|
275
|
-
*
|
|
276
|
-
* `policies.sql` only DROPs the names it is about to CREATE, and a rule's
|
|
277
|
-
* generated name contains a hash of its own semantics — so editing a rule
|
|
278
|
-
* writes a *new* policy and abandons the old one. Postgres ORs PERMISSIVE
|
|
279
|
-
* policies together, which makes an abandoned grant outrank every tightening
|
|
280
|
-
* that replaced it, and push reported success the whole time.
|
|
281
|
-
*
|
|
282
|
-
* Runs after `applyPolicies` so the current policies are already in place: the
|
|
283
|
-
* drift check then sees exactly the set that should survive, and anything else
|
|
284
|
-
* on a managed table is by definition left over.
|
|
285
|
-
*/
|
|
286
|
-
async function reconcilePolicies(databaseUrl: string, collectionsPath: string): Promise<void> {
|
|
287
|
-
try {
|
|
288
|
-
const { checkPolicyDrift, dropOrphanedPolicies, formatPolicyDrift, hasDrift } =
|
|
289
|
-
await import("./security/policy-drift");
|
|
290
|
-
const { loadCollections } = await import("./schema/doctor");
|
|
291
|
-
|
|
292
|
-
const collections = await loadCollections(path.resolve(process.cwd(), collectionsPath));
|
|
293
|
-
const { Client } = await import("pg");
|
|
294
|
-
const client = new Client({ connectionString: databaseUrl });
|
|
295
|
-
await client.connect();
|
|
296
|
-
try {
|
|
297
|
-
const drift = await checkPolicyDrift(client as never, collections);
|
|
298
|
-
const { dropped, kept } = await dropOrphanedPolicies(client as never, drift, collections);
|
|
299
|
-
|
|
300
|
-
for (const p of dropped) {
|
|
301
|
-
logger.info(chalk.gray(` ✓ Dropped superseded policy "${p.name}" on ${p.schema}.${p.table}`));
|
|
302
|
-
}
|
|
303
|
-
if (dropped.length > 0) {
|
|
304
|
-
logger.info(chalk.green(` ✓ Removed ${dropped.length} superseded RLS ${dropped.length === 1 ? "policy" : "policies"}.`));
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
// Custom-named orphans are indistinguishable from policies someone
|
|
308
|
-
// wrote in SQL deliberately, so they are reported, never dropped.
|
|
309
|
-
if (kept.length > 0) {
|
|
310
|
-
logger.warn(chalk.yellow(" ⚠️ Policies in the database that no collection describes:"));
|
|
311
|
-
for (const p of kept) {
|
|
312
|
-
logger.warn(chalk.yellow(` • ${p.schema}.${p.table} → "${p.name}" (${p.command} TO ${p.roles.join(", ")})`));
|
|
313
|
-
}
|
|
314
|
-
logger.warn(chalk.yellow(" These still grant access. Drop them by hand if they are stale."));
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
// Missing/diverged are not push's to fix, but staying silent about
|
|
318
|
-
// them is how a database ends up not matching its config.
|
|
319
|
-
const remaining = { ...drift, orphaned: kept };
|
|
320
|
-
if (hasDrift(remaining) && (remaining.missing.length > 0 || remaining.diverged.length > 0)) {
|
|
321
|
-
logger.warn(chalk.yellow(" ⚠️ RLS policies do not match your collections:"));
|
|
322
|
-
logger.warn(formatPolicyDrift({ ...remaining, orphaned: [] }));
|
|
323
|
-
}
|
|
324
|
-
} finally {
|
|
325
|
-
await client.end();
|
|
326
|
-
}
|
|
327
|
-
} catch (err) {
|
|
328
|
-
logger.warn(chalk.yellow(` ⚠️ Could not reconcile RLS policies: ${err instanceof Error ? err.message : String(err)}`));
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
272
|
async function branchCommand(rawArgs: string[]): Promise<void> {
|
|
333
273
|
const branchAction = rawArgs[2]; // create, list, delete, info
|
|
334
274
|
|
package/src/connection.ts
CHANGED
|
@@ -27,68 +27,11 @@ const DEFAULT_POOL: Required<PostgresPoolConfig> = {
|
|
|
27
27
|
max: 20,
|
|
28
28
|
idleTimeoutMillis: 30_000,
|
|
29
29
|
connectionTimeoutMillis: 10_000,
|
|
30
|
-
|
|
31
|
-
// statement_timeout. When the client timer fires first, node-postgres
|
|
32
|
-
// abandons the in-flight statement but keeps the connection — inside a
|
|
33
|
-
// transaction that leaves the tx open (and any pending ROLLBACK is
|
|
34
|
-
// spliced out of the client queue before it ever reaches the wire), so
|
|
35
|
-
// the pooled connection is returned still in-transaction with its RLS
|
|
36
|
-
// GUCs set. The server abort (SQLSTATE 57014) is the clean path; the
|
|
37
|
-
// client timeout is only a backstop for a dead network.
|
|
38
|
-
queryTimeout: 60_000,
|
|
30
|
+
queryTimeout: 30_000,
|
|
39
31
|
statementTimeout: 30_000,
|
|
40
32
|
keepAlive: true
|
|
41
33
|
};
|
|
42
34
|
|
|
43
|
-
/** ReadyForQuery status byte: `I` idle, `T` in transaction, `E` failed transaction. */
|
|
44
|
-
const TX_IDLE = "I";
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Destroy pool clients that are released while still inside a transaction.
|
|
48
|
-
*
|
|
49
|
-
* pg-pool returns a client to the idle list whenever `release()` is called
|
|
50
|
-
* without an error — even if the connection is still mid-transaction (status
|
|
51
|
-
* `T`/`E`). That happens in practice: drizzle's pool transaction releases in
|
|
52
|
-
* a `finally` after attempting ROLLBACK, and if the ROLLBACK itself fails
|
|
53
|
-
* (e.g. it was queued behind a statement that hit the client-side
|
|
54
|
-
* query_timeout), the client goes back dirty. The next checkout then runs
|
|
55
|
-
* its statements inside the zombie transaction — with the previous request's
|
|
56
|
-
* `app.*` RLS GUCs still applied, which turns unrelated queries into
|
|
57
|
-
* RLS-scoped ones (observed in production as registration failing with
|
|
58
|
-
* SQLSTATE 42501 under a leaked anonymous context).
|
|
59
|
-
*
|
|
60
|
-
* pg-pool emits `release` before it consults its private `_expired` set, so
|
|
61
|
-
* marking the client expired here makes `_release()` destroy it instead of
|
|
62
|
-
* pooling it. Both `client._txStatus` (pg ≥ 8.16) and `pool._expired` are
|
|
63
|
-
* private APIs — feature-detect and fall back to loud logging so an upstream
|
|
64
|
-
* change degrades to observability, never to silent corruption.
|
|
65
|
-
*/
|
|
66
|
-
export function guardPoolAgainstDirtyRelease(pool: Pool, label: string): void {
|
|
67
|
-
pool.on("release", (err: Error | undefined, client: unknown) => {
|
|
68
|
-
if (err) return; // errored clients are already destroyed by pg-pool
|
|
69
|
-
const txStatus = (client as { _txStatus?: string | null })?._txStatus;
|
|
70
|
-
if (typeof txStatus !== "string" || txStatus === TX_IDLE) return;
|
|
71
|
-
|
|
72
|
-
// pg-pool keeps expired clients in a WeakSet (a plain object works too
|
|
73
|
-
// if upstream ever changes it — duck-type on add/has).
|
|
74
|
-
const expired = (pool as unknown as { _expired?: { add(c: object): unknown; has(c: object): boolean } })._expired;
|
|
75
|
-
if (expired && typeof expired.add === "function" && typeof expired.has === "function" && client && typeof client === "object") {
|
|
76
|
-
expired.add(client);
|
|
77
|
-
logger.error(
|
|
78
|
-
`[${label}] Client released back to the pool while still in a transaction ` +
|
|
79
|
-
`(status '${txStatus}') — destroying it so the open transaction and its ` +
|
|
80
|
-
`session state (RLS GUCs) cannot leak into the next request.`
|
|
81
|
-
);
|
|
82
|
-
} else {
|
|
83
|
-
logger.error(
|
|
84
|
-
`[${label}] Client released mid-transaction (status '${txStatus}') but the ` +
|
|
85
|
-
`pool's internal expiry set is unavailable (pg-pool internals changed?). ` +
|
|
86
|
-
`The connection may leak its open transaction into subsequent requests.`
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
|
|
92
35
|
/**
|
|
93
36
|
* Create a Drizzle-backed Postgres connection with a production-grade
|
|
94
37
|
* connection pool.
|
|
@@ -132,7 +75,6 @@ export function createPostgresDatabaseConnection(
|
|
|
132
75
|
logger.warn("[pg-pool] Connection timeout detected — pool will auto-retry");
|
|
133
76
|
}
|
|
134
77
|
});
|
|
135
|
-
guardPoolAgainstDirtyRelease(pool, "pg-pool");
|
|
136
78
|
|
|
137
79
|
// Create drizzle instance — pass schema when available to enable db.query relational API
|
|
138
80
|
const db = schema ? drizzle(pool, { schema }) : drizzle(pool);
|
|
@@ -176,7 +118,6 @@ export function createDirectDatabaseConnection(
|
|
|
176
118
|
pool.on("error", (err) => {
|
|
177
119
|
logger.error("[pg-direct-pool] Unexpected pool error", { detail: err.message });
|
|
178
120
|
});
|
|
179
|
-
guardPoolAgainstDirtyRelease(pool, "pg-direct-pool");
|
|
180
121
|
|
|
181
122
|
const db = schema ? drizzle(pool, { schema }) : drizzle(pool);
|
|
182
123
|
|
|
@@ -216,7 +157,6 @@ export function createReadReplicaConnection(
|
|
|
216
157
|
pool.on("error", (err) => {
|
|
217
158
|
logger.error("[pg-replica-pool] Unexpected pool error", { detail: err.message });
|
|
218
159
|
});
|
|
219
|
-
guardPoolAgainstDirtyRelease(pool, "pg-replica-pool");
|
|
220
160
|
|
|
221
161
|
const db = schema ? drizzle(pool, { schema }) : drizzle(pool);
|
|
222
162
|
|
|
@@ -2,7 +2,6 @@ import { Pool } from "pg";
|
|
|
2
2
|
import { drizzle } from "drizzle-orm/node-postgres";
|
|
3
3
|
import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
4
4
|
import { logger } from "@rebasepro/server";
|
|
5
|
-
import { guardPoolAgainstDirtyRelease } from "./connection";
|
|
6
5
|
|
|
7
6
|
export class DatabasePoolManager {
|
|
8
7
|
private pools: Map<string, Pool> = new Map();
|
|
@@ -51,7 +50,6 @@ export class DatabasePoolManager {
|
|
|
51
50
|
pool.on("error", (err) => {
|
|
52
51
|
logger.error(`[DatabasePoolManager] Unexpected error on idle client for db ${databaseName}`, { error: err });
|
|
53
52
|
});
|
|
54
|
-
guardPoolAgainstDirtyRelease(pool, `pg-pool:${databaseName}`);
|
|
55
53
|
|
|
56
54
|
this.pools.set(databaseName, pool);
|
|
57
55
|
return pool;
|
package/src/schema/doctor.ts
CHANGED
|
@@ -37,7 +37,7 @@ export type IssueSeverity = "error" | "warning" | "info";
|
|
|
37
37
|
|
|
38
38
|
export interface DoctorIssue {
|
|
39
39
|
severity: IssueSeverity;
|
|
40
|
-
category: "missing_table" | "missing_column" | "type_mismatch" | "missing_constraint" | "schema_stale" | "missing_enum" | "enum_value_mismatch" | "missing_foreign_key" | "sdk_stale"
|
|
40
|
+
category: "missing_table" | "missing_column" | "type_mismatch" | "missing_constraint" | "schema_stale" | "missing_enum" | "enum_value_mismatch" | "missing_foreign_key" | "sdk_stale";
|
|
41
41
|
table?: string;
|
|
42
42
|
column?: string;
|
|
43
43
|
expected?: string;
|
|
@@ -61,29 +61,19 @@ export function getExpectedColumnType(prop: Property): string | null {
|
|
|
61
61
|
const sp = prop as StringProperty;
|
|
62
62
|
if (sp.enum) return "USER-DEFINED"; // pgEnum → USER-DEFINED in information_schema
|
|
63
63
|
if ("isId" in sp && sp.isId === "uuid") return "uuid";
|
|
64
|
-
if (sp.columnType === "
|
|
65
|
-
// A markdown/multiline string compiles to `text`, not varchar — the
|
|
66
|
-
// generator treats those UI hints as column-type signals, so the
|
|
67
|
-
// expectation here must too or every such column reads as drift.
|
|
68
|
-
if (sp.columnType === "text" || sp.ui?.markdown || sp.ui?.multiline) return "text";
|
|
64
|
+
if (sp.columnType === "text") return "text";
|
|
69
65
|
if (sp.columnType === "char") return "character";
|
|
70
66
|
return "character varying";
|
|
71
67
|
}
|
|
72
68
|
case "number": {
|
|
73
69
|
const np = prop as NumberProperty;
|
|
74
|
-
if (np.columnType)
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
serial: "integer",
|
|
82
|
-
bigserial: "bigint",
|
|
83
|
-
smallserial: "smallint"
|
|
84
|
-
};
|
|
85
|
-
return serialWidths[np.columnType] ?? np.columnType;
|
|
86
|
-
}
|
|
70
|
+
if (np.columnType === "double precision") return "double precision";
|
|
71
|
+
if (np.columnType === "real") return "real";
|
|
72
|
+
if (np.columnType === "bigint") return "bigint";
|
|
73
|
+
if (np.columnType === "serial") return "integer"; // serial is integer under the hood
|
|
74
|
+
if (np.columnType === "bigserial") return "bigint";
|
|
75
|
+
if (np.columnType === "integer") return "integer";
|
|
76
|
+
if (np.columnType === "numeric") return "numeric";
|
|
87
77
|
if (np.validation?.integer || ("isId" in np && np.isId)) return "integer";
|
|
88
78
|
return "numeric";
|
|
89
79
|
}
|
|
@@ -211,18 +201,15 @@ export async function checkCollectionsVsSdk(
|
|
|
211
201
|
): Promise<{ passed: boolean; issues: DoctorIssue[] }> {
|
|
212
202
|
const issues: DoctorIssue[] = [];
|
|
213
203
|
|
|
214
|
-
//
|
|
215
|
-
// you choose to. A project that never generated one isn't drifting, so report
|
|
216
|
-
// it as information rather than a warning; otherwise `doctor` can never come
|
|
217
|
-
// back clean on a fresh project and users learn to ignore its output.
|
|
204
|
+
// Check if SDK file exists
|
|
218
205
|
if (!fs.existsSync(sdkFilePath)) {
|
|
219
206
|
issues.push({
|
|
220
|
-
severity: "
|
|
221
|
-
category: "
|
|
222
|
-
message:
|
|
223
|
-
fix: "Run `rebase generate-sdk`
|
|
207
|
+
severity: "warning",
|
|
208
|
+
category: "sdk_stale",
|
|
209
|
+
message: `Generated SDK typedefs file does not exist at "${sdkFilePath}".`,
|
|
210
|
+
fix: "Run `rebase generate-sdk`"
|
|
224
211
|
});
|
|
225
|
-
return { passed:
|
|
212
|
+
return { passed: false,
|
|
226
213
|
issues };
|
|
227
214
|
}
|
|
228
215
|
|
|
@@ -644,15 +631,11 @@ export function renderReport(report: DoctorReport): void {
|
|
|
644
631
|
}
|
|
645
632
|
|
|
646
633
|
function renderPhase(label: string, passed: boolean, issues: DoctorIssue[]): void {
|
|
647
|
-
|
|
648
|
-
const warnCount = issues.filter((i) => i.severity === "warning").length;
|
|
649
|
-
const infoIssues = issues.filter((i) => i.severity === "info");
|
|
650
|
-
|
|
651
|
-
// Informational notes don't make a phase unhealthy, so key the header off
|
|
652
|
-
// real problems rather than `passed` alone.
|
|
653
|
-
if (errorCount === 0 && warnCount === 0) {
|
|
634
|
+
if (passed) {
|
|
654
635
|
logger.info(` ${chalk.green("✅")} ${label}: ${chalk.green("In sync")}`);
|
|
655
636
|
} else {
|
|
637
|
+
const errorCount = issues.filter((i) => i.severity === "error").length;
|
|
638
|
+
const warnCount = issues.filter((i) => i.severity === "warning").length;
|
|
656
639
|
const parts: string[] = [];
|
|
657
640
|
if (errorCount > 0) parts.push(`${errorCount} error${errorCount > 1 ? "s" : ""}`);
|
|
658
641
|
if (warnCount > 0) parts.push(`${warnCount} warning${warnCount > 1 ? "s" : ""}`);
|
|
@@ -660,14 +643,7 @@ function renderPhase(label: string, passed: boolean, issues: DoctorIssue[]): voi
|
|
|
660
643
|
}
|
|
661
644
|
logger.info("");
|
|
662
645
|
|
|
663
|
-
|
|
664
|
-
for (const issue of infoIssues) {
|
|
665
|
-
const fixPart = issue.fix ? chalk.gray(` — ${issue.fix}`) : "";
|
|
666
|
-
logger.info(` ${chalk.gray("ℹ")} ${chalk.gray(issue.message)}${fixPart}`);
|
|
667
|
-
logger.info("");
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
for (const issue of issues.filter((i) => i.severity !== "info")) {
|
|
646
|
+
for (const issue of issues) {
|
|
671
647
|
const severityIcon = issue.severity === "error" ? chalk.red("✗") : chalk.yellow("⚠");
|
|
672
648
|
const categoryLabel = formatCategory(issue.category);
|
|
673
649
|
logger.info(` ${chalk.gray("┌─")} ${severityIcon} ${chalk.bold(categoryLabel)} ${chalk.gray("─".repeat(Math.max(0, 42 - categoryLabel.length)))}`);
|
|
@@ -698,8 +674,7 @@ function formatCategory(cat: DoctorIssue["category"]): string {
|
|
|
698
674
|
missing_enum: "Missing Enum",
|
|
699
675
|
enum_value_mismatch: "Enum Value Mismatch",
|
|
700
676
|
missing_foreign_key: "Missing Foreign Key",
|
|
701
|
-
sdk_stale: "Stale SDK Types"
|
|
702
|
-
sdk_not_generated: "SDK Types Not Generated"
|
|
677
|
+
sdk_stale: "Stale SDK Types"
|
|
703
678
|
};
|
|
704
679
|
return labels[cat];
|
|
705
680
|
}
|