@rebasepro/server-postgres 0.21.2-canary.g1ea48be → 0.23.0

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.
Files changed (83) hide show
  1. package/dist/{BranchService-CucnFcSE.js → BranchService-DGPL6G_C.js} +2 -2
  2. package/dist/{BranchService-CucnFcSE.js.map → BranchService-DGPL6G_C.js.map} +1 -1
  3. package/dist/PostgresBackendDriver.d.ts +41 -1
  4. package/dist/auth/services.d.ts +27 -8
  5. package/dist/backup/backup-cli.d.ts +22 -0
  6. package/dist/{backup-cli-CkjsJEcu.js → backup-cli-24v4OlSp.js} +17 -14
  7. package/dist/backup-cli-24v4OlSp.js.map +1 -0
  8. package/dist/{backup-service-BNLwvxuy.js → backup-service-HQ9GC4tN.js} +2 -2
  9. package/dist/{backup-service-BNLwvxuy.js.map → backup-service-HQ9GC4tN.js.map} +1 -1
  10. package/dist/{cli-errors-Dka89exj.js → cli-errors-B8qHg02P.js} +6 -1
  11. package/dist/cli-errors-B8qHg02P.js.map +1 -0
  12. package/dist/cli-flags-BglvpjHv.js +138 -0
  13. package/dist/cli-flags-BglvpjHv.js.map +1 -0
  14. package/dist/cli-flags.d.ts +28 -0
  15. package/dist/cli-helpers.d.ts +6 -0
  16. package/dist/cli-scratch-database.d.ts +34 -0
  17. package/dist/cli.js +97 -261
  18. package/dist/cli.js.map +1 -1
  19. package/dist/{column-plan-helpers-CpILzHJS.js → column-plan-helpers-DF-8dTVa.js} +16 -20
  20. package/dist/column-plan-helpers-DF-8dTVa.js.map +1 -0
  21. package/dist/{doctor-Gzzd8wTq.js → doctor-Cb2thZ8s.js} +8 -7
  22. package/dist/doctor-Cb2thZ8s.js.map +1 -0
  23. package/dist/{ensure-collection-policies-B4IDFtQ-.js → ensure-collection-policies-RHUcEp4v.js} +5 -5
  24. package/dist/{ensure-collection-policies-B4IDFtQ-.js.map → ensure-collection-policies-RHUcEp4v.js.map} +1 -1
  25. package/dist/{ensure-collection-tables-Cr2ye5JC.js → ensure-collection-tables-BEEjn5cn.js} +53 -22
  26. package/dist/ensure-collection-tables-BEEjn5cn.js.map +1 -0
  27. package/dist/{ensure-tables-BnEvEJPr.js → ensure-tables-Dhn9KM3B.js} +2 -2
  28. package/dist/{ensure-tables-BnEvEJPr.js.map → ensure-tables-Dhn9KM3B.js.map} +1 -1
  29. package/dist/{generate-drizzle-schema-logic-DFa9qy9u.js → generate-drizzle-schema-logic-BfzK7UQd.js} +2 -2
  30. package/dist/{generate-drizzle-schema-logic-DFa9qy9u.js.map → generate-drizzle-schema-logic-BfzK7UQd.js.map} +1 -1
  31. package/dist/{generate-drizzle-schema-D2MDdJt-.js → generate-drizzle-schema-yzY_BLhr.js} +2 -2
  32. package/dist/{generate-drizzle-schema-D2MDdJt-.js.map → generate-drizzle-schema-yzY_BLhr.js.map} +1 -1
  33. package/dist/{generate-postgres-ddl-logic-CR2xcS7e.js → generate-postgres-ddl-logic-Bt2d2mRH.js} +2 -2
  34. package/dist/{generate-postgres-ddl-logic-CR2xcS7e.js.map → generate-postgres-ddl-logic-Bt2d2mRH.js.map} +1 -1
  35. package/dist/index.es.js +1882 -392
  36. package/dist/index.es.js.map +1 -1
  37. package/dist/{introspect-db-logic-C6LQdTxj.js → introspect-db-logic-WMuAfxvw.js} +323 -4
  38. package/dist/introspect-db-logic-WMuAfxvw.js.map +1 -0
  39. package/dist/{plan-schema-Hgl62S-w.js → plan-schema-C0fxM8dY.js} +170 -38
  40. package/dist/plan-schema-C0fxM8dY.js.map +1 -0
  41. package/dist/{policy-drift-B0GDRh7_.js → policy-drift-DljYdrpW.js} +2 -2
  42. package/dist/{policy-drift-B0GDRh7_.js.map → policy-drift-DljYdrpW.js.map} +1 -1
  43. package/dist/{rls-bootstrap-sql-BHA6jLtj.js → rls-bootstrap-sql-B8EclDyM.js} +320 -17
  44. package/dist/rls-bootstrap-sql-B8EclDyM.js.map +1 -0
  45. package/dist/schema/atlas-argv.d.ts +15 -0
  46. package/dist/schema/column-plan-helpers.d.ts +23 -4
  47. package/dist/schema/destructive-sql.d.ts +52 -1
  48. package/dist/schema/doctor-cli.js +3 -3
  49. package/dist/schema/generate-drizzle-schema.js +1 -1
  50. package/dist/schema/generate-postgres-ddl.js +1 -1
  51. package/dist/schema/introspect-db.js +1 -312
  52. package/dist/schema/introspect-db.js.map +1 -1
  53. package/dist/schema/plan/diff-plan.d.ts +3 -2
  54. package/dist/schema/plan/plan-schema.d.ts +10 -3
  55. package/dist/schema/plan/types.d.ts +6 -1
  56. package/dist/schema/search-column.d.ts +21 -1
  57. package/dist/services/FetchService.d.ts +147 -30
  58. package/dist/services/PersistService.d.ts +56 -5
  59. package/dist/services/RelationService.d.ts +42 -1
  60. package/dist/services/RelationWriteService.d.ts +22 -1
  61. package/dist/services/cdc/CdcListener.d.ts +6 -1
  62. package/dist/services/collection-helpers.d.ts +11 -0
  63. package/dist/services/dataService.d.ts +19 -21
  64. package/dist/services/field-op-sql.d.ts +71 -0
  65. package/dist/services/junction-writes.d.ts +19 -3
  66. package/dist/services/pg-notify-listener.d.ts +7 -0
  67. package/dist/services/read-field-access.d.ts +19 -0
  68. package/dist/services/read-scope.d.ts +100 -0
  69. package/dist/services/realtimeService.d.ts +77 -3
  70. package/dist/services/row-pipeline.d.ts +5 -0
  71. package/dist/services/soft-delete.d.ts +0 -2
  72. package/dist/services/write-transaction-scope.d.ts +39 -0
  73. package/dist/utils/drizzle-conditions.d.ts +63 -8
  74. package/dist/websocket.d.ts +10 -3
  75. package/package.json +9 -9
  76. package/dist/backup-cli-CkjsJEcu.js.map +0 -1
  77. package/dist/cli-errors-Dka89exj.js.map +0 -1
  78. package/dist/column-plan-helpers-CpILzHJS.js.map +0 -1
  79. package/dist/doctor-Gzzd8wTq.js.map +0 -1
  80. package/dist/ensure-collection-tables-Cr2ye5JC.js.map +0 -1
  81. package/dist/introspect-db-logic-C6LQdTxj.js.map +0 -1
  82. package/dist/plan-schema-Hgl62S-w.js.map +0 -1
  83. package/dist/rls-bootstrap-sql-BHA6jLtj.js.map +0 -1
@@ -54,5 +54,20 @@ export interface AtlasInvocation {
54
54
  * instead, with `stripCarvedOutStatements`.
55
55
  */
56
56
  export declare function acceptsExcludeFlag(domain: string, args: string[]): boolean;
57
+ /**
58
+ * Does this Atlas invocation plan against the scratch database (`--dev-url`)?
59
+ *
60
+ * `schema apply` and `migrate diff` do; `migrate apply`, `hash` and `status`
61
+ * never read it, so `db migrate` has no reason to create one.
62
+ */
63
+ export declare function takesDevUrl(domain: string, args: string[]): boolean;
57
64
  /** Assemble the full argv, connection flags and all. */
58
65
  export declare function buildAtlasArgs(invocation: AtlasInvocation): string[];
66
+ /**
67
+ * `atlas migrate apply`'s own arguments for `rebase db migrate`.
68
+ *
69
+ * `positionals` are what is left of the user's line once every flag the
70
+ * driver knows has been consumed — at most the optional amount Atlas takes. A
71
+ * flag reaching here is a flag Atlas rejects before doing any work.
72
+ */
73
+ export declare function migrateApplyArgs(positionals: string[], baseline: string | undefined): string[];
@@ -44,12 +44,31 @@ export declare const resolveColumnName: (propName: string, prop?: Property | nul
44
44
  * config was written. Refused here instead, where the collection has a name.
45
45
  */
46
46
  export declare const assertSinglePrimaryKey: (collection: CollectionConfig) => void;
47
- export declare const getPrimaryKeyProp: (collection: CollectionConfig) => {
47
+ /**
48
+ * A collection's primary key, by both of its names.
49
+ *
50
+ * `name` is the property key — what the generated Drizzle table is keyed by,
51
+ * so it is what a relation's `references` names. `column` is the SQL
52
+ * identifier, so it is what a foreign key's `REFERENCES` names. They differ
53
+ * whenever the key is camelCase or carries a `columnName` (`authorId` is
54
+ * `author_id`, and every table `rebase schema introspect` adopts is spelled
55
+ * that way), and a constraint written with the key named a column that does
56
+ * not exist: `db push` could not build its desired state, and boot failed the
57
+ * constraint on every start.
58
+ */
59
+ export interface PrimaryKey {
60
+ /** Property key: the Drizzle object key and the JSON field. */
48
61
  name: string;
49
- type: "string" | "number";
50
- isUuid: boolean;
51
- };
62
+ /** SQL column. */
63
+ column: string;
64
+ /** The declaring property; absent for the implicit `id TEXT` of a collection that declares none. */
65
+ prop?: Property;
66
+ }
67
+ export declare const getPrimaryKeyProp: (collection: CollectionConfig) => PrimaryKey;
68
+ /** The primary key's property key. What a Drizzle relation's `references` names. */
52
69
  export declare const getPrimaryKeyName: (collection: CollectionConfig) => string;
70
+ /** The primary key's column. What a foreign key's `REFERENCES` names. */
71
+ export declare const getPrimaryKeyColumn: (collection: CollectionConfig) => string;
53
72
  export declare const isIdProperty: (propName: string, prop: Property, collection: CollectionConfig) => boolean;
54
73
  /**
55
74
  * The labels an `enum` declares, in order, whichever form it was written in.
@@ -9,6 +9,13 @@
9
9
  * here, and refuse to auto-approve anything destructive without an explicit
10
10
  * opt-in.
11
11
  *
12
+ * A type change is the one destructive plan that names no DROP. `ALTER COLUMN
13
+ * "price" TYPE integer` rounds every 19.99 to 20, and `TYPE date` throws away
14
+ * every timestamp's time of day, in a statement that reads like a routine
15
+ * alter. So a type change is gated too, unless it is a widening every value
16
+ * survives unchanged — which needs the column's current type, read from the
17
+ * catalogue by the caller.
18
+ *
12
19
  * Everything in this file is side-effect free so it can be unit-tested
13
20
  * without Atlas or a database.
14
21
  */
@@ -24,13 +31,57 @@ export interface DestructiveStatement {
24
31
  statement: string;
25
32
  /** Which destructive operation it was flagged for, e.g. "DROP COLUMN". */
26
33
  kind: string;
34
+ /**
35
+ * What in the statement loses data, when the kind alone does not say:
36
+ * for `ALTER COLUMN TYPE`, each lossy column with its old and new type.
37
+ */
38
+ detail?: string;
39
+ }
40
+ /** A column as the database has it now, with its type as `format_type` spells it. */
41
+ export interface ExistingColumnType {
42
+ schema: string;
43
+ table: string;
44
+ column: string;
45
+ /** e.g. `character varying(255)`, `numeric(10,2)`, `timestamp with time zone`. */
46
+ type: string;
27
47
  }
48
+ /**
49
+ * Every column of every ordinary table outside the system schemas, with its
50
+ * type. What {@link detectDestructiveStatements} compares a planned type
51
+ * change against; `format_type` because it carries the modifier (`(255)`,
52
+ * `(10,2)`) that decides whether a change widens or narrows.
53
+ */
54
+ export declare const COLUMN_TYPES_SQL = "\n SELECT n.nspname AS \"schema\",\n c.relname AS \"table\",\n a.attname AS \"column\",\n format_type(a.atttypid, a.atttypmod) AS \"type\"\n FROM pg_attribute a\n JOIN pg_class c ON c.oid = a.attrelid\n JOIN pg_namespace n ON n.oid = c.relnamespace\n WHERE c.relkind IN ('r', 'p')\n AND a.attnum > 0\n AND NOT a.attisdropped\n AND n.nspname NOT IN ('pg_catalog', 'information_schema')\n AND n.nspname NOT LIKE 'pg\\_%'\n";
28
55
  /**
29
56
  * Scan an Atlas plan (the SQL printed by `schema apply --dry-run`) and return
30
57
  * the statements that would destroy data. An empty array means the plan is
31
58
  * safe to auto-approve.
59
+ *
60
+ * `columnTypes` is what the database holds now. A type change to a column it
61
+ * does not list is flagged: not knowing what a column is means not knowing
62
+ * whether the change keeps its values.
63
+ */
64
+ export declare function detectDestructiveStatements(planSql: string, columnTypes?: readonly ExistingColumnType[]): DestructiveStatement[];
65
+ /**
66
+ * Is `ALTER COLUMN … TYPE to` a widening, one that keeps every value exactly
67
+ * as it was?
68
+ *
69
+ * Deliberately short. Everything not listed needs a confirmation, including
70
+ * changes that are usually harmless: `timestamp` → `timestamptz` reads each
71
+ * value in the session's time zone, `json` → `jsonb` drops duplicate keys,
72
+ * `real` → `double precision` prints 0.1 as 0.100000001490116, and `integer` →
73
+ * `text` keeps the digits but changes what the column is.
74
+ *
75
+ * - the same type, with a modifier at least as wide (`varchar(100)` →
76
+ * `varchar(255)`, `numeric(10,2)` → `numeric(12,2)` or unbounded,
77
+ * `timestamptz(3)` → `timestamptz`);
78
+ * - `smallint` → `integer` → `bigint`, and any of them to a `numeric` with
79
+ * room for all their digits;
80
+ * - `varchar(n)` → `text`, and `text` → an unbounded `varchar`.
81
+ *
82
+ * Arrays follow their element type, and never gain or lose a dimension.
32
83
  */
33
- export declare function detectDestructiveStatements(planSql: string): DestructiveStatement[];
84
+ export declare function isLosslessTypeChange(from: string, to: string): boolean;
34
85
  export type PushDecision = "apply" | "confirm" | "refuse";
35
86
  /**
36
87
  * Decide how `db push` should proceed given the plan's destructiveness and
@@ -1,11 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
  import { createRequire as __createRequire } from "module";
3
3
  __createRequire(import.meta.url);
4
- import { i as diagnoseDbError } from "../cli-errors-Dka89exj.js";
4
+ import { i as diagnoseDbError } from "../cli-errors-B8qHg02P.js";
5
5
  import { o as validatePolicyPgRoles, s as warnOnAnonymousGrants } from "../rls-enforcement-C6Xk0lA6.js";
6
6
  import { t as chalk } from "../source-Br7L7GOI.js";
7
- import { n as loadCollections, r as runDoctor } from "../doctor-Gzzd8wTq.js";
8
- import { n as formatPolicyDrift, r as hasDrift, t as checkPolicyDrift } from "../policy-drift-B0GDRh7_.js";
7
+ import { n as loadCollections, r as runDoctor } from "../doctor-Cb2thZ8s.js";
8
+ import { n as formatPolicyDrift, r as hasDrift, t as checkPolicyDrift } from "../policy-drift-DljYdrpW.js";
9
9
  import path from "path";
10
10
  //#region src/schema/doctor-policy-checks.ts
11
11
  /**
@@ -1,6 +1,6 @@
1
1
  import { createRequire as __createRequire } from "module";
2
2
  __createRequire(import.meta.url);
3
- import { t as main } from "../generate-drizzle-schema-D2MDdJt-.js";
3
+ import { t as main } from "../generate-drizzle-schema-yzY_BLhr.js";
4
4
  //#region src/schema/bin/generate-drizzle-schema.ts
5
5
  /**
6
6
  * The built entry point for `generate-drizzle-schema`.
@@ -1,6 +1,6 @@
1
1
  import { createRequire as __createRequire } from "module";
2
2
  __createRequire(import.meta.url);
3
- import { a as generatePostgresVectorDdl, i as generatePostgresTriggersDdl, n as generatePostgresPoliciesDdl, r as generatePostgresSearchDdl, t as generatePostgresDdl } from "../generate-postgres-ddl-logic-CR2xcS7e.js";
3
+ import { a as generatePostgresVectorDdl, i as generatePostgresTriggersDdl, n as generatePostgresPoliciesDdl, r as generatePostgresSearchDdl, t as generatePostgresDdl } from "../generate-postgres-ddl-logic-Bt2d2mRH.js";
4
4
  import { n as outError, t as out } from "../cli-output-CNdMql-L.js";
5
5
  import { loadCollectionsFromDirectory } from "@rebasepro/server";
6
6
  import { declaredDatabaseExtensions } from "@rebasepro/types";
@@ -1,9 +1,9 @@
1
1
  import { createRequire as __createRequire } from "module";
2
2
  __createRequire(import.meta.url);
3
3
  import { a as __toESM, t as require___vite_browser_external } from "../__vite-browser-external-BnuHet1e.js";
4
+ import { _ as isUsableFileName, a as generateCollectionFile, f as classifyTables, g as countRowsUpTo, i as buildTablesMap, l as mergeIndexContent, o as generateIndexContent, p as lookupCandidates, r as buildEnumMap, u as safeHostFromUrl, v as quoteRelation, y as readSchemaMetadata } from "../introspect-db-logic-WMuAfxvw.js";
4
5
  import { n as outError, r as outWarn, t as out } from "../cli-output-CNdMql-L.js";
5
6
  import { t as chalk } from "../source-Br7L7GOI.js";
6
- import { a as generateCollectionFile, f as classifyTables, i as buildTablesMap, l as mergeIndexContent, o as generateIndexContent, p as lookupCandidates, r as buildEnumMap, u as safeHostFromUrl } from "../introspect-db-logic-C6LQdTxj.js";
7
7
  import { t as require_arg } from "../arg-Dni7MzLB.js";
8
8
  import pg from "pg";
9
9
  import fs from "fs";
@@ -77,317 +77,6 @@ function detectCollectionBuilder(outDir) {
77
77
  return "annotation";
78
78
  }
79
79
  //#endregion
80
- //#region src/schema/introspect-db-queries.ts
81
- /**
82
- * A Postgres identifier, quoted so that whatever is inside it stays inside it.
83
- *
84
- * Introspection reads table and schema names out of `pg_class` and puts them
85
- * straight back into SQL, and wrapping a name in double quotes is not the same
86
- * as escaping it: a table named `x" ; DROP TABLE users; --` closes the quote
87
- * and then the statement. Postgres's own escape for a quote inside a quoted
88
- * identifier is to double it, which is what this does.
89
- *
90
- * "The catalogue is trusted" is true of your own database, and is exactly the
91
- * assumption this tool cannot make. Introspection exists to be pointed at
92
- * databases somebody else built, and it runs with the privileges of whoever
93
- * ran it — which is usually a superuser, because that is who has a connection
94
- * string to a database they are adopting.
95
- */
96
- function quoteIdentifier(name) {
97
- return `"${name.replace(/"/g, "\"\"")}"`;
98
- }
99
- /** `"schema"."table"`, both halves quoted. */
100
- function quoteRelation(schema, table) {
101
- return `${quoteIdentifier(schema)}.${quoteIdentifier(table)}`;
102
- }
103
- /**
104
- * Can this table's name be the name of the file describing it?
105
- *
106
- * Introspection writes `<collectionsDir>/<tableName>.ts`, and the table name
107
- * came from a database this tool did not create. Postgres will hold a table
108
- * called `../../../etc/cron.d/x`, and `path.join` resolves it — so without this
109
- * the generator writes a file wherever the name points, as whoever ran the
110
- * command.
111
- *
112
- * Deliberately narrow, and deliberately not a blocklist of `..` and `/`: the
113
- * question is "is this a plain file name?", and every answer that is not
114
- * obviously yes is a table better written by hand. NUL and the Windows
115
- * separator are in scope because a collections directory is checked out on
116
- * whatever machine the developer has.
117
- */
118
- function isUsableFileName(name) {
119
- if (name.length === 0 || name.length > 200) return false;
120
- if (name === "." || name === "..") return false;
121
- if (/[/\\:\u0000-\u001f]/.test(name)) return false;
122
- return !name.startsWith(".") && !name.endsWith(".");
123
- }
124
- /**
125
- * Base tables, excluding partitions.
126
- *
127
- * `relispartition` is the reason this reads `pg_class` rather than
128
- * `information_schema.tables`, which reports every partition as a base table of
129
- * its own. Pagila partitions `payment` by month; introspected through
130
- * information_schema it yields `payment` plus 26 near-identical
131
- * `payment_p2022_*` collections, each with its own nav entry.
132
- *
133
- * `relkind = 'p'` (the partitioned parent) is included and `'r'` partitions are
134
- * dropped, which is the right way round: the parent is the queryable table.
135
- */
136
- var TABLES_QUERY = `
137
- SELECT c.relname AS table_name,
138
- c.relkind = 'p' AS is_partitioned
139
- FROM pg_class c
140
- JOIN pg_namespace n ON n.oid = c.relnamespace
141
- WHERE n.nspname = $1
142
- AND c.relkind IN ('r', 'p')
143
- AND NOT c.relispartition
144
- AND c.relname NOT LIKE 'drizzle_%'
145
- AND c.relname NOT LIKE 'rebase_%'
146
- ORDER BY c.relname
147
- `;
148
- /**
149
- * Columns, with the catalog facts that say a column is not the user's to edit.
150
- *
151
- * `is_generated`/`is_identity` are read for that reason: a generated column
152
- * rejects any write, so a form that offers it is offering a field that always
153
- * errors. `character_maximum_length` is the declared `varchar(n)` bound —
154
- * already enforced by the database, and free to surface as validation.
155
- */
156
- var COLUMNS_QUERY = `
157
- SELECT
158
- c.table_name,
159
- c.column_name,
160
- c.data_type,
161
- c.udt_name,
162
- c.is_nullable,
163
- c.column_default,
164
- c.ordinal_position,
165
- c.is_generated,
166
- c.is_identity,
167
- c.identity_generation,
168
- c.character_maximum_length,
169
- c.numeric_precision,
170
- c.numeric_scale,
171
- (SELECT a.atttypmod FROM pg_attribute a
172
- JOIN pg_class pc ON a.attrelid = pc.oid
173
- WHERE pc.relname = c.table_name
174
- AND a.attname = c.column_name
175
- AND pc.relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = c.table_schema)) as atttypmod
176
- FROM information_schema.columns c
177
- WHERE c.table_schema = $1
178
- ORDER BY c.table_name, c.ordinal_position
179
- `;
180
- var ENUMS_QUERY = `
181
- SELECT t.typname AS enum_name,
182
- e.enumlabel AS enum_value,
183
- e.enumsortorder AS sort_order
184
- FROM pg_type t
185
- JOIN pg_enum e ON t.oid = e.enumtypid
186
- JOIN pg_namespace n ON t.typnamespace = n.oid
187
- WHERE n.nspname = $1
188
- ORDER BY t.typname, e.enumsortorder
189
- `;
190
- /**
191
- * Primary key columns, in key order.
192
- *
193
- * `ORDER BY k.ord` matters for composite keys: the position of a column inside
194
- * the key is what tells a two-column PK made of two foreign keys apart from an
195
- * ordinary composite key, and `= ANY(i.indkey)` (the shape this replaced)
196
- * returns catalog order instead.
197
- */
198
- var PRIMARY_KEYS_QUERY = `
199
- SELECT t.relname AS table_name,
200
- a.attname AS column_name
201
- FROM pg_index i
202
- JOIN pg_class t ON t.oid = i.indrelid
203
- JOIN pg_namespace n ON n.oid = t.relnamespace
204
- CROSS JOIN LATERAL unnest(i.indkey) WITH ORDINALITY AS k(attnum, ord)
205
- JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = k.attnum
206
- WHERE i.indisprimary AND n.nspname = $1
207
- ORDER BY t.relname, k.ord
208
- `;
209
- /**
210
- * Foreign keys, one row per referencing column, with the delete rule.
211
- *
212
- * Read from `pg_constraint` rather than through
213
- * `information_schema.constraint_column_usage`, which does not preserve the
214
- * pairing on a composite foreign key: it reports the cross product of the
215
- * constraint's columns, so a two-column FK comes back as four rows, two of them
216
- * pairing the wrong columns. `unnest(...) WITH ORDINALITY` on both sides joined
217
- * on the ordinal keeps each referencing column with the column it references.
218
- *
219
- * `confdeltype` is carried because `ON DELETE CASCADE` is the schema author
220
- * saying, in the database, that the child cannot outlive the parent — the one
221
- * unambiguous declaration of ownership a schema contains.
222
- *
223
- * Constraints declared on a *partition* are attributed to the partition root,
224
- * because that is the table this run generates a collection for. Pagila declares
225
- * `payment`'s three foreign keys on each monthly partition and none on the
226
- * parent, so reading `pg_constraint` at face value gives the `payment`
227
- * collection no relations at all. `pick = 1` then keeps one row per logical key,
228
- * preferring the root's own constraint when it has one, so a schema that
229
- * declares the key on the parent *and* inherits it onto 26 partitions does not
230
- * yield 27 copies of the same relation.
231
- */
232
- var FOREIGN_KEYS_QUERY = `
233
- WITH fk AS (
234
- SELECT con.oid,
235
- con.conname,
236
- con.conrelid,
237
- con.confrelid,
238
- con.conkey,
239
- con.confkey,
240
- con.confdeltype,
241
- COALESCE(pg_partition_root(con.conrelid), con.conrelid) AS root_oid,
242
- row_number() OVER (
243
- PARTITION BY COALESCE(pg_partition_root(con.conrelid), con.conrelid),
244
- con.confrelid, con.conkey, con.confkey
245
- ORDER BY (con.conrelid = COALESCE(pg_partition_root(con.conrelid), con.conrelid)) DESC,
246
- con.oid
247
- ) AS pick
248
- FROM pg_constraint con
249
- JOIN pg_namespace n ON n.oid = con.connamespace
250
- WHERE con.contype = 'f' AND n.nspname = $1
251
- )
252
- SELECT root.relname AS table_name,
253
- sa.attname AS column_name,
254
- tgt.relname AS foreign_table_name,
255
- ta.attname AS foreign_column_name,
256
- fk.conname AS constraint_name,
257
- s.ord::int AS ordinal,
258
- CASE fk.confdeltype
259
- WHEN 'a' THEN 'NO ACTION'
260
- WHEN 'r' THEN 'RESTRICT'
261
- WHEN 'c' THEN 'CASCADE'
262
- WHEN 'n' THEN 'SET NULL'
263
- WHEN 'd' THEN 'SET DEFAULT'
264
- END AS delete_rule
265
- FROM fk
266
- JOIN pg_class root ON root.oid = fk.root_oid
267
- JOIN pg_class tgt ON tgt.oid = fk.confrelid
268
- CROSS JOIN LATERAL unnest(fk.conkey) WITH ORDINALITY AS s(attnum, ord)
269
- JOIN pg_attribute sa ON sa.attrelid = fk.conrelid AND sa.attnum = s.attnum
270
- CROSS JOIN LATERAL unnest(fk.confkey) WITH ORDINALITY AS f(attnum, ord)
271
- JOIN pg_attribute ta ON ta.attrelid = fk.confrelid AND ta.attnum = f.attnum
272
- WHERE fk.pick = 1 AND s.ord = f.ord
273
- ORDER BY root.relname, fk.conname, s.ord
274
- `;
275
- /**
276
- * Unique constraints and unique indexes, as ordered column lists.
277
- *
278
- * Indexes rather than constraints alone, because `CREATE UNIQUE INDEX` and
279
- * `ADD CONSTRAINT ... UNIQUE` produce the same guarantee and schemas use both.
280
- * Partial indexes (`indpred IS NOT NULL`) are excluded: they promise uniqueness
281
- * only over the rows matching their predicate, which is not the promise a
282
- * `validation: { unique: true }` field makes.
283
- */
284
- var UNIQUE_CONSTRAINTS_QUERY = `
285
- SELECT t.relname AS table_name,
286
- ic.relname AS constraint_name,
287
- -- ::text, because the driver has no parser for an array of \`name\`
288
- -- and hands back the raw \`{a,b}\` literal as a string. Every consumer
289
- -- here indexes and compares it as an array, and a string of that
290
- -- shape fails those silently rather than loudly.
291
- array_agg(a.attname::text ORDER BY k.ord) AS column_names
292
- FROM pg_index i
293
- JOIN pg_class t ON t.oid = i.indrelid
294
- JOIN pg_class ic ON ic.oid = i.indexrelid
295
- JOIN pg_namespace n ON n.oid = t.relnamespace
296
- CROSS JOIN LATERAL unnest(i.indkey) WITH ORDINALITY AS k(attnum, ord)
297
- JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = k.attnum
298
- WHERE i.indisunique
299
- AND NOT i.indisprimary
300
- AND i.indpred IS NULL
301
- AND n.nspname = $1
302
- AND NOT t.relispartition
303
- GROUP BY t.relname, ic.relname
304
- ORDER BY t.relname, ic.relname
305
- `;
306
- /**
307
- * CHECK constraints, as the source text Postgres reproduces them from.
308
- *
309
- * `pg_get_constraintdef` output is normalized by the server — the parser in
310
- * {@link ./introspect-db-constraints.ts} reads that normalized form, not
311
- * whatever the author typed.
312
- */
313
- var CHECK_CONSTRAINTS_QUERY = `
314
- SELECT DISTINCT
315
- root.relname AS table_name,
316
- con.conname AS constraint_name,
317
- pg_get_constraintdef(con.oid) AS definition
318
- FROM pg_constraint con
319
- JOIN pg_namespace n ON n.oid = con.connamespace
320
- JOIN pg_class root ON root.oid = COALESCE(pg_partition_root(con.conrelid), con.conrelid)
321
- WHERE con.contype = 'c'
322
- AND n.nspname = $1
323
- ORDER BY root.relname, con.conname
324
- `;
325
- /**
326
- * `COMMENT ON TABLE` / `COMMENT ON COLUMN`, which is documentation the author
327
- * already wrote and which nothing downstream has ever read.
328
- */
329
- var COMMENTS_QUERY = `
330
- SELECT t.relname AS table_name,
331
- a.attname AS column_name,
332
- d.description AS comment
333
- FROM pg_description d
334
- JOIN pg_class t ON t.oid = d.objoid
335
- JOIN pg_namespace n ON n.oid = t.relnamespace
336
- LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = d.objsubid
337
- WHERE n.nspname = $1
338
- AND t.relkind IN ('r', 'p')
339
- AND (d.objsubid = 0 OR a.attname IS NOT NULL)
340
- ORDER BY t.relname, d.objsubid
341
- `;
342
- /**
343
- * Counts a table's rows, stopping once the answer can only be "more than
344
- * `limit`".
345
- *
346
- * Introspection asks this to tell a small reference table from a table that
347
- * merely looks like one (see `classifyTables`). `reltuples` would answer for
348
- * free but lies on a table that has never been analyzed — which is every table
349
- * in a database restored from a dump, i.e. exactly the case introspection meets
350
- * — and reports -1 there rather than an error, so the lie is silent.
351
- *
352
- * The subquery's LIMIT caps the work at `limit + 1` rows however large the
353
- * table is, so this stays cheap on a table with a billion rows.
354
- */
355
- async function countRowsUpTo(client, schema, table, limit) {
356
- const { rows } = await client.query(`SELECT count(*)::text AS count FROM (SELECT 1 FROM ${quoteRelation(schema, table)} LIMIT ${limit + 1}) probe`);
357
- return Number(rows[0]?.count ?? 0);
358
- }
359
- /**
360
- * Reads everything the generator needs, in one pass.
361
- *
362
- * `rowCounts` is deliberately absent here: it costs a query per table and only
363
- * a handful of tables can possibly need it. The caller fills it in for the
364
- * candidates `lookupCandidates()` names.
365
- */
366
- async function readSchemaMetadata(client, schema) {
367
- const tables = await client.query(TABLES_QUERY, [schema]);
368
- const columns = await client.query(COLUMNS_QUERY, [schema]);
369
- const enumValues = await client.query(ENUMS_QUERY, [schema]);
370
- const pks = await client.query(PRIMARY_KEYS_QUERY, [schema]);
371
- const fks = await client.query(FOREIGN_KEYS_QUERY, [schema]);
372
- const uniques = await client.query(UNIQUE_CONSTRAINTS_QUERY, [schema]);
373
- const checks = await client.query(CHECK_CONSTRAINTS_QUERY, [schema]);
374
- const comments = await client.query(COMMENTS_QUERY, [schema]);
375
- const known = new Set(tables.rows.map((t) => t.table_name));
376
- const scoped = (rows) => rows.filter((r) => known.has(r.table_name));
377
- return {
378
- schema,
379
- tables: tables.rows,
380
- columns: scoped(columns.rows),
381
- enumValues: enumValues.rows,
382
- pks: scoped(pks.rows),
383
- fks: scoped(fks.rows).filter((fk) => known.has(fk.foreign_table_name)),
384
- uniques: scoped(uniques.rows),
385
- checks: scoped(checks.rows),
386
- comments: scoped(comments.rows),
387
- rowCounts: {}
388
- };
389
- }
390
- //#endregion
391
80
  //#region src/schema/introspect-db-constraints.ts
392
81
  /** The length functions whose argument is the column being constrained. */
393
82
  var LENGTH_FUNCTIONS = /* @__PURE__ */ new Set([