@rebasepro/server-postgres 0.10.0 → 0.10.1-canary.0a881d4
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 +7 -3
- package/dist/auth/services.d.ts +43 -4
- package/dist/backup/backup-logic.d.ts +23 -0
- package/dist/backup/backup-service.d.ts +44 -2
- package/dist/backup/pg-tools.d.ts +41 -1
- package/dist/chunk-DSJWtz9O.js +40 -0
- package/dist/cli-helpers.d.ts +33 -1
- package/dist/ensure-collection-tables-C9gy4STB.js +304 -0
- package/dist/ensure-collection-tables-C9gy4STB.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.es.js +1480 -4648
- package/dist/index.es.js.map +1 -1
- package/dist/schema/auth-schema.d.ts +170 -0
- package/dist/schema/destructive-sql.d.ts +49 -0
- package/dist/schema/ensure-collection-tables.d.ts +79 -0
- package/dist/schema/generate-postgres-ddl-logic.d.ts +4 -1
- package/dist/services/cdc/CdcListener.d.ts +7 -14
- package/dist/services/channel-bus/ChannelBus.d.ts +29 -0
- package/dist/services/channel-bus/PostgresChannelBus.d.ts +111 -0
- package/dist/services/channel-bus/index.d.ts +55 -0
- package/dist/services/channel-history.d.ts +11 -0
- package/dist/services/channel-presence.d.ts +66 -0
- package/dist/services/pg-notify-listener.d.ts +47 -0
- package/dist/services/realtimeService.d.ts +114 -6
- package/dist/src-CBgtrPhJ.js +336 -0
- package/dist/src-CBgtrPhJ.js.map +1 -0
- package/dist/src-DG6ZsQQ3.js +4026 -0
- package/dist/src-DG6ZsQQ3.js.map +1 -0
- package/package.json +8 -9
- package/src/PostgresBootstrapper.ts +72 -3
- package/src/auth/ensure-tables.ts +91 -3
- package/src/auth/services.ts +186 -48
- package/src/backup/backup-cli.ts +60 -1
- package/src/backup/backup-cron.ts +24 -1
- package/src/backup/backup-logic.ts +62 -0
- package/src/backup/backup-service.ts +132 -13
- package/src/backup/pg-tools.ts +70 -2
- package/src/cli-helpers.ts +82 -27
- package/src/cli.ts +152 -6
- package/src/index.ts +4 -0
- package/src/schema/auth-schema.ts +41 -3
- package/src/schema/destructive-sql.ts +94 -0
- package/src/schema/doctor.ts +6 -6
- package/src/schema/ensure-collection-tables.test.ts +156 -0
- package/src/schema/ensure-collection-tables.ts +297 -0
- package/src/schema/generate-drizzle-schema-logic.ts +13 -9
- package/src/schema/generate-postgres-ddl-logic.ts +22 -15
- package/src/schema/introspect-db-inference.ts +13 -13
- package/src/schema/introspect-db-logic.ts +6 -6
- package/src/services/cdc/CdcListener.ts +27 -91
- package/src/services/channel-bus/ChannelBus.ts +44 -0
- package/src/services/channel-bus/PostgresChannelBus.ts +299 -0
- package/src/services/channel-bus/index.ts +123 -0
- package/src/services/channel-history.ts +35 -0
- package/src/services/channel-presence.ts +148 -0
- package/src/services/pg-notify-listener.ts +137 -0
- package/src/services/realtimeService.ts +383 -14
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { describe, expect, it } from "@jest/globals";
|
|
2
|
+
import type { CollectionConfig } from "@rebasepro/types";
|
|
3
|
+
import {
|
|
4
|
+
planCollectionSchemaEnsure,
|
|
5
|
+
ensureCollectionTables,
|
|
6
|
+
type ExistingSchema,
|
|
7
|
+
type Queryable
|
|
8
|
+
} from "./ensure-collection-tables";
|
|
9
|
+
|
|
10
|
+
const posts = {
|
|
11
|
+
name: "Posts",
|
|
12
|
+
slug: "posts",
|
|
13
|
+
properties: {
|
|
14
|
+
id: { name: "ID", type: "string", isId: "uuid" },
|
|
15
|
+
title: { name: "Title", type: "string" },
|
|
16
|
+
views: { name: "Views", type: "number" },
|
|
17
|
+
status: {
|
|
18
|
+
name: "Status",
|
|
19
|
+
type: "string",
|
|
20
|
+
enum: [
|
|
21
|
+
{ id: "draft", label: "Draft" },
|
|
22
|
+
{ id: "published", label: "Published" }
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
} as unknown as CollectionConfig;
|
|
27
|
+
|
|
28
|
+
const empty = (): ExistingSchema => ({ tables: new Map(), enums: new Set() });
|
|
29
|
+
|
|
30
|
+
const withTable = (key: string, columns: string[]): ExistingSchema => ({
|
|
31
|
+
tables: new Map([[key, new Set(columns)]]),
|
|
32
|
+
enums: new Set()
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("planning an additive schema ensure", () => {
|
|
36
|
+
it("creates a missing table, its enum type, and its columns in that order", () => {
|
|
37
|
+
const plan = planCollectionSchemaEnsure([posts], empty());
|
|
38
|
+
const kinds = plan.actions.map(a => a.kind);
|
|
39
|
+
|
|
40
|
+
// The enum must exist before the column that references it, and the
|
|
41
|
+
// table before its own columns.
|
|
42
|
+
expect(kinds.indexOf("create-enum")).toBeLessThan(kinds.indexOf("create-table"));
|
|
43
|
+
expect(kinds.indexOf("create-table")).toBeLessThan(kinds.indexOf("add-column"));
|
|
44
|
+
expect(plan.statements.join("\n")).toMatch(/CREATE TABLE IF NOT EXISTS "public"\."posts"/);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("adds only the columns an existing table is missing", () => {
|
|
48
|
+
const plan = planCollectionSchemaEnsure([posts], withTable("public.posts", ["id", "title"]));
|
|
49
|
+
const added = plan.actions.filter(a => a.kind === "add-column").map(a => a.target);
|
|
50
|
+
|
|
51
|
+
expect(added).toContain("public.posts.views");
|
|
52
|
+
expect(added).toContain("public.posts.status");
|
|
53
|
+
expect(added).not.toContain("public.posts.title");
|
|
54
|
+
expect(plan.actions.some(a => a.kind === "create-table")).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("is a no-op against a database that is already current", () => {
|
|
58
|
+
const existing: ExistingSchema = {
|
|
59
|
+
tables: new Map([["public.posts", new Set(["id", "title", "views", "status"])]]),
|
|
60
|
+
enums: new Set(["public.posts_status"])
|
|
61
|
+
};
|
|
62
|
+
expect(planCollectionSchemaEnsure([posts], existing).actions).toEqual([]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("skips an enum type that already exists, since CREATE TYPE has no IF NOT EXISTS", () => {
|
|
66
|
+
const existing: ExistingSchema = { tables: new Map(), enums: new Set(["public.posts_status"]) };
|
|
67
|
+
const plan = planCollectionSchemaEnsure([posts], existing);
|
|
68
|
+
expect(plan.actions.some(a => a.kind === "create-enum")).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("NEVER emits a destructive statement, whatever the database contains", () => {
|
|
72
|
+
// The core safety property. This runs unattended against customer data
|
|
73
|
+
// with nobody reading a diff, so a column the collections no longer
|
|
74
|
+
// mention must be left alone, not dropped.
|
|
75
|
+
const existing = withTable("public.posts", ["id", "title", "legacy_column", "another_old_one"]);
|
|
76
|
+
const sql = planCollectionSchemaEnsure([posts], existing).statements.join("\n");
|
|
77
|
+
|
|
78
|
+
expect(sql).not.toMatch(/\bDROP\b/i);
|
|
79
|
+
expect(sql).not.toMatch(/\bTRUNCATE\b/i);
|
|
80
|
+
expect(sql).not.toMatch(/ALTER COLUMN/i);
|
|
81
|
+
expect(sql).not.toMatch(/legacy_column/);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("never adds a NOT NULL column, which an existing table with rows could not take", () => {
|
|
85
|
+
const sql = planCollectionSchemaEnsure([posts], withTable("public.posts", ["id"]))
|
|
86
|
+
.statements.join("\n");
|
|
87
|
+
expect(sql).not.toMatch(/NOT NULL/i);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("leaves relation columns to a real migration rather than adding them without their key", () => {
|
|
91
|
+
const withRelation = {
|
|
92
|
+
...posts,
|
|
93
|
+
properties: {
|
|
94
|
+
...(posts as unknown as { properties: Record<string, unknown> }).properties,
|
|
95
|
+
author: { name: "Author", type: "reference", target: () => posts }
|
|
96
|
+
}
|
|
97
|
+
} as unknown as CollectionConfig;
|
|
98
|
+
const plan = planCollectionSchemaEnsure([withRelation], withTable("public.posts", ["id"]));
|
|
99
|
+
expect(plan.actions.some(a => a.target.endsWith(".author"))).toBe(false);
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
describe("applying the plan", () => {
|
|
104
|
+
function fakeClient(): { client: Queryable; executed: string[] } {
|
|
105
|
+
const executed: string[] = [];
|
|
106
|
+
const client: Queryable = {
|
|
107
|
+
async query<T>(sql: string): Promise<{ rows: T[] }> {
|
|
108
|
+
executed.push(sql);
|
|
109
|
+
return { rows: [] as T[] };
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
return { client, executed };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
it("creates the schema, reads what exists, then applies", async () => {
|
|
116
|
+
const { client, executed } = fakeClient();
|
|
117
|
+
const plan = await ensureCollectionTables(client, [posts]);
|
|
118
|
+
|
|
119
|
+
expect(plan.actions.length).toBeGreaterThan(0);
|
|
120
|
+
expect(executed.some(s => s.includes("information_schema.columns"))).toBe(true);
|
|
121
|
+
expect(executed.some(s => s.includes("CREATE TABLE IF NOT EXISTS"))).toBe(true);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("surfaces which statement failed rather than a bare driver error", async () => {
|
|
125
|
+
const client: Queryable = {
|
|
126
|
+
async query<T>(sql: string): Promise<{ rows: T[] }> {
|
|
127
|
+
if (sql.startsWith("CREATE TABLE")) throw new Error("permission denied");
|
|
128
|
+
return { rows: [] as T[] };
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
await expect(ensureCollectionTables(client, [posts])).rejects.toThrow(/permission denied/);
|
|
132
|
+
await expect(ensureCollectionTables(client, [posts])).rejects.toThrow(/public\.posts/);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("does nothing when the database is already current", async () => {
|
|
136
|
+
const client: Queryable = {
|
|
137
|
+
async query<T>(sql: string): Promise<{ rows: T[] }> {
|
|
138
|
+
if (sql.includes("information_schema.columns")) {
|
|
139
|
+
return {
|
|
140
|
+
rows: ["id", "title", "views", "status"].map(c => ({
|
|
141
|
+
table_schema: "public",
|
|
142
|
+
table_name: "posts",
|
|
143
|
+
column_name: c
|
|
144
|
+
})) as unknown as T[]
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
if (sql.includes("pg_type")) {
|
|
148
|
+
return { rows: [{ schema: "public", name: "posts_status" }] as unknown as T[] };
|
|
149
|
+
}
|
|
150
|
+
return { rows: [] as T[] };
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
const plan = await ensureCollectionTables(client, [posts]);
|
|
154
|
+
expect(plan.actions).toEqual([]);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bringing a database up to date with a bundle's collections, additively.
|
|
3
|
+
*
|
|
4
|
+
* ## Why this exists
|
|
5
|
+
*
|
|
6
|
+
* A managed runtime boots someone else's compiled project against a database it
|
|
7
|
+
* has never seen. Auth tables are ensured at boot already, but collection tables
|
|
8
|
+
* were not created by anything: the platform ran the app and every `/api/data/*`
|
|
9
|
+
* request answered 500 on a missing relation. `rebase db push` cannot help — it
|
|
10
|
+
* is an Atlas-driven CLI command, and the runtime image ships no CLI.
|
|
11
|
+
*
|
|
12
|
+
* ## Why additive-only, forever
|
|
13
|
+
*
|
|
14
|
+
* This runs unattended, against a database with customers' data in it, with no
|
|
15
|
+
* human reading a diff. So it may only ever do things that cannot lose data:
|
|
16
|
+
* create a missing table, add a missing column, create a missing enum type.
|
|
17
|
+
*
|
|
18
|
+
* It will **never** drop a table or a column, narrow a type, or alter a
|
|
19
|
+
* constraint. A removed field leaves its column behind; a renamed field looks
|
|
20
|
+
* like an addition and the old column stays. That is the correct trade for an
|
|
21
|
+
* automated path — the alternative is an unattended process that can silently
|
|
22
|
+
* destroy a column, which is precisely the failure `db push` was hardened
|
|
23
|
+
* against. Destructive changes stay a deliberate, human-reviewed migration.
|
|
24
|
+
*
|
|
25
|
+
* Because of that, this is safe to run on every boot, and re-running it is a
|
|
26
|
+
* no-op.
|
|
27
|
+
*/
|
|
28
|
+
import { type CollectionConfig, type Property, isPostgresCollectionConfig } from "@rebasepro/types";
|
|
29
|
+
import { getTableName } from "@rebasepro/common";
|
|
30
|
+
import {
|
|
31
|
+
getSqlColumnType,
|
|
32
|
+
resolveColumnName,
|
|
33
|
+
isIdProperty
|
|
34
|
+
} from "./generate-postgres-ddl-logic";
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The subset of a database handle this needs: run a statement, get rows back.
|
|
38
|
+
*
|
|
39
|
+
* Deliberately parameterless. Everything here is DDL or catalogue reads keyed by
|
|
40
|
+
* schema name, and schema names are identifiers — they cannot be bound as
|
|
41
|
+
* parameters anyway. They are validated against {@link SAFE_IDENTIFIER} before
|
|
42
|
+
* they reach a statement, so a config that somehow carried a quote is refused
|
|
43
|
+
* rather than concatenated.
|
|
44
|
+
*/
|
|
45
|
+
export interface Queryable {
|
|
46
|
+
query<T = unknown>(sql: string): Promise<{ rows: T[] }>;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Postgres identifiers this module is willing to interpolate. */
|
|
50
|
+
const SAFE_IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_$]*$/;
|
|
51
|
+
|
|
52
|
+
function assertSafeIdentifier(value: string, what: string): string {
|
|
53
|
+
if (!SAFE_IDENTIFIER.test(value)) {
|
|
54
|
+
throw new Error(`Refusing to build SQL with an unsafe ${what}: ${JSON.stringify(value)}`);
|
|
55
|
+
}
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** What the database currently has, as the planner needs it. */
|
|
60
|
+
export interface ExistingSchema {
|
|
61
|
+
/** `schema.table` → set of column names. */
|
|
62
|
+
tables: Map<string, Set<string>>;
|
|
63
|
+
/** `schema.typename` of every enum type that already exists. */
|
|
64
|
+
enums: Set<string>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface EnsureAction {
|
|
68
|
+
kind: "create-enum" | "create-table" | "add-column";
|
|
69
|
+
/** Qualified target, for logging: `public.posts` or `public.posts.title`. */
|
|
70
|
+
target: string;
|
|
71
|
+
sql: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface EnsurePlan {
|
|
75
|
+
actions: EnsureAction[];
|
|
76
|
+
/** Every statement, in dependency order. Empty when the schema is current. */
|
|
77
|
+
statements: string[];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function schemaOf(collection: CollectionConfig): string {
|
|
81
|
+
return isPostgresCollectionConfig(collection) && collection.schema ? collection.schema : "public";
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function qualified(collection: CollectionConfig): string {
|
|
85
|
+
return `${schemaOf(collection)}.${getTableName(collection)}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Enum types a collection's properties require, as `schema.typename`.
|
|
90
|
+
*
|
|
91
|
+
* Named exactly as the DDL generator names them (`<table>_<column>`), because
|
|
92
|
+
* a column added here has to reference the same type the generator would have
|
|
93
|
+
* created — a second, differently-named type for the same field would be a
|
|
94
|
+
* silent schema fork.
|
|
95
|
+
*/
|
|
96
|
+
function requiredEnums(collection: CollectionConfig): { name: string; values: string[] }[] {
|
|
97
|
+
const table = getTableName(collection);
|
|
98
|
+
const schema = schemaOf(collection);
|
|
99
|
+
const out: { name: string; values: string[] }[] = [];
|
|
100
|
+
for (const [propName, prop] of Object.entries(collection.properties ?? {})) {
|
|
101
|
+
const p = prop as Property;
|
|
102
|
+
if (!("enum" in p) || !p.enum) continue;
|
|
103
|
+
if (p.type !== "string" && p.type !== "number") continue;
|
|
104
|
+
const values = (p.enum as unknown[])
|
|
105
|
+
.map(entry =>
|
|
106
|
+
entry && typeof entry === "object" && "id" in (entry as Record<string, unknown>)
|
|
107
|
+
? String((entry as Record<string, unknown>).id)
|
|
108
|
+
: String(entry)
|
|
109
|
+
)
|
|
110
|
+
.filter(v => v.length > 0);
|
|
111
|
+
if (values.length === 0) continue;
|
|
112
|
+
out.push({ name: `${schema}.${table}_${resolveColumnName(propName, p)}`, values });
|
|
113
|
+
}
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Single-quote escaping for an enum label. */
|
|
118
|
+
function quoteLiteral(value: string): string {
|
|
119
|
+
return `'${value.replace(/'/g, "''")}'`;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Decide what to add. Pure — the caller supplies what exists and runs the result.
|
|
124
|
+
*
|
|
125
|
+
* Ordering matters and is deliberate: enum types before the tables and columns
|
|
126
|
+
* that reference them, tables before the columns added to other tables (a new
|
|
127
|
+
* table may be the target of a relation), and nothing is emitted twice.
|
|
128
|
+
*/
|
|
129
|
+
export function planCollectionSchemaEnsure(
|
|
130
|
+
collections: CollectionConfig[],
|
|
131
|
+
existing: ExistingSchema
|
|
132
|
+
): EnsurePlan {
|
|
133
|
+
const actions: EnsureAction[] = [];
|
|
134
|
+
const plannedEnums = new Set<string>();
|
|
135
|
+
|
|
136
|
+
// 1. Enum types. `CREATE TYPE` has no IF NOT EXISTS, so an existing type is
|
|
137
|
+
// skipped by name rather than guarded in SQL.
|
|
138
|
+
for (const collection of collections) {
|
|
139
|
+
for (const { name, values } of requiredEnums(collection)) {
|
|
140
|
+
if (existing.enums.has(name) || plannedEnums.has(name)) continue;
|
|
141
|
+
plannedEnums.add(name);
|
|
142
|
+
const [schema, typeName] = name.split(".");
|
|
143
|
+
actions.push({
|
|
144
|
+
kind: "create-enum",
|
|
145
|
+
target: name,
|
|
146
|
+
sql: `CREATE TYPE "${schema}"."${typeName}" AS ENUM (${values.map(quoteLiteral).join(", ")});`
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 2. Missing tables. Only the identity column is created here; every other
|
|
152
|
+
// column is added by step 3, so a new table and an existing table that
|
|
153
|
+
// gained a field travel the exact same code path. One way to build a
|
|
154
|
+
// column means one way for it to be wrong.
|
|
155
|
+
const created = new Set<string>();
|
|
156
|
+
for (const collection of collections) {
|
|
157
|
+
const key = qualified(collection);
|
|
158
|
+
if (existing.tables.has(key) || created.has(key)) continue;
|
|
159
|
+
created.add(key);
|
|
160
|
+
const schema = schemaOf(collection);
|
|
161
|
+
const table = getTableName(collection);
|
|
162
|
+
const idEntry = Object.entries(collection.properties ?? {}).find(([n, p]) =>
|
|
163
|
+
isIdProperty(n, p as Property, collection)
|
|
164
|
+
);
|
|
165
|
+
const idName = idEntry ? resolveColumnName(idEntry[0], idEntry[1] as Property) : "id";
|
|
166
|
+
const idProp = idEntry?.[1] as Property | undefined;
|
|
167
|
+
let idDef: string;
|
|
168
|
+
if (idProp?.type === "number") {
|
|
169
|
+
idDef = `"${idName}" BIGSERIAL PRIMARY KEY`;
|
|
170
|
+
} else if (
|
|
171
|
+
idProp &&
|
|
172
|
+
idProp.type === "string" &&
|
|
173
|
+
(idProp as { isId?: unknown }).isId === "uuid"
|
|
174
|
+
) {
|
|
175
|
+
idDef = `"${idName}" UUID PRIMARY KEY DEFAULT gen_random_uuid()`;
|
|
176
|
+
} else {
|
|
177
|
+
idDef = `"${idName}" TEXT PRIMARY KEY`;
|
|
178
|
+
}
|
|
179
|
+
actions.push({
|
|
180
|
+
kind: "create-table",
|
|
181
|
+
target: key,
|
|
182
|
+
sql: `CREATE TABLE IF NOT EXISTS "${schema}"."${table}" (${idDef});`
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// 3. Missing columns, on both brand-new and pre-existing tables.
|
|
187
|
+
for (const collection of collections) {
|
|
188
|
+
const key = qualified(collection);
|
|
189
|
+
const schema = schemaOf(collection);
|
|
190
|
+
const table = getTableName(collection);
|
|
191
|
+
const present = existing.tables.get(key) ?? new Set<string>();
|
|
192
|
+
for (const [propName, prop] of Object.entries(collection.properties ?? {})) {
|
|
193
|
+
const p = prop as Property;
|
|
194
|
+
if (isIdProperty(propName, p, collection)) continue;
|
|
195
|
+
// A relation's own column is emitted by the DDL generator with a
|
|
196
|
+
// foreign key; adding a bare column here would create the column
|
|
197
|
+
// without the constraint and make the generator's later output
|
|
198
|
+
// disagree with the database. Left to a real migration.
|
|
199
|
+
if (p.type === "reference" || p.type === "relation") continue;
|
|
200
|
+
const column = resolveColumnName(propName, p);
|
|
201
|
+
if (present.has(column)) continue;
|
|
202
|
+
const type = getSqlColumnType(propName, p, collection, collections);
|
|
203
|
+
actions.push({
|
|
204
|
+
kind: "add-column",
|
|
205
|
+
target: `${key}.${column}`,
|
|
206
|
+
// Never NOT NULL: an existing table with rows cannot take a
|
|
207
|
+
// non-null column without a default, and inventing one would be
|
|
208
|
+
// guessing at the customer's data.
|
|
209
|
+
sql: `ALTER TABLE "${schema}"."${table}" ADD COLUMN IF NOT EXISTS "${column}" ${type};`
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return { actions, statements: actions.map(a => a.sql) };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Read what the database has, for the schemas the collections live in. */
|
|
218
|
+
export async function readExistingSchema(
|
|
219
|
+
client: Queryable,
|
|
220
|
+
schemas: string[]
|
|
221
|
+
): Promise<ExistingSchema> {
|
|
222
|
+
const tables = new Map<string, Set<string>>();
|
|
223
|
+
const enums = new Set<string>();
|
|
224
|
+
if (schemas.length === 0) return { tables, enums };
|
|
225
|
+
|
|
226
|
+
const inList = schemas
|
|
227
|
+
.map(schema => `'${assertSafeIdentifier(schema, "schema name")}'`)
|
|
228
|
+
.join(", ");
|
|
229
|
+
|
|
230
|
+
const { rows: columns } = await client.query<{
|
|
231
|
+
table_schema: string;
|
|
232
|
+
table_name: string;
|
|
233
|
+
column_name: string;
|
|
234
|
+
}>(
|
|
235
|
+
`SELECT table_schema, table_name, column_name
|
|
236
|
+
FROM information_schema.columns
|
|
237
|
+
WHERE table_schema IN (${inList})`
|
|
238
|
+
);
|
|
239
|
+
for (const row of columns) {
|
|
240
|
+
const key = `${row.table_schema}.${row.table_name}`;
|
|
241
|
+
if (!tables.has(key)) tables.set(key, new Set());
|
|
242
|
+
tables.get(key)!.add(row.column_name);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const { rows: enumRows } = await client.query<{ schema: string; name: string }>(
|
|
246
|
+
`SELECT n.nspname AS schema, t.typname AS name
|
|
247
|
+
FROM pg_type t
|
|
248
|
+
JOIN pg_namespace n ON t.typnamespace = n.oid
|
|
249
|
+
WHERE t.typtype = 'e' AND n.nspname IN (${inList})`
|
|
250
|
+
);
|
|
251
|
+
for (const row of enumRows) enums.add(`${row.schema}.${row.name}`);
|
|
252
|
+
|
|
253
|
+
return { tables, enums };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Bring the database up to date. Returns what it did.
|
|
258
|
+
*
|
|
259
|
+
* Each statement runs on its own rather than in one transaction: they are all
|
|
260
|
+
* independently safe and idempotent, and a single failure (an enum label that
|
|
261
|
+
* cannot be added, say) should not roll back the tables that were created fine.
|
|
262
|
+
* The error is surfaced with the statement that caused it.
|
|
263
|
+
*/
|
|
264
|
+
export async function ensureCollectionTables(
|
|
265
|
+
client: Queryable,
|
|
266
|
+
collections: CollectionConfig[],
|
|
267
|
+
log?: (message: string) => void
|
|
268
|
+
): Promise<EnsurePlan> {
|
|
269
|
+
const schemas = Array.from(new Set(collections.map(schemaOf)));
|
|
270
|
+
for (const schema of schemas) {
|
|
271
|
+
assertSafeIdentifier(schema, "schema name");
|
|
272
|
+
if (schema !== "public") {
|
|
273
|
+
await client.query(`CREATE SCHEMA IF NOT EXISTS "${schema}";`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const existing = await readExistingSchema(client, schemas);
|
|
278
|
+
const plan = planCollectionSchemaEnsure(collections, existing);
|
|
279
|
+
|
|
280
|
+
if (plan.actions.length === 0) {
|
|
281
|
+
log?.("Schema is up to date; nothing to create.");
|
|
282
|
+
return plan;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
for (const action of plan.actions) {
|
|
286
|
+
try {
|
|
287
|
+
await client.query(action.sql);
|
|
288
|
+
log?.(`${action.kind}: ${action.target}`);
|
|
289
|
+
} catch (err) {
|
|
290
|
+
throw new Error(
|
|
291
|
+
`Failed to ${action.kind} ${action.target}: ` +
|
|
292
|
+
`${err instanceof Error ? err.message : String(err)}\n ${action.sql}`
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return plan;
|
|
297
|
+
}
|
|
@@ -97,12 +97,14 @@ const getDrizzleColumn = (propName: string, prop: Property, collection: Collecti
|
|
|
97
97
|
columnDefinition = `uuid("${colName}")`;
|
|
98
98
|
} else if (stringProp.columnType === "uuid") {
|
|
99
99
|
columnDefinition = `uuid("${colName}")`;
|
|
100
|
-
} else if (stringProp.columnType === "text" || stringProp.ui?.markdown || stringProp.ui?.multiline) {
|
|
101
|
-
columnDefinition = `text("${colName}")`;
|
|
102
100
|
} else if (stringProp.columnType === "char") {
|
|
103
101
|
columnDefinition = `char("${colName}")`;
|
|
104
|
-
} else {
|
|
102
|
+
} else if (stringProp.columnType === "varchar") {
|
|
105
103
|
columnDefinition = `varchar("${colName}")`;
|
|
104
|
+
} else {
|
|
105
|
+
// `text` is the default, and the only length-unbounded choice.
|
|
106
|
+
// Ask for `varchar` explicitly if you want the length constraint.
|
|
107
|
+
columnDefinition = `text("${colName}")`;
|
|
106
108
|
}
|
|
107
109
|
if (isIdProperty(propName, prop, collection)) {
|
|
108
110
|
columnDefinition += ".primaryKey()";
|
|
@@ -252,7 +254,7 @@ const getDrizzleColumn = (propName: string, prop: Property, collection: Collecti
|
|
|
252
254
|
const targetTableVar = getTableVarName(getTableName(targetCollection));
|
|
253
255
|
const pkProp = getPrimaryKeyProp(targetCollection);
|
|
254
256
|
const targetIdField = pkProp.name;
|
|
255
|
-
const baseColumn = pkProp.type === "number" ? `integer("${fkColumnName}")` : (pkProp.isUuid ? `uuid("${fkColumnName}")` : `
|
|
257
|
+
const baseColumn = pkProp.type === "number" ? `integer("${fkColumnName}")` : (pkProp.isUuid ? `uuid("${fkColumnName}")` : `text("${fkColumnName}")`);
|
|
256
258
|
|
|
257
259
|
const onUpdate = relation.onUpdate ? `onUpdate: "${relation.onUpdate}"` : "";
|
|
258
260
|
const required = prop.validation?.required;
|
|
@@ -274,14 +276,14 @@ const getDrizzleColumn = (propName: string, prop: Property, collection: Collecti
|
|
|
274
276
|
const refProp = prop as ReferenceProperty;
|
|
275
277
|
const targetCollection = collections.find(c => c.slug === refProp.path || getTableName(c) === refProp.path);
|
|
276
278
|
if (!targetCollection) {
|
|
277
|
-
columnDefinition = `
|
|
279
|
+
columnDefinition = `text("${colName}")`;
|
|
278
280
|
break;
|
|
279
281
|
}
|
|
280
282
|
|
|
281
283
|
const pkProp = getPrimaryKeyProp(targetCollection);
|
|
282
284
|
const targetTableVar = getTableVarName(getTableName(targetCollection));
|
|
283
285
|
const targetIdField = pkProp.name;
|
|
284
|
-
const baseColumn = pkProp.type === "number" ? `integer("${colName}")` : (pkProp.isUuid ? `uuid("${colName}")` : `
|
|
286
|
+
const baseColumn = pkProp.type === "number" ? `integer("${colName}")` : (pkProp.isUuid ? `uuid("${colName}")` : `text("${colName}")`);
|
|
285
287
|
|
|
286
288
|
const required = prop.validation?.required;
|
|
287
289
|
const onDelete = required ? "cascade" : "set null";
|
|
@@ -597,8 +599,10 @@ export const generateSchema = async (collections: CollectionConfig[], stripPolic
|
|
|
597
599
|
const onDelete = relation.onDelete ?? "cascade";
|
|
598
600
|
const refOptions = `{ onDelete: \"${onDelete}\" }`;
|
|
599
601
|
|
|
600
|
-
|
|
601
|
-
|
|
602
|
+
// `text`, matching the string default: a junction column must have the
|
|
603
|
+
// same type as the primary key it references.
|
|
604
|
+
const sourceColType = isNumericId(sourceCollection) ? "integer" : (getPrimaryKeyProp(sourceCollection).isUuid ? "uuid" : "text");
|
|
605
|
+
const targetColType = isNumericId(targetCollection) ? "integer" : (getPrimaryKeyProp(targetCollection).isUuid ? "uuid" : "text");
|
|
602
606
|
const sourceId = getPrimaryKeyName(sourceCollection);
|
|
603
607
|
const targetId = getPrimaryKeyName(targetCollection);
|
|
604
608
|
|
|
@@ -637,7 +641,7 @@ export const generateSchema = async (collections: CollectionConfig[], stripPolic
|
|
|
637
641
|
// We should generate a basic id column if one was completely omitted.
|
|
638
642
|
const hasIdColumn = Array.from(columns).some(col => col.includes(".primaryKey()"));
|
|
639
643
|
if (!hasIdColumn) {
|
|
640
|
-
columns.add(" id:
|
|
644
|
+
columns.add(" id: text(\"id\").primaryKey()");
|
|
641
645
|
}
|
|
642
646
|
|
|
643
647
|
schemaContent += `${Array.from(columns).join(",\n")}`;
|
|
@@ -4,7 +4,7 @@ import { toSnakeCase, getPolicyNamesForRule } from "@rebasepro/utils";
|
|
|
4
4
|
|
|
5
5
|
// --- Helper Functions ---
|
|
6
6
|
|
|
7
|
-
const resolveColumnName = (propName: string, prop?: Property | null): string => {
|
|
7
|
+
export const resolveColumnName = (propName: string, prop?: Property | null): string => {
|
|
8
8
|
if (prop && "columnName" in prop && typeof prop.columnName === "string") {
|
|
9
9
|
return prop.columnName;
|
|
10
10
|
}
|
|
@@ -36,7 +36,7 @@ const getPrimaryKeyName = (collection: CollectionConfig): string => {
|
|
|
36
36
|
return getPrimaryKeyProp(collection).name;
|
|
37
37
|
};
|
|
38
38
|
|
|
39
|
-
const isIdProperty = (propName: string, prop: Property, collection: CollectionConfig): boolean => {
|
|
39
|
+
export const isIdProperty = (propName: string, prop: Property, collection: CollectionConfig): boolean => {
|
|
40
40
|
if ("isId" in prop && Boolean(prop.isId)) return true;
|
|
41
41
|
const hasExplicitId = Object.values(collection.properties ?? {}).some(p => "isId" in (p as unknown as object) && Boolean((p as unknown as Record<string, unknown>).isId));
|
|
42
42
|
return !hasExplicitId && propName === "id";
|
|
@@ -90,7 +90,7 @@ const generateSinglePolicyDdl = (collection: CollectionConfig, rule: SecurityRul
|
|
|
90
90
|
return `${ddl};\n`;
|
|
91
91
|
};
|
|
92
92
|
|
|
93
|
-
const getSqlColumnType = (propName: string, prop: Property, collection: CollectionConfig, collections: CollectionConfig[]): string => {
|
|
93
|
+
export const getSqlColumnType = (propName: string, prop: Property, collection: CollectionConfig, collections: CollectionConfig[]): string => {
|
|
94
94
|
switch (prop.type) {
|
|
95
95
|
case "string": {
|
|
96
96
|
const stringProp = prop as StringProperty;
|
|
@@ -103,13 +103,18 @@ const getSqlColumnType = (propName: string, prop: Property, collection: Collecti
|
|
|
103
103
|
if (stringProp.isId === "uuid" || stringProp.columnType === "uuid") {
|
|
104
104
|
return "UUID";
|
|
105
105
|
}
|
|
106
|
-
if (stringProp.columnType === "text" || stringProp.ui?.markdown || stringProp.ui?.multiline) {
|
|
107
|
-
return "TEXT";
|
|
108
|
-
}
|
|
109
106
|
if (stringProp.columnType === "char") {
|
|
110
107
|
return "CHAR(255)";
|
|
111
108
|
}
|
|
112
|
-
|
|
109
|
+
if (stringProp.columnType === "varchar") {
|
|
110
|
+
return "VARCHAR(255)";
|
|
111
|
+
}
|
|
112
|
+
// `text` is the default. The two generators disagreed here before:
|
|
113
|
+
// this one emitted VARCHAR(255) while the drizzle path emitted a bare
|
|
114
|
+
// `varchar()`, which Postgres treats as unbounded — so the same
|
|
115
|
+
// property produced a capped column down one path and an uncapped one
|
|
116
|
+
// down the other.
|
|
117
|
+
return "TEXT";
|
|
113
118
|
}
|
|
114
119
|
case "number": {
|
|
115
120
|
const numProp = prop as NumberProperty;
|
|
@@ -173,20 +178,20 @@ const getSqlColumnType = (propName: string, prop: Property, collection: Collecti
|
|
|
173
178
|
try {
|
|
174
179
|
targetCollection = relation.target();
|
|
175
180
|
} catch {
|
|
176
|
-
return "
|
|
181
|
+
return "TEXT";
|
|
177
182
|
}
|
|
178
183
|
const pkProp = getPrimaryKeyProp(targetCollection);
|
|
179
|
-
return pkProp.type === "number" ? "INTEGER" : (pkProp.isUuid ? "UUID" : "
|
|
184
|
+
return pkProp.type === "number" ? "INTEGER" : (pkProp.isUuid ? "UUID" : "TEXT");
|
|
180
185
|
}
|
|
181
186
|
case "reference": {
|
|
182
187
|
const refProp = prop as ReferenceProperty;
|
|
183
188
|
const targetCollection = collections.find(c => c.slug === refProp.path || getTableName(c) === refProp.path);
|
|
184
|
-
if (!targetCollection) return "
|
|
189
|
+
if (!targetCollection) return "TEXT";
|
|
185
190
|
const pkProp = getPrimaryKeyProp(targetCollection);
|
|
186
|
-
return pkProp.type === "number" ? "INTEGER" : (pkProp.isUuid ? "UUID" : "
|
|
191
|
+
return pkProp.type === "number" ? "INTEGER" : (pkProp.isUuid ? "UUID" : "TEXT");
|
|
187
192
|
}
|
|
188
193
|
default:
|
|
189
|
-
return "
|
|
194
|
+
return "TEXT";
|
|
190
195
|
}
|
|
191
196
|
};
|
|
192
197
|
|
|
@@ -287,8 +292,10 @@ export const generatePostgresDdl = async (
|
|
|
287
292
|
const targetSchema = isPostgresCollectionConfig(targetCollection) && targetCollection.schema ? targetCollection.schema : "public";
|
|
288
293
|
const { sourceColumn, targetColumn } = relation.through;
|
|
289
294
|
|
|
290
|
-
|
|
291
|
-
|
|
295
|
+
// TEXT, matching the string default: a junction column has to have the
|
|
296
|
+
// same type as the primary key it references.
|
|
297
|
+
const sourceColType = isNumericId(sourceCollection) ? "INTEGER" : (getPrimaryKeyProp(sourceCollection).isUuid ? "UUID" : "TEXT");
|
|
298
|
+
const targetColType = isNumericId(targetCollection) ? "INTEGER" : (getPrimaryKeyProp(targetCollection).isUuid ? "UUID" : "TEXT");
|
|
292
299
|
const sourceId = getPrimaryKeyName(sourceCollection);
|
|
293
300
|
const targetId = getPrimaryKeyName(targetCollection);
|
|
294
301
|
|
|
@@ -424,7 +431,7 @@ export const generatePostgresDdl = async (
|
|
|
424
431
|
// Backwards compatibility: add default id primary key if missing
|
|
425
432
|
const hasPk = columns.some(c => c.includes("PRIMARY KEY"));
|
|
426
433
|
if (!hasPk) {
|
|
427
|
-
columns.unshift(' "id"
|
|
434
|
+
columns.unshift(' "id" TEXT PRIMARY KEY');
|
|
428
435
|
}
|
|
429
436
|
|
|
430
437
|
ddl += columns.join(",\n");
|