@rebasepro/server-postgres 0.10.1-canary.b1e3dbf → 0.10.1-canary.ff9ccd6

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rebasepro/server-postgres",
3
3
  "type": "module",
4
- "version": "0.10.1-canary.b1e3dbf",
4
+ "version": "0.10.1-canary.ff9ccd6",
5
5
  "description": "PostgreSQL data source backend implementation for Rebase with Drizzle ORM",
6
6
  "funding": {
7
7
  "url": "https://github.com/sponsors/rebaseco"
@@ -47,21 +47,20 @@
47
47
  "execa": "^9.6.1",
48
48
  "pg": "^8.21.0",
49
49
  "ws": "^8.21.0",
50
- "@rebasepro/codegen": "0.10.1-canary.b1e3dbf",
51
- "@rebasepro/common": "0.10.1-canary.b1e3dbf",
52
- "@rebasepro/types": "0.10.1-canary.b1e3dbf",
53
- "@rebasepro/server": "0.10.1-canary.b1e3dbf",
54
- "@rebasepro/utils": "0.10.1-canary.b1e3dbf"
50
+ "@rebasepro/codegen": "0.10.1-canary.ff9ccd6",
51
+ "@rebasepro/server": "0.10.1-canary.ff9ccd6",
52
+ "@rebasepro/types": "0.10.1-canary.ff9ccd6",
53
+ "@rebasepro/utils": "0.10.1-canary.ff9ccd6",
54
+ "@rebasepro/common": "0.10.1-canary.ff9ccd6"
55
55
  },
56
56
  "devDependencies": {
57
- "@hono/node-server": "^2.0.9",
57
+ "@hono/node-server": "^2.0.11",
58
58
  "@jest/globals": "^30.4.1",
59
59
  "@types/jest": "^30.0.0",
60
60
  "@types/node": "^25.9.3",
61
61
  "@types/pg": "^8.20.0",
62
62
  "@types/ws": "^8.18.1",
63
- "@vitejs/plugin-react": "^6.0.2",
64
- "hono": "^4.12.25",
63
+ "hono": "^4.12.27",
65
64
  "jest": "^30.4.2",
66
65
  "ts-jest": "^29.4.11",
67
66
  "typescript": "^6.0.3",
@@ -644,6 +644,46 @@ authRepository };
644
644
  return internals.realtimeService;
645
645
  },
646
646
 
647
+ /**
648
+ * Create any collection tables, columns and enum types the database is
649
+ * missing — additively, never destructively.
650
+ *
651
+ * This is what lets the managed runtime boot a project against a fresh
652
+ * database and actually serve it. Before this, only auth tables were
653
+ * ensured, so a managed tenant came up with working sign-in and a 500 on
654
+ * every data route.
655
+ *
656
+ * Runs through the drizzle handle's underlying session so it uses the
657
+ * same connection (and therefore the same privileges) the driver already
658
+ * proved it can bootstrap with.
659
+ */
660
+ async ensureCollectionSchema(
661
+ collections: unknown[],
662
+ driverResult: InitializedDriver,
663
+ log?: (message: string) => void
664
+ ): Promise<{ applied: number }> {
665
+ const internals = driverResult.internals as PostgresDriverInternals;
666
+ const { ensureCollectionTables } = await import("./schema/ensure-collection-tables");
667
+ // Runs through the drizzle handle the driver already bootstrapped
668
+ // with, so it uses exactly the connection and privileges that were
669
+ // proven to work. Every statement is DDL or a catalogue read with no
670
+ // bindable values (schema names are identifiers), and the module
671
+ // validates them before they reach a string.
672
+ const queryable = {
673
+ async query<T>(text: string): Promise<{ rows: T[] }> {
674
+ const result = await internals.db.execute(sql.raw(text));
675
+ const rows = (result as unknown as { rows?: T[] }).rows;
676
+ return { rows: rows ?? (Array.isArray(result) ? (result as T[]) : []) };
677
+ }
678
+ };
679
+ const plan = await ensureCollectionTables(
680
+ queryable,
681
+ collections as Parameters<typeof ensureCollectionTables>[1],
682
+ log
683
+ );
684
+ return { applied: plan.actions.length };
685
+ },
686
+
647
687
  getAdmin(driverResult: InitializedDriver): DatabaseAdmin | undefined {
648
688
  const internals = driverResult.internals as PostgresDriverInternals;
649
689
  return internals.driver.admin;
@@ -62,12 +62,10 @@ export function getExpectedColumnType(prop: Property): string | null {
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
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";
69
65
  if (sp.columnType === "char") return "character";
70
- return "character varying";
66
+ if (sp.columnType === "varchar") return "character varying";
67
+ // `text` is the default — see generate-postgres-ddl-logic.
68
+ return "text";
71
69
  }
72
70
  case "number": {
73
71
  const np = prop as NumberProperty;
@@ -122,7 +120,9 @@ export function getExpectedColumnType(prop: Property): string | null {
122
120
  case "relation":
123
121
  return null; // FK columns are derived from the relation, not from the property
124
122
  case "reference":
125
- return "character varying"; // References default to varchar FK
123
+ // A reference FK follows the key it points at, and a string key is
124
+ // `text` — see generate-postgres-ddl-logic.
125
+ return "text";
126
126
  case "vector":
127
127
  return "USER-DEFINED";
128
128
  case "binary":
@@ -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}")` : `varchar("${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 = `varchar("${colName}")`;
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}")` : `varchar("${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
- const sourceColType = isNumericId(sourceCollection) ? "integer" : (getPrimaryKeyProp(sourceCollection).isUuid ? "uuid" : "varchar");
601
- const targetColType = isNumericId(targetCollection) ? "integer" : (getPrimaryKeyProp(targetCollection).isUuid ? "uuid" : "varchar");
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: varchar(\"id\").primaryKey()");
644
+ columns.add(" id: text(\"id\").primaryKey()");
641
645
  }
642
646
 
643
647
  schemaContent += `${Array.from(columns).join(",\n")}`;