@rebasepro/server-postgres 0.9.1-canary.7dddf96 → 0.9.1-canary.97e305f
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/PostgresBootstrapper.d.ts +10 -0
- package/dist/auth/services.d.ts +16 -0
- package/dist/connection.d.ts +21 -0
- package/dist/index.es.js +455 -243
- package/dist/index.es.js.map +1 -1
- package/dist/schema/doctor.d.ts +1 -1
- package/dist/services/FetchService.d.ts +4 -38
- package/dist/services/RelationService.d.ts +34 -0
- package/dist/services/collection-helpers.d.ts +34 -7
- package/dist/services/row-pipeline.d.ts +63 -0
- package/package.json +10 -7
- package/src/PostgresBackendDriver.ts +18 -5
- package/src/PostgresBootstrapper.ts +51 -2
- package/src/auth/ensure-tables.ts +73 -11
- package/src/auth/services.ts +49 -19
- package/src/connection.ts +61 -1
- package/src/databasePoolManager.ts +2 -0
- package/src/schema/doctor.ts +45 -20
- package/src/schema/introspect-db.ts +19 -2
- package/src/services/FetchService.ts +29 -163
- package/src/services/PersistService.ts +9 -2
- package/src/services/RelationService.ts +153 -93
- package/src/services/collection-helpers.ts +59 -27
- package/src/services/realtimeService.ts +4 -2
- package/src/services/row-pipeline.ts +239 -0
- package/src/utils/drizzle-conditions.ts +13 -0
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" | "sdk_not_generated";
|
|
6
6
|
table?: string;
|
|
7
7
|
column?: string;
|
|
8
8
|
expected?: string;
|
|
@@ -40,37 +40,11 @@ export declare class FetchService {
|
|
|
40
40
|
* the target relation so actual row data is returned.
|
|
41
41
|
*/
|
|
42
42
|
private buildWithConfig;
|
|
43
|
-
/**
|
|
44
|
-
* Detect if a many-to-many relation uses a junction table in the Drizzle schema.
|
|
45
|
-
*/
|
|
46
|
-
private isJunctionRelation;
|
|
47
43
|
/**
|
|
48
44
|
* Get the Drizzle relation name on the junction table that points to the actual target row.
|
|
49
45
|
* For example, for posts_tags junction, this returns "tag_id" (the relation pointing to tags).
|
|
50
46
|
*/
|
|
51
47
|
private getJunctionTargetRelationName;
|
|
52
|
-
/**
|
|
53
|
-
* The address a relation ref points at.
|
|
54
|
-
*
|
|
55
|
-
* The whole key, not its first column: a composite-keyed target addressed
|
|
56
|
-
* by `tenant_id` alone points at every row that shares it. And a target
|
|
57
|
-
* whose key cannot be resolved at all used to throw here — reading
|
|
58
|
-
* `targetPks[0]` of an empty array — taking down the parent's fetch over a
|
|
59
|
-
* relation it may not even have asked for; the first column is a guess, but
|
|
60
|
-
* a ref that resolves to nothing beats no rows at all.
|
|
61
|
-
*/
|
|
62
|
-
private relationTargetAddress;
|
|
63
|
-
/**
|
|
64
|
-
* Convert a db.query result row (with nested relation objects) to a flat row.
|
|
65
|
-
* Handles:
|
|
66
|
-
* - Type normalization (dates, numbers, NaN) via normalizeDbValues
|
|
67
|
-
* - Converting nested relation objects to { id, path, __type: "relation" } for CMS
|
|
68
|
-
* - Flattening junction-table many-to-many results
|
|
69
|
-
*
|
|
70
|
-
* The row's own address is not among them: it is derived by the consumer
|
|
71
|
-
* from the collection's primary keys.
|
|
72
|
-
*/
|
|
73
|
-
private drizzleResultToRow;
|
|
74
48
|
/**
|
|
75
49
|
* Post-fetch joinPath relations for a single flat row.
|
|
76
50
|
* joinPath relations cannot be expressed via Drizzle's `with` config,
|
|
@@ -82,16 +56,6 @@ export declare class FetchService {
|
|
|
82
56
|
* Uses RelationService to query the database and maps results back to the flattened objects.
|
|
83
57
|
*/
|
|
84
58
|
private resolveJoinPathRelationsBatchRest;
|
|
85
|
-
/**
|
|
86
|
-
* Convert a db.query result row to a flat REST-style row with populated relations.
|
|
87
|
-
*
|
|
88
|
-
* Every column is copied through under its own name, with the value Postgres
|
|
89
|
-
* returned. This used to open with a synthesized `id` and then skip the key
|
|
90
|
-
* column, which renamed it (a `sku` primary key was served as `id`, and `sku`
|
|
91
|
-
* did not appear at all) and restringified it (`42` → `"42"`). Consumers that
|
|
92
|
-
* need an address derive it from the collection's primary keys.
|
|
93
|
-
*/
|
|
94
|
-
private drizzleResultToRestRow;
|
|
95
59
|
/**
|
|
96
60
|
* Build db.query-compatible options from standard fetch options.
|
|
97
61
|
* Handles filter, search, orderBy, limit, and cursor-based pagination.
|
|
@@ -122,8 +86,10 @@ export declare class FetchService {
|
|
|
122
86
|
}): Promise<Record<string, unknown>[]>;
|
|
123
87
|
/**
|
|
124
88
|
* Fallback path used when db.query is unavailable.
|
|
125
|
-
*
|
|
126
|
-
*
|
|
89
|
+
*
|
|
90
|
+
* The primary path runs the results through `toCmsRow`, which maps
|
|
91
|
+
* relations from what drizzle already nested — no query per row. This one
|
|
92
|
+
* has no nesting to read, so it resolves relations itself, in batches.
|
|
127
93
|
*
|
|
128
94
|
* Process raw database results into flat rows with relations.
|
|
129
95
|
*/
|
|
@@ -20,6 +20,40 @@ export declare class RelationService {
|
|
|
20
20
|
private db;
|
|
21
21
|
private registry;
|
|
22
22
|
constructor(db: DrizzleClient, registry: PostgresCollectionRegistry);
|
|
23
|
+
/**
|
|
24
|
+
* One target row, as the {@link RelatedRow} everything here returns.
|
|
25
|
+
*
|
|
26
|
+
* Eight sites built this by hand, which is how the address came to be the
|
|
27
|
+
* target's first key column in all eight — one edit, eight places to miss.
|
|
28
|
+
*
|
|
29
|
+
* `resolveNested` is the one thing they did not agree on, and the
|
|
30
|
+
* disagreement was invisible: the single-parent fetches pass `db` and
|
|
31
|
+
* `registry` to `parseDataFromServer`, so the target's *own* relations get
|
|
32
|
+
* resolved too, while the batch paths deliberately do not — a query per
|
|
33
|
+
* target row is the N+1 the batching exists to avoid. Naming the parameter
|
|
34
|
+
* makes that a decision rather than a difference between two call sites
|
|
35
|
+
* nobody was comparing.
|
|
36
|
+
*/
|
|
37
|
+
private toRelatedRow;
|
|
38
|
+
/**
|
|
39
|
+
* A WHERE matching any of `parentIds`, by the whole key.
|
|
40
|
+
*
|
|
41
|
+
* A single key is an `IN (…)`. A composite one cannot be: matching
|
|
42
|
+
* `tenant_id IN (1, 1)` collects every row of tenant 1, so two parents that
|
|
43
|
+
* share their first column each receive the other's relations. It becomes
|
|
44
|
+
* an OR of ANDs — one exact address per parent — which Postgres indexes the
|
|
45
|
+
* same way it would a multi-column key lookup.
|
|
46
|
+
*/
|
|
47
|
+
private parentKeyCondition;
|
|
48
|
+
/**
|
|
49
|
+
* Reject a relation that cannot express a composite-keyed parent.
|
|
50
|
+
*
|
|
51
|
+
* `localKey` and `foreignKeyOnTarget` are single column names: one column
|
|
52
|
+
* cannot reference a two-column key, so such a relation has no correct
|
|
53
|
+
* reading. Left alone it would silently match on the first key column and
|
|
54
|
+
* hand a tenant's rows to its neighbour — say so instead.
|
|
55
|
+
*/
|
|
56
|
+
private assertSingleKeyAddressable;
|
|
23
57
|
/**
|
|
24
58
|
* Fetch rows related to a parent row through a specific relation
|
|
25
59
|
*/
|
|
@@ -24,7 +24,37 @@ export interface DrizzleColumnMeta {
|
|
|
24
24
|
export declare function getColumnMeta(col: AnyPgColumn): DrizzleColumnMeta;
|
|
25
25
|
export declare function getCollectionByPath(collectionPath: string, registry: PostgresCollectionRegistry): CollectionConfig;
|
|
26
26
|
export declare function getTableForCollection(collection: CollectionConfig, registry: PostgresCollectionRegistry): PgTable<any>;
|
|
27
|
+
/**
|
|
28
|
+
* The key columns a collection's rows are addressed by.
|
|
29
|
+
*
|
|
30
|
+
* Three tiers, in order: properties marked `isId`, the primary keys of the
|
|
31
|
+
* drizzle schema, and finally a column literally named `id`. Only the first is
|
|
32
|
+
* visible to the browser, which is why a key known only to drizzle is reported
|
|
33
|
+
* at boot — see {@link warnOnKeysTheAdminCannotResolve}.
|
|
34
|
+
*
|
|
35
|
+
* Returns `[]` when nothing resolves, rather than throwing. It used to open by
|
|
36
|
+
* resolving the table, which throws when there is none — so the `isId` tier,
|
|
37
|
+
* which needs no table at all, was unreachable for exactly the collections
|
|
38
|
+
* most likely to have no table registered. Every caller that wanted "no keys"
|
|
39
|
+
* to mean "no keys" had to spell that out in a try/catch.
|
|
40
|
+
*
|
|
41
|
+
* Callers that cannot proceed without a key must say so themselves, naming the
|
|
42
|
+
* collection: an empty array here means "this collection has no address", which
|
|
43
|
+
* is a different answer in a notification (broadcast a wildcard) than in a save
|
|
44
|
+
* (fail).
|
|
45
|
+
*/
|
|
27
46
|
export declare function getPrimaryKeys(collection: CollectionConfig, registry: PostgresCollectionRegistry): PrimaryKeyInfo[];
|
|
47
|
+
/**
|
|
48
|
+
* The key columns, for callers that cannot do their job without one.
|
|
49
|
+
*
|
|
50
|
+
* {@link getPrimaryKeys} answers "what keys, if any" and returns `[]` for a
|
|
51
|
+
* collection with no address. Most of this driver, though, is building a WHERE
|
|
52
|
+
* clause and has no meaning without a key — for those, an empty array is not an
|
|
53
|
+
* answer, and indexing `[0]` into it produces `Cannot read properties of
|
|
54
|
+
* undefined` three frames from where the real problem is. This says what is
|
|
55
|
+
* wrong and which collection it is wrong about.
|
|
56
|
+
*/
|
|
57
|
+
export declare function requirePrimaryKeys(collection: CollectionConfig, registry: PostgresCollectionRegistry): PrimaryKeyInfo[];
|
|
28
58
|
/**
|
|
29
59
|
* Collections whose key the *browser* cannot resolve, and what it will do
|
|
30
60
|
* instead.
|
|
@@ -65,12 +95,9 @@ export declare function warnOnKeysTheAdminCannotResolve(collections: CollectionC
|
|
|
65
95
|
* The address of a row: derived from the collection's primary keys, because a
|
|
66
96
|
* row does not carry one — it is exactly its columns.
|
|
67
97
|
*
|
|
68
|
-
* Falls back to a literal `id` column,
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* (
|
|
72
|
-
* nothing). Returns `""` when there is no key and no `id` — callers decide what
|
|
73
|
-
* that means, since "unaddressable" is a different answer in a notification
|
|
74
|
-
* (broadcast a wildcard) than in a save (fail).
|
|
98
|
+
* Falls back to a literal `id` column, for a row that reached us from somewhere
|
|
99
|
+
* other than this driver. Returns `""` when there is no key and no `id` —
|
|
100
|
+
* callers decide what that means, since "unaddressable" is a different answer
|
|
101
|
+
* in a notification (broadcast a wildcard) than in a save (fail).
|
|
75
102
|
*/
|
|
76
103
|
export declare function deriveRowAddress(row: Record<string, unknown>, collection: CollectionConfig, registry: PostgresCollectionRegistry): string;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { CollectionConfig, Relation } from "@rebasepro/types";
|
|
2
|
+
import { PostgresCollectionRegistry } from "../collections/PostgresCollectionRegistry";
|
|
3
|
+
/**
|
|
4
|
+
* Turning a drizzle result into a row we serve.
|
|
5
|
+
*
|
|
6
|
+
* There are two shapes, and they are the same walk. Both take a row whose
|
|
7
|
+
* relation fields hold nested objects, both unwrap junction rows to reach the
|
|
8
|
+
* target behind them, and both leave every other column alone. They differ only
|
|
9
|
+
* in what they put where the relation was:
|
|
10
|
+
*
|
|
11
|
+
* - `"ref"` — a `{ id, path, __type: "relation" }` reference carrying the
|
|
12
|
+
* target's values. This is what the admin renders.
|
|
13
|
+
* - `"inline"` — the target's own columns, flat. This is what REST serves.
|
|
14
|
+
*
|
|
15
|
+
* They used to be two functions that happened to agree, and the agreement was
|
|
16
|
+
* not enforced by anything: the row-identity bug had to be fixed five times
|
|
17
|
+
* across differently-shaped copies of this walk, and one of the copies was
|
|
18
|
+
* dead code nobody had noticed. Whatever the next cross-cutting change is, it
|
|
19
|
+
* is one edit here.
|
|
20
|
+
*/
|
|
21
|
+
export type RelationStyle = "ref" | "inline";
|
|
22
|
+
/**
|
|
23
|
+
* Whether a many-relation reaches its target through a junction table.
|
|
24
|
+
*
|
|
25
|
+
* Also used to build the drizzle `with` config, which is why it is exported:
|
|
26
|
+
* the query has to nest one level deeper for a junction, and the row walk has
|
|
27
|
+
* to unwrap that same level back out.
|
|
28
|
+
*/
|
|
29
|
+
export declare function isJunctionRelation(relation: Relation): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* The address a relation ref points at.
|
|
32
|
+
*
|
|
33
|
+
* The whole key, not its first column: a composite-keyed target addressed by
|
|
34
|
+
* `tenant_id` alone points at every row that shares it. A target whose key
|
|
35
|
+
* cannot be resolved at all used to throw here — reading `[0]` of an empty
|
|
36
|
+
* array — taking down the parent's fetch over a relation it may not even have
|
|
37
|
+
* asked for. The first column is a guess, but a ref that resolves to nothing
|
|
38
|
+
* beats no rows at all.
|
|
39
|
+
*/
|
|
40
|
+
export declare function relationTargetAddress(targetRow: Record<string, unknown>, targetCollection: CollectionConfig, registry: PostgresCollectionRegistry): string;
|
|
41
|
+
/**
|
|
42
|
+
* The row the admin renders: every column, with relations as references.
|
|
43
|
+
*
|
|
44
|
+
* Values are normalized (dates, numbers, NaN) because the admin's view-model
|
|
45
|
+
* expects real types. The row's own address is *not* among the columns — it is
|
|
46
|
+
* derived by the consumer from the collection's primary keys.
|
|
47
|
+
*/
|
|
48
|
+
export declare function toCmsRow(row: Record<string, unknown>, collection: CollectionConfig, registry: PostgresCollectionRegistry): Record<string, unknown>;
|
|
49
|
+
/**
|
|
50
|
+
* The row REST serves: every column under its own name, with the value Postgres
|
|
51
|
+
* returned, and relations inlined as the target's columns.
|
|
52
|
+
*
|
|
53
|
+
* Values are the ones the database returned, except where that contradicts the
|
|
54
|
+
* declared type: a `number` property is served as a number (see
|
|
55
|
+
* {@link coerceDeclaredNumber}). Dates stay as the database returned them —
|
|
56
|
+
* JSON has its own opinions about dates that the admin's view-model does not
|
|
57
|
+
* share.
|
|
58
|
+
*
|
|
59
|
+
* Keyed by the row rather than by the relation list — a REST fetch only loads
|
|
60
|
+
* the relations `include` asked for, so the row is the authority on which are
|
|
61
|
+
* actually there.
|
|
62
|
+
*/
|
|
63
|
+
export declare function toRestRow(row: Record<string, unknown>, collection: CollectionConfig, registry: PostgresCollectionRegistry): Record<string, unknown>;
|
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.97e305f",
|
|
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,18 +71,20 @@
|
|
|
71
71
|
"execa": "^9.6.1",
|
|
72
72
|
"pg": "^8.21.0",
|
|
73
73
|
"ws": "^8.21.0",
|
|
74
|
-
"@rebasepro/
|
|
75
|
-
"@rebasepro/
|
|
76
|
-
"@rebasepro/server": "0.9.1-canary.
|
|
77
|
-
"@rebasepro/types": "0.9.1-canary.
|
|
78
|
-
"@rebasepro/utils": "0.9.1-canary.
|
|
74
|
+
"@rebasepro/codegen": "0.9.1-canary.97e305f",
|
|
75
|
+
"@rebasepro/common": "0.9.1-canary.97e305f",
|
|
76
|
+
"@rebasepro/server": "0.9.1-canary.97e305f",
|
|
77
|
+
"@rebasepro/types": "0.9.1-canary.97e305f",
|
|
78
|
+
"@rebasepro/utils": "0.9.1-canary.97e305f"
|
|
79
79
|
},
|
|
80
80
|
"devDependencies": {
|
|
81
|
+
"@hono/node-server": "^2.0.9",
|
|
81
82
|
"@types/jest": "^30.0.0",
|
|
82
83
|
"@types/node": "^25.9.3",
|
|
83
84
|
"@types/pg": "^8.20.0",
|
|
84
85
|
"@types/ws": "^8.18.1",
|
|
85
86
|
"@vitejs/plugin-react": "^6.0.2",
|
|
87
|
+
"hono": "^4.12.25",
|
|
86
88
|
"jest": "^30.4.2",
|
|
87
89
|
"ts-jest": "^29.4.11",
|
|
88
90
|
"typescript": "^6.0.3",
|
|
@@ -103,6 +105,7 @@
|
|
|
103
105
|
"test:lint": "eslint \"src/**\" --quiet",
|
|
104
106
|
"test": "jest --passWithNoTests",
|
|
105
107
|
"test:e2e": "vitest run --config vitest.e2e.config.ts",
|
|
106
|
-
"clean": "rm -rf dist && find ./src -name '*.js' -type f | xargs rm -f"
|
|
108
|
+
"clean": "rm -rf dist && find ./src -name '*.js' -type f | xargs rm -f",
|
|
109
|
+
"smoke:baas": "tsx scripts/smoke-baas.ts"
|
|
107
110
|
}
|
|
108
111
|
}
|
|
@@ -548,13 +548,26 @@ export class PostgresBackendDriver implements DataDriver {
|
|
|
548
548
|
let updatedValues = values;
|
|
549
549
|
const contextForCallback = this.buildCallContext();
|
|
550
550
|
|
|
551
|
-
// Fetch previous values for callbacks AND history recording
|
|
551
|
+
// Fetch previous values for callbacks AND history recording. Same walk
|
|
552
|
+
// as the saved row the callbacks receive (`fetchOneForRest`), so
|
|
553
|
+
// `values` and `previousValues` compare like with like — a Date on one
|
|
554
|
+
// side and its ISO string on the other reads as a change that never
|
|
555
|
+
// happened.
|
|
552
556
|
let previousValuesForHistory: Partial<M> | undefined;
|
|
553
557
|
if (status === "existing" && id) {
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
+
try {
|
|
559
|
+
const existing = await this.dataService.getFetchService()
|
|
560
|
+
.fetchOneForRest(path, id, undefined, resolvedCollection?.databaseId);
|
|
561
|
+
if (existing) {
|
|
562
|
+
const { id: _existingId, ...existingValues } = existing;
|
|
563
|
+
previousValuesForHistory = existingValues as Partial<M>;
|
|
564
|
+
}
|
|
565
|
+
} catch (err) {
|
|
566
|
+
// Best-effort enrichment: callbacks and history run without
|
|
567
|
+
// previous values rather than the save failing on a read the
|
|
568
|
+
// write itself does not need (e.g. a collection whose key the
|
|
569
|
+
// registry cannot resolve).
|
|
570
|
+
logger.debug(`[save] Could not fetch previous values for "${path}"`, { detail: err instanceof Error ? err.message : String(err) });
|
|
558
571
|
}
|
|
559
572
|
}
|
|
560
573
|
|
|
@@ -65,6 +65,15 @@ export interface PostgresDriverInternals {
|
|
|
65
65
|
realtimeService: RealtimeService;
|
|
66
66
|
driver: PostgresBackendDriver;
|
|
67
67
|
poolManager?: DatabasePoolManager;
|
|
68
|
+
/**
|
|
69
|
+
* Attach CDC triggers to tables that did not exist when the driver
|
|
70
|
+
* bootstrapped. Only set when database-level capture is actually active.
|
|
71
|
+
*
|
|
72
|
+
* Auth owns its own tables and creates them later in boot, so at driver
|
|
73
|
+
* bootstrap they are legitimately missing and get skipped; without this
|
|
74
|
+
* they would stay uninstrumented until the next restart.
|
|
75
|
+
*/
|
|
76
|
+
provisionCdcForTables?: (tables: CdcTableRef[]) => Promise<void>;
|
|
68
77
|
}
|
|
69
78
|
|
|
70
79
|
// Re-export from shared CLI error utilities
|
|
@@ -334,6 +343,7 @@ export function createPostgresBootstrapper(pgConfig: PostgresDriverConfig): Back
|
|
|
334
343
|
const wantsCdc = cdcMode !== "off";
|
|
335
344
|
const explicitCdc = cdcMode === "trigger" || cdcMode === "wal";
|
|
336
345
|
let cdcEnabled = false;
|
|
346
|
+
let provisionCdcForTables: PostgresDriverInternals["provisionCdcForTables"];
|
|
337
347
|
|
|
338
348
|
if (wantsCdc && !directUrl) {
|
|
339
349
|
const reason = "no direct database connection is available for the realtime LISTEN client (set DATABASE_DIRECT_URL)";
|
|
@@ -367,6 +377,11 @@ export function createPostgresBootstrapper(pgConfig: PostgresDriverConfig): Back
|
|
|
367
377
|
await provisionTriggerCdc(cdcRunSql, cdcTables);
|
|
368
378
|
await realtimeService.enableCdc(directUrl);
|
|
369
379
|
cdcEnabled = true;
|
|
380
|
+
// Boot steps that create their own tables (auth) run after
|
|
381
|
+
// this one and use it to instrument what they just created.
|
|
382
|
+
provisionCdcForTables = async (tables) => {
|
|
383
|
+
await provisionTriggerCdc(cdcRunSql, tables);
|
|
384
|
+
};
|
|
370
385
|
logger.info(
|
|
371
386
|
`📡 [CDC] Realtime source = database-level change capture (mode: ${cdcMode === "wal" ? "wal→trigger" : "trigger"}). ` +
|
|
372
387
|
`All writes now emit realtime events regardless of origin.`
|
|
@@ -417,6 +432,14 @@ export function createPostgresBootstrapper(pgConfig: PostgresDriverConfig): Back
|
|
|
417
432
|
);
|
|
418
433
|
const missing: Array<{ slug: string; table: string }> = [];
|
|
419
434
|
for (const col of registeredCollections) {
|
|
435
|
+
// Auth owns its table and creates it later in this same
|
|
436
|
+
// boot (initializeAuth → ensureAuthTablesExist), so it is
|
|
437
|
+
// legitimately absent right now. Reporting it as drift
|
|
438
|
+
// tells the user to `db:push` a table that is about to
|
|
439
|
+
// exist — and on an introspected database, one that the
|
|
440
|
+
// database was never supposed to hold.
|
|
441
|
+
if ((col as { auth?: { enabled?: boolean } }).auth?.enabled) continue;
|
|
442
|
+
|
|
420
443
|
const schemaName = "schema" in col && col.schema ? col.schema : "public";
|
|
421
444
|
const tableName = registry.hasTableForCollection(
|
|
422
445
|
col.table ?? col.slug
|
|
@@ -431,8 +454,10 @@ export function createPostgresBootstrapper(pgConfig: PostgresDriverConfig): Back
|
|
|
431
454
|
const checkName = resolvedTable ?? tableName;
|
|
432
455
|
const fullCheckName = schemaName === "public" ? checkName : `${schemaName}.${checkName}`;
|
|
433
456
|
if (!dbTables.has(fullCheckName)) {
|
|
457
|
+
// Report what was actually looked up: an unqualified
|
|
458
|
+
// "users" sends people hunting for public.users.
|
|
434
459
|
missing.push({ slug: col.slug,
|
|
435
|
-
table:
|
|
460
|
+
table: fullCheckName });
|
|
436
461
|
}
|
|
437
462
|
}
|
|
438
463
|
if (missing.length > 0) {
|
|
@@ -466,7 +491,8 @@ table: checkName });
|
|
|
466
491
|
registry,
|
|
467
492
|
realtimeService,
|
|
468
493
|
driver,
|
|
469
|
-
poolManager
|
|
494
|
+
poolManager,
|
|
495
|
+
provisionCdcForTables
|
|
470
496
|
};
|
|
471
497
|
|
|
472
498
|
return {
|
|
@@ -495,6 +521,29 @@ table: checkName });
|
|
|
495
521
|
// ensureAuthTablesExist works with the collection abstraction — no Drizzle leakage.
|
|
496
522
|
await ensureAuthTablesExist(db, authCollection);
|
|
497
523
|
|
|
524
|
+
// The driver bootstrapped before these tables existed, so CDC skipped
|
|
525
|
+
// them. Instrument them now, or writes to the user table emit no
|
|
526
|
+
// realtime events until the next restart.
|
|
527
|
+
if (authCollection && internals.provisionCdcForTables) {
|
|
528
|
+
const authSchema = "schema" in authCollection && typeof authCollection.schema === "string"
|
|
529
|
+
? authCollection.schema
|
|
530
|
+
: "rebase";
|
|
531
|
+
const authTable = "table" in authCollection && typeof authCollection.table === "string"
|
|
532
|
+
? authCollection.table
|
|
533
|
+
: authCollection.slug;
|
|
534
|
+
if (authTable) {
|
|
535
|
+
try {
|
|
536
|
+
await internals.provisionCdcForTables([{ schema: authSchema, table: authTable }]);
|
|
537
|
+
} catch (err) {
|
|
538
|
+
logger.warn(
|
|
539
|
+
`⚠️ [CDC] Could not attach change-capture to the auth table "${authSchema}.${authTable}" — ` +
|
|
540
|
+
"writes to it won't emit database-level events.",
|
|
541
|
+
{ detail: err instanceof Error ? err.message : String(err) }
|
|
542
|
+
);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
498
547
|
let emailService: EmailService | undefined;
|
|
499
548
|
if (authConfig.email) {
|
|
500
549
|
emailService = createEmailService(authConfig.email as EmailConfig);
|
|
@@ -251,17 +251,32 @@ 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
|
-
|
|
254
|
+
// ── Migration: reconcile the full users column set (safe for existing tables) ──
|
|
255
|
+
// CREATE TABLE IF NOT EXISTS never revisits an existing table, so a
|
|
256
|
+
// database provisioned by an older framework era is missing every
|
|
257
|
+
// column added since. Each column the auth services read or write must
|
|
258
|
+
// be back-filled here, or upgraded deployments break on the first
|
|
259
|
+
// statement that references it. `email` is deliberately absent: it has
|
|
260
|
+
// existed since the first era and cannot be added NOT NULL safely.
|
|
261
|
+
const userColumnBackfills = [
|
|
262
|
+
"display_name VARCHAR(255)",
|
|
263
|
+
"photo_url VARCHAR(500)",
|
|
264
|
+
"roles TEXT[] DEFAULT '{}' NOT NULL",
|
|
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
|
+
}
|
|
265
280
|
|
|
266
281
|
// ── Migration: Copy roles from legacy junction table to inline column ──
|
|
267
282
|
// If the old rebase.user_roles and rebase.roles tables exist, migrate
|
|
@@ -358,6 +373,53 @@ export async function ensureAuthTablesExist(db: NodePgDatabase, collection?: Col
|
|
|
358
373
|
ON ${sql.raw(recoveryCodesTableName)}(user_id)
|
|
359
374
|
`);
|
|
360
375
|
|
|
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
|
+
|
|
361
423
|
logger.info("✅ Auth tables ready");
|
|
362
424
|
} catch (error) {
|
|
363
425
|
logger.error("❌ Failed to create auth tables", { error });
|