@rebasepro/server-postgres 0.9.1-canary.a57c262 → 0.9.1-canary.a8dbf1c
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 +21 -0
- package/dist/PostgresBackendDriver.d.ts +18 -0
- package/dist/PostgresBootstrapper.d.ts +17 -1
- package/dist/auth/services.d.ts +68 -52
- package/dist/connection.d.ts +21 -0
- package/dist/index.es.js +914 -226
- package/dist/index.es.js.map +1 -1
- package/dist/schema/auth-bootstrap-sql.d.ts +1 -1
- package/dist/schema/auth-schema.d.ts +24 -24
- package/dist/schema/doctor.d.ts +1 -1
- package/dist/schema/introspect-db-logic.d.ts +0 -5
- package/dist/schema/introspect-db-naming.d.ts +10 -0
- package/dist/security/policy-drift.d.ts +30 -0
- package/dist/security/rls-enforcement.d.ts +2 -2
- package/dist/services/channel-history.d.ts +118 -0
- package/dist/services/realtimeService.d.ts +69 -2
- package/dist/services/row-pipeline.d.ts +5 -2
- package/package.json +8 -31
- package/src/PostgresBackendDriver.ts +74 -10
- package/src/PostgresBootstrapper.ts +69 -3
- package/src/auth/ensure-tables.ts +170 -28
- package/src/auth/services.ts +181 -150
- package/src/cli.ts +60 -0
- package/src/connection.ts +61 -1
- package/src/databasePoolManager.ts +2 -0
- package/src/schema/auth-bootstrap-sql.ts +7 -1
- package/src/schema/auth-schema.ts +13 -13
- package/src/schema/doctor.ts +45 -20
- package/src/schema/introspect-db-inference.ts +1 -1
- package/src/schema/introspect-db-logic.ts +1 -10
- package/src/schema/introspect-db-naming.ts +15 -0
- package/src/schema/introspect-db.ts +11 -1
- package/src/schema/introspect-runtime.ts +1 -1
- package/src/security/policy-drift.test.ts +106 -1
- package/src/security/policy-drift.ts +56 -0
- package/src/security/rls-enforcement.ts +11 -5
- package/src/services/PersistService.ts +9 -2
- package/src/services/channel-history.ts +343 -0
- package/src/services/realtimeService.ts +198 -10
- package/src/services/row-pipeline.ts +70 -7
- package/src/utils/drizzle-conditions.ts +13 -0
- package/src/websocket.ts +30 -11
package/src/cli.ts
CHANGED
|
@@ -176,6 +176,7 @@ 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);
|
|
179
180
|
await ensureRlsUserRole(databaseUrl);
|
|
180
181
|
} else {
|
|
181
182
|
logger.warn(chalk.yellow(" ⚠️ DATABASE_URL not found in environment, skipping RLS policies application."));
|
|
@@ -269,6 +270,65 @@ async function applyPolicies(databaseUrl: string): Promise<void> {
|
|
|
269
270
|
}
|
|
270
271
|
}
|
|
271
272
|
|
|
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
|
+
|
|
272
332
|
async function branchCommand(rawArgs: string[]): Promise<void> {
|
|
273
333
|
const branchAction = rawArgs[2]; // create, list, delete, info
|
|
274
334
|
|
package/src/connection.ts
CHANGED
|
@@ -27,11 +27,68 @@ const DEFAULT_POOL: Required<PostgresPoolConfig> = {
|
|
|
27
27
|
max: 20,
|
|
28
28
|
idleTimeoutMillis: 30_000,
|
|
29
29
|
connectionTimeoutMillis: 10_000,
|
|
30
|
-
|
|
30
|
+
// The client-side read timeout MUST be comfortably above the server-side
|
|
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,
|
|
31
39
|
statementTimeout: 30_000,
|
|
32
40
|
keepAlive: true
|
|
33
41
|
};
|
|
34
42
|
|
|
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
|
+
|
|
35
92
|
/**
|
|
36
93
|
* Create a Drizzle-backed Postgres connection with a production-grade
|
|
37
94
|
* connection pool.
|
|
@@ -75,6 +132,7 @@ export function createPostgresDatabaseConnection(
|
|
|
75
132
|
logger.warn("[pg-pool] Connection timeout detected — pool will auto-retry");
|
|
76
133
|
}
|
|
77
134
|
});
|
|
135
|
+
guardPoolAgainstDirtyRelease(pool, "pg-pool");
|
|
78
136
|
|
|
79
137
|
// Create drizzle instance — pass schema when available to enable db.query relational API
|
|
80
138
|
const db = schema ? drizzle(pool, { schema }) : drizzle(pool);
|
|
@@ -118,6 +176,7 @@ export function createDirectDatabaseConnection(
|
|
|
118
176
|
pool.on("error", (err) => {
|
|
119
177
|
logger.error("[pg-direct-pool] Unexpected pool error", { detail: err.message });
|
|
120
178
|
});
|
|
179
|
+
guardPoolAgainstDirtyRelease(pool, "pg-direct-pool");
|
|
121
180
|
|
|
122
181
|
const db = schema ? drizzle(pool, { schema }) : drizzle(pool);
|
|
123
182
|
|
|
@@ -157,6 +216,7 @@ export function createReadReplicaConnection(
|
|
|
157
216
|
pool.on("error", (err) => {
|
|
158
217
|
logger.error("[pg-replica-pool] Unexpected pool error", { detail: err.message });
|
|
159
218
|
});
|
|
219
|
+
guardPoolAgainstDirtyRelease(pool, "pg-replica-pool");
|
|
160
220
|
|
|
161
221
|
const db = schema ? drizzle(pool, { schema }) : drizzle(pool);
|
|
162
222
|
|
|
@@ -2,6 +2,7 @@ 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";
|
|
5
6
|
|
|
6
7
|
export class DatabasePoolManager {
|
|
7
8
|
private pools: Map<string, Pool> = new Map();
|
|
@@ -50,6 +51,7 @@ export class DatabasePoolManager {
|
|
|
50
51
|
pool.on("error", (err) => {
|
|
51
52
|
logger.error(`[DatabasePoolManager] Unexpected error on idle client for db ${databaseName}`, { error: err });
|
|
52
53
|
});
|
|
54
|
+
guardPoolAgainstDirtyRelease(pool, `pg-pool:${databaseName}`);
|
|
53
55
|
|
|
54
56
|
this.pools.set(databaseName, pool);
|
|
55
57
|
return pool;
|
|
@@ -24,8 +24,14 @@
|
|
|
24
24
|
export const AUTH_BOOTSTRAP_SQL = `-- Auth schema + RLS helper functions (required by the policies below)
|
|
25
25
|
CREATE SCHEMA IF NOT EXISTS auth;
|
|
26
26
|
|
|
27
|
+
-- Falls back to the pre-rename \`app.user_id\` so a database that has taken the
|
|
28
|
+
-- new schema but is still served by an older backend keeps resolving the
|
|
29
|
+
-- principal. Drop the COALESCE once no such deployment remains.
|
|
27
30
|
CREATE OR REPLACE FUNCTION auth.uid() RETURNS text AS $$
|
|
28
|
-
SELECT
|
|
31
|
+
SELECT COALESCE(
|
|
32
|
+
NULLIF(current_setting('app.uid', true), ''),
|
|
33
|
+
NULLIF(current_setting('app.user_id', true), '')
|
|
34
|
+
);
|
|
29
35
|
$$ LANGUAGE sql STABLE;
|
|
30
36
|
|
|
31
37
|
CREATE OR REPLACE FUNCTION auth.jwt() RETURNS jsonb AS $$
|
|
@@ -35,14 +35,14 @@ export function createAuthSchema(usersSchemaName = "rebase") {
|
|
|
35
35
|
*/
|
|
36
36
|
const refreshTokens = tableCreator("refresh_tokens", {
|
|
37
37
|
id: uuid("id").defaultRandom().primaryKey(),
|
|
38
|
-
|
|
38
|
+
uid: uuid("uid").notNull().references(() => users.id, { onDelete: "cascade" }),
|
|
39
39
|
tokenHash: varchar("token_hash", { length: 255 }).notNull().unique(),
|
|
40
40
|
expiresAt: timestamp("expires_at").notNull(),
|
|
41
41
|
userAgent: varchar("user_agent", { length: 500 }),
|
|
42
42
|
ipAddress: varchar("ip_address", { length: 45 }),
|
|
43
43
|
createdAt: timestamp("created_at").defaultNow().notNull()
|
|
44
44
|
}, (table) => ({
|
|
45
|
-
uniqueDeviceSession: unique("unique_device_session").on(table.
|
|
45
|
+
uniqueDeviceSession: unique("unique_device_session").on(table.uid, table.userAgent, table.ipAddress)
|
|
46
46
|
}));
|
|
47
47
|
|
|
48
48
|
/**
|
|
@@ -50,7 +50,7 @@ export function createAuthSchema(usersSchemaName = "rebase") {
|
|
|
50
50
|
*/
|
|
51
51
|
const passwordResetTokens = tableCreator("password_reset_tokens", {
|
|
52
52
|
id: uuid("id").defaultRandom().primaryKey(),
|
|
53
|
-
|
|
53
|
+
uid: uuid("uid").notNull().references(() => users.id, { onDelete: "cascade" }),
|
|
54
54
|
tokenHash: varchar("token_hash", { length: 255 }).notNull().unique(),
|
|
55
55
|
expiresAt: timestamp("expires_at").notNull(),
|
|
56
56
|
usedAt: timestamp("used_at"),
|
|
@@ -71,7 +71,7 @@ export function createAuthSchema(usersSchemaName = "rebase") {
|
|
|
71
71
|
*/
|
|
72
72
|
const userIdentities = tableCreator("user_identities", {
|
|
73
73
|
id: uuid("id").defaultRandom().primaryKey(),
|
|
74
|
-
|
|
74
|
+
uid: uuid("uid").notNull().references(() => users.id, { onDelete: "cascade" }),
|
|
75
75
|
provider: varchar("provider", { length: 50 }).notNull(), // e.g. 'google', 'linkedin'
|
|
76
76
|
providerId: varchar("provider_id", { length: 255 }).notNull(),
|
|
77
77
|
profileData: jsonb("profile_data"),
|
|
@@ -86,7 +86,7 @@ export function createAuthSchema(usersSchemaName = "rebase") {
|
|
|
86
86
|
*/
|
|
87
87
|
const mfaFactors = tableCreator("mfa_factors", {
|
|
88
88
|
id: uuid("id").defaultRandom().primaryKey(),
|
|
89
|
-
|
|
89
|
+
uid: uuid("uid").notNull().references(() => users.id, { onDelete: "cascade" }),
|
|
90
90
|
factorType: varchar("factor_type", { length: 20 }).notNull(), // 'totp'
|
|
91
91
|
secretEncrypted: varchar("secret_encrypted", { length: 500 }).notNull(),
|
|
92
92
|
friendlyName: varchar("friendly_name", { length: 255 }),
|
|
@@ -112,7 +112,7 @@ export function createAuthSchema(usersSchemaName = "rebase") {
|
|
|
112
112
|
*/
|
|
113
113
|
const recoveryCodes = tableCreator("recovery_codes", {
|
|
114
114
|
id: uuid("id").defaultRandom().primaryKey(),
|
|
115
|
-
|
|
115
|
+
uid: uuid("uid").notNull().references(() => users.id, { onDelete: "cascade" }),
|
|
116
116
|
codeHash: varchar("code_hash", { length: 255 }).notNull(),
|
|
117
117
|
usedAt: timestamp("used_at"),
|
|
118
118
|
createdAt: timestamp("created_at").defaultNow().notNull()
|
|
@@ -123,7 +123,7 @@ export function createAuthSchema(usersSchemaName = "rebase") {
|
|
|
123
123
|
*/
|
|
124
124
|
const magicLinkTokens = tableCreator("magic_link_tokens", {
|
|
125
125
|
id: uuid("id").defaultRandom().primaryKey(),
|
|
126
|
-
|
|
126
|
+
uid: uuid("uid").notNull().references(() => users.id, { onDelete: "cascade" }),
|
|
127
127
|
tokenHash: varchar("token_hash", { length: 255 }).notNull().unique(),
|
|
128
128
|
expiresAt: timestamp("expires_at").notNull(),
|
|
129
129
|
usedAt: timestamp("used_at"),
|
|
@@ -171,28 +171,28 @@ export const usersRelations = relations(users, ({ many }) => ({
|
|
|
171
171
|
|
|
172
172
|
export const refreshTokensRelations = relations(refreshTokens, ({ one }) => ({
|
|
173
173
|
user: one(users, {
|
|
174
|
-
fields: [refreshTokens.
|
|
174
|
+
fields: [refreshTokens.uid],
|
|
175
175
|
references: [users.id]
|
|
176
176
|
})
|
|
177
177
|
}));
|
|
178
178
|
|
|
179
179
|
export const passwordResetTokensRelations = relations(passwordResetTokens, ({ one }) => ({
|
|
180
180
|
user: one(users, {
|
|
181
|
-
fields: [passwordResetTokens.
|
|
181
|
+
fields: [passwordResetTokens.uid],
|
|
182
182
|
references: [users.id]
|
|
183
183
|
})
|
|
184
184
|
}));
|
|
185
185
|
|
|
186
186
|
export const userIdentitiesRelations = relations(userIdentities, ({ one }) => ({
|
|
187
187
|
user: one(users, {
|
|
188
|
-
fields: [userIdentities.
|
|
188
|
+
fields: [userIdentities.uid],
|
|
189
189
|
references: [users.id]
|
|
190
190
|
})
|
|
191
191
|
}));
|
|
192
192
|
|
|
193
193
|
export const mfaFactorsRelations = relations(mfaFactors, ({ one, many }) => ({
|
|
194
194
|
user: one(users, {
|
|
195
|
-
fields: [mfaFactors.
|
|
195
|
+
fields: [mfaFactors.uid],
|
|
196
196
|
references: [users.id]
|
|
197
197
|
}),
|
|
198
198
|
challenges: many(mfaChallenges)
|
|
@@ -207,14 +207,14 @@ export const mfaChallengesRelations = relations(mfaChallenges, ({ one }) => ({
|
|
|
207
207
|
|
|
208
208
|
export const recoveryCodesRelations = relations(recoveryCodes, ({ one }) => ({
|
|
209
209
|
user: one(users, {
|
|
210
|
-
fields: [recoveryCodes.
|
|
210
|
+
fields: [recoveryCodes.uid],
|
|
211
211
|
references: [users.id]
|
|
212
212
|
})
|
|
213
213
|
}));
|
|
214
214
|
|
|
215
215
|
export const magicLinkTokensRelations = relations(magicLinkTokens, ({ one }) => ({
|
|
216
216
|
user: one(users, {
|
|
217
|
-
fields: [magicLinkTokens.
|
|
217
|
+
fields: [magicLinkTokens.uid],
|
|
218
218
|
references: [users.id]
|
|
219
219
|
})
|
|
220
220
|
}));
|
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" | "sdk_not_generated";
|
|
41
41
|
table?: string;
|
|
42
42
|
column?: string;
|
|
43
43
|
expected?: string;
|
|
@@ -61,19 +61,29 @@ 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 === "
|
|
64
|
+
if (sp.columnType === "uuid") return "uuid";
|
|
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";
|
|
65
69
|
if (sp.columnType === "char") return "character";
|
|
66
70
|
return "character varying";
|
|
67
71
|
}
|
|
68
72
|
case "number": {
|
|
69
73
|
const np = prop as NumberProperty;
|
|
70
|
-
if (np.columnType
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
74
|
+
if (np.columnType) {
|
|
75
|
+
// The generator passes any columnType straight through to drizzle,
|
|
76
|
+
// so mirror that rather than enumerating a subset (which reported
|
|
77
|
+
// drift for anything unlisted, e.g. smallint). Serial types are
|
|
78
|
+
// integers with a sequence default; information_schema reports the
|
|
79
|
+
// underlying width.
|
|
80
|
+
const serialWidths: Record<string, string> = {
|
|
81
|
+
serial: "integer",
|
|
82
|
+
bigserial: "bigint",
|
|
83
|
+
smallserial: "smallint"
|
|
84
|
+
};
|
|
85
|
+
return serialWidths[np.columnType] ?? np.columnType;
|
|
86
|
+
}
|
|
77
87
|
if (np.validation?.integer || ("isId" in np && np.isId)) return "integer";
|
|
78
88
|
return "numeric";
|
|
79
89
|
}
|
|
@@ -201,15 +211,18 @@ export async function checkCollectionsVsSdk(
|
|
|
201
211
|
): Promise<{ passed: boolean; issues: DoctorIssue[] }> {
|
|
202
212
|
const issues: DoctorIssue[] = [];
|
|
203
213
|
|
|
204
|
-
//
|
|
214
|
+
// The typed SDK is opt-in — nothing in a scaffolded project imports it until
|
|
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.
|
|
205
218
|
if (!fs.existsSync(sdkFilePath)) {
|
|
206
219
|
issues.push({
|
|
207
|
-
severity: "
|
|
208
|
-
category: "
|
|
209
|
-
message:
|
|
210
|
-
fix: "Run `rebase generate-sdk`"
|
|
220
|
+
severity: "info",
|
|
221
|
+
category: "sdk_not_generated",
|
|
222
|
+
message: "Typed SDK not generated (optional).",
|
|
223
|
+
fix: "Run `rebase generate-sdk` if you want typed collection access"
|
|
211
224
|
});
|
|
212
|
-
return { passed:
|
|
225
|
+
return { passed: true,
|
|
213
226
|
issues };
|
|
214
227
|
}
|
|
215
228
|
|
|
@@ -631,11 +644,15 @@ export function renderReport(report: DoctorReport): void {
|
|
|
631
644
|
}
|
|
632
645
|
|
|
633
646
|
function renderPhase(label: string, passed: boolean, issues: DoctorIssue[]): void {
|
|
634
|
-
|
|
647
|
+
const errorCount = issues.filter((i) => i.severity === "error").length;
|
|
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) {
|
|
635
654
|
logger.info(` ${chalk.green("✅")} ${label}: ${chalk.green("In sync")}`);
|
|
636
655
|
} else {
|
|
637
|
-
const errorCount = issues.filter((i) => i.severity === "error").length;
|
|
638
|
-
const warnCount = issues.filter((i) => i.severity === "warning").length;
|
|
639
656
|
const parts: string[] = [];
|
|
640
657
|
if (errorCount > 0) parts.push(`${errorCount} error${errorCount > 1 ? "s" : ""}`);
|
|
641
658
|
if (warnCount > 0) parts.push(`${warnCount} warning${warnCount > 1 ? "s" : ""}`);
|
|
@@ -643,7 +660,14 @@ function renderPhase(label: string, passed: boolean, issues: DoctorIssue[]): voi
|
|
|
643
660
|
}
|
|
644
661
|
logger.info("");
|
|
645
662
|
|
|
646
|
-
|
|
663
|
+
// Notes render as a quiet one-liner, not a full drift box.
|
|
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")) {
|
|
647
671
|
const severityIcon = issue.severity === "error" ? chalk.red("✗") : chalk.yellow("⚠");
|
|
648
672
|
const categoryLabel = formatCategory(issue.category);
|
|
649
673
|
logger.info(` ${chalk.gray("┌─")} ${severityIcon} ${chalk.bold(categoryLabel)} ${chalk.gray("─".repeat(Math.max(0, 42 - categoryLabel.length)))}`);
|
|
@@ -674,7 +698,8 @@ function formatCategory(cat: DoctorIssue["category"]): string {
|
|
|
674
698
|
missing_enum: "Missing Enum",
|
|
675
699
|
enum_value_mismatch: "Enum Value Mismatch",
|
|
676
700
|
missing_foreign_key: "Missing Foreign Key",
|
|
677
|
-
sdk_stale: "Stale SDK Types"
|
|
701
|
+
sdk_stale: "Stale SDK Types",
|
|
702
|
+
sdk_not_generated: "SDK Types Not Generated"
|
|
678
703
|
};
|
|
679
704
|
return labels[cat];
|
|
680
705
|
}
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* and consumed directly by tests.
|
|
8
8
|
*/
|
|
9
9
|
import { inferPropertyFromData } from "./introspect-db-inference";
|
|
10
|
+
import { humanize } from "./introspect-db-naming";
|
|
10
11
|
|
|
11
12
|
// ── Typed interfaces for SQL query results ────────────────────────────
|
|
12
13
|
|
|
@@ -117,16 +118,6 @@ export function singularize(word: string): string {
|
|
|
117
118
|
return word;
|
|
118
119
|
}
|
|
119
120
|
|
|
120
|
-
/**
|
|
121
|
-
* Convert a snake_case name to a human-readable Title Case label.
|
|
122
|
-
* e.g. "created_at" -> "Created At", "customer_id" -> "Customer Id"
|
|
123
|
-
*/
|
|
124
|
-
export function humanize(snakeName: string): string {
|
|
125
|
-
return snakeName
|
|
126
|
-
.replace(/_/g, " ")
|
|
127
|
-
.replace(/\b\w/g, (c) => c.toUpperCase());
|
|
128
|
-
}
|
|
129
|
-
|
|
130
121
|
/**
|
|
131
122
|
* Convert a snake_case table name to a camelCase + "Collection" variable name.
|
|
132
123
|
* e.g. "company_token" -> "companyTokenCollection"
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Naming helpers shared by the introspection modules. These live apart from
|
|
3
|
+
* `introspect-db-logic.ts` because the inference pass needs them too, and
|
|
4
|
+
* importing them from there would close a cycle back through this module.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Convert a snake_case name to a human-readable Title Case label.
|
|
9
|
+
* e.g. "created_at" -> "Created At", "customer_id" -> "Customer Id"
|
|
10
|
+
*/
|
|
11
|
+
export function humanize(snakeName: string): string {
|
|
12
|
+
return snakeName
|
|
13
|
+
.replace(/_/g, " ")
|
|
14
|
+
.replace(/\b\w/g, (c) => c.toUpperCase());
|
|
15
|
+
}
|
|
@@ -243,7 +243,17 @@ async function main() {
|
|
|
243
243
|
const merged = mergeIndexContent(existing, generatedFiles);
|
|
244
244
|
fs.writeFileSync(indexPath, merged, "utf-8");
|
|
245
245
|
} else {
|
|
246
|
-
|
|
246
|
+
// --force replaces collections derived from the database, but the
|
|
247
|
+
// directory can also hold hand-written ones with no table in the
|
|
248
|
+
// introspected schema (the auth users collection lives in
|
|
249
|
+
// "rebase"). The backend discovers the whole directory, so an
|
|
250
|
+
// index listing only introspected tables would silently drop them
|
|
251
|
+
// from the admin UI while the API still served them.
|
|
252
|
+
const siblings = fs.readdirSync(outDir)
|
|
253
|
+
.filter(f => f.endsWith(".ts") && f !== "index.ts")
|
|
254
|
+
.map(f => f.replace(/\.ts$/, ""));
|
|
255
|
+
const allFiles = [...new Set([...generatedFiles, ...siblings])];
|
|
256
|
+
const indexContent = generateIndexContent(allFiles);
|
|
247
257
|
fs.writeFileSync(indexPath, indexContent, "utf-8");
|
|
248
258
|
}
|
|
249
259
|
logger.info(chalk.green(` ✓ ${indexPath}`));
|
|
@@ -23,11 +23,11 @@ import {
|
|
|
23
23
|
buildTablesMap,
|
|
24
24
|
buildEnumMap,
|
|
25
25
|
identifyJoinTables,
|
|
26
|
-
humanize,
|
|
27
26
|
singularize,
|
|
28
27
|
mapPgType,
|
|
29
28
|
getIconForTable
|
|
30
29
|
} from "./introspect-db-logic";
|
|
30
|
+
import { humanize } from "./introspect-db-naming";
|
|
31
31
|
|
|
32
32
|
export interface IntrospectedSchema {
|
|
33
33
|
tablesMap: Map<string, TableMeta>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { describe, expect, it } from "@jest/globals";
|
|
2
2
|
import type { CollectionConfig } from "@rebasepro/types";
|
|
3
3
|
|
|
4
|
-
import { checkPolicyDrift, parseExpectedPolicies, formatPolicyDrift, hasDrift, type PolicyRef, type Queryable } from "./policy-drift";
|
|
4
|
+
import { checkPolicyDrift, parseExpectedPolicies, formatPolicyDrift, hasDrift, dropOrphanedPolicies, isGeneratedPolicyName, type PolicyRef, type Queryable } from "./policy-drift";
|
|
5
5
|
import { generatePostgresPoliciesDdl } from "../schema/generate-postgres-ddl-logic";
|
|
6
6
|
|
|
7
7
|
function collection(slug: string): CollectionConfig {
|
|
@@ -154,3 +154,108 @@ describe("checkPolicyDrift", () => {
|
|
|
154
154
|
expect(drift.missing.length).toBeGreaterThan(0);
|
|
155
155
|
});
|
|
156
156
|
});
|
|
157
|
+
|
|
158
|
+
describe("isGeneratedPolicyName", () => {
|
|
159
|
+
it("recognises the generator's own shape", () => {
|
|
160
|
+
expect(isGeneratedPolicyName("documents_insert_a1b2c3d", "documents")).toBe(true);
|
|
161
|
+
// One rule spanning several operations appends the operation index.
|
|
162
|
+
expect(isGeneratedPolicyName("documents_update_a1b2c3d_1", "documents")).toBe(true);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("does not claim names a human could have written", () => {
|
|
166
|
+
expect(isGeneratedPolicyName("owner_access", "documents")).toBe(false);
|
|
167
|
+
expect(isGeneratedPolicyName("documents_default_admin_read", "documents")).toBe(false);
|
|
168
|
+
// Right shape, wrong table — belongs to something else.
|
|
169
|
+
expect(isGeneratedPolicyName("teams_insert_a1b2c3d", "documents")).toBe(false);
|
|
170
|
+
// A digest is 7 lowercase hex characters, nothing else.
|
|
171
|
+
expect(isGeneratedPolicyName("documents_insert_notahex", "documents")).toBe(false);
|
|
172
|
+
expect(isGeneratedPolicyName("documents_grant_a1b2c3d", "documents")).toBe(false);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
describe("dropOrphanedPolicies", () => {
|
|
177
|
+
/** Records the DDL issued so the test can assert on what was dropped. */
|
|
178
|
+
function recordingDb(rows: Record<string, unknown>[]) {
|
|
179
|
+
const executed: string[] = [];
|
|
180
|
+
const db: Queryable = {
|
|
181
|
+
query: async (text: string) => {
|
|
182
|
+
if (!/^SELECT/i.test(text)) executed.push(text);
|
|
183
|
+
return { rows: rows as never[] };
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
return { db, executed };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
it("drops the policy a rule edit superseded", async () => {
|
|
190
|
+
// The reported failure: tightening a rule renames its policy, and the
|
|
191
|
+
// permissive original stays live and keeps ORing itself back in.
|
|
192
|
+
const cols = [collection("documents")];
|
|
193
|
+
const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
|
|
194
|
+
const stale = {
|
|
195
|
+
schemaname: "public", tablename: "documents", policyname: "documents_insert_dead1ee",
|
|
196
|
+
roles: ["public"], cmd: "INSERT", qual: null, with_check: "true"
|
|
197
|
+
};
|
|
198
|
+
const live = [...expected.map((p) => liveRow(p)), stale];
|
|
199
|
+
|
|
200
|
+
const drift = await checkPolicyDrift(dbWith(live), cols);
|
|
201
|
+
const { db, executed } = recordingDb(live);
|
|
202
|
+
const { dropped, kept } = await dropOrphanedPolicies(db, drift, cols);
|
|
203
|
+
|
|
204
|
+
expect(dropped).toHaveLength(1);
|
|
205
|
+
expect(dropped[0].name).toBe("documents_insert_dead1ee");
|
|
206
|
+
expect(kept).toHaveLength(0);
|
|
207
|
+
expect(executed).toEqual([
|
|
208
|
+
'DROP POLICY IF EXISTS "documents_insert_dead1ee" ON "public"."documents"'
|
|
209
|
+
]);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("leaves a hand-written policy alone and reports it instead", async () => {
|
|
213
|
+
const cols = [collection("documents")];
|
|
214
|
+
const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
|
|
215
|
+
const live = [
|
|
216
|
+
...expected.map((p) => liveRow(p)),
|
|
217
|
+
{ schemaname: "public", tablename: "documents", policyname: "ops_break_glass", roles: ["public"], cmd: "ALL", qual: "true", with_check: null }
|
|
218
|
+
];
|
|
219
|
+
|
|
220
|
+
const drift = await checkPolicyDrift(dbWith(live), cols);
|
|
221
|
+
const { db, executed } = recordingDb(live);
|
|
222
|
+
const { dropped, kept } = await dropOrphanedPolicies(db, drift, cols);
|
|
223
|
+
|
|
224
|
+
expect(dropped).toHaveLength(0);
|
|
225
|
+
expect(kept.map((p) => p.name)).toEqual(["ops_break_glass"]);
|
|
226
|
+
expect(executed).toEqual([]);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("never touches a table the collections do not describe", async () => {
|
|
230
|
+
// Another application sharing the schema owns this table; a
|
|
231
|
+
// generator-shaped name there is coincidence, not our leftover.
|
|
232
|
+
const cols = [collection("documents")];
|
|
233
|
+
const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
|
|
234
|
+
const live = [
|
|
235
|
+
...expected.map((p) => liveRow(p)),
|
|
236
|
+
{ schemaname: "public", tablename: "legacy", policyname: "legacy_select_a1b2c3d", roles: ["public"], cmd: "SELECT", qual: "true", with_check: null }
|
|
237
|
+
];
|
|
238
|
+
|
|
239
|
+
const drift = await checkPolicyDrift(dbWith(live), cols);
|
|
240
|
+
const { db, executed } = recordingDb(live);
|
|
241
|
+
const { dropped, kept } = await dropOrphanedPolicies(db, drift, cols);
|
|
242
|
+
|
|
243
|
+
expect(dropped).toHaveLength(0);
|
|
244
|
+
expect(kept.map((p) => p.name)).toEqual(["legacy_select_a1b2c3d"]);
|
|
245
|
+
expect(executed).toEqual([]);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("does nothing when the database already matches", async () => {
|
|
249
|
+
const cols = [collection("documents")];
|
|
250
|
+
const expected = parseExpectedPolicies(generatePostgresPoliciesDdl(cols));
|
|
251
|
+
const live = expected.map((p) => liveRow(p));
|
|
252
|
+
|
|
253
|
+
const drift = await checkPolicyDrift(dbWith(live), cols);
|
|
254
|
+
const { db, executed } = recordingDb(live);
|
|
255
|
+
const { dropped, kept } = await dropOrphanedPolicies(db, drift, cols);
|
|
256
|
+
|
|
257
|
+
expect(dropped).toHaveLength(0);
|
|
258
|
+
expect(kept).toHaveLength(0);
|
|
259
|
+
expect(executed).toEqual([]);
|
|
260
|
+
});
|
|
261
|
+
});
|