@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.
- package/dist/{BranchService-CucnFcSE.js → BranchService-DGPL6G_C.js} +2 -2
- package/dist/{BranchService-CucnFcSE.js.map → BranchService-DGPL6G_C.js.map} +1 -1
- package/dist/PostgresBackendDriver.d.ts +41 -1
- package/dist/auth/services.d.ts +27 -8
- package/dist/backup/backup-cli.d.ts +22 -0
- package/dist/{backup-cli-CkjsJEcu.js → backup-cli-24v4OlSp.js} +17 -14
- package/dist/backup-cli-24v4OlSp.js.map +1 -0
- package/dist/{backup-service-BNLwvxuy.js → backup-service-HQ9GC4tN.js} +2 -2
- package/dist/{backup-service-BNLwvxuy.js.map → backup-service-HQ9GC4tN.js.map} +1 -1
- package/dist/{cli-errors-Dka89exj.js → cli-errors-B8qHg02P.js} +6 -1
- package/dist/cli-errors-B8qHg02P.js.map +1 -0
- package/dist/cli-flags-BglvpjHv.js +138 -0
- package/dist/cli-flags-BglvpjHv.js.map +1 -0
- package/dist/cli-flags.d.ts +28 -0
- package/dist/cli-helpers.d.ts +6 -0
- package/dist/cli-scratch-database.d.ts +34 -0
- package/dist/cli.js +97 -261
- package/dist/cli.js.map +1 -1
- package/dist/{column-plan-helpers-CpILzHJS.js → column-plan-helpers-DF-8dTVa.js} +16 -20
- package/dist/column-plan-helpers-DF-8dTVa.js.map +1 -0
- package/dist/{doctor-Gzzd8wTq.js → doctor-Cb2thZ8s.js} +8 -7
- package/dist/doctor-Cb2thZ8s.js.map +1 -0
- package/dist/{ensure-collection-policies-B4IDFtQ-.js → ensure-collection-policies-RHUcEp4v.js} +5 -5
- package/dist/{ensure-collection-policies-B4IDFtQ-.js.map → ensure-collection-policies-RHUcEp4v.js.map} +1 -1
- package/dist/{ensure-collection-tables-Cr2ye5JC.js → ensure-collection-tables-BEEjn5cn.js} +53 -22
- package/dist/ensure-collection-tables-BEEjn5cn.js.map +1 -0
- package/dist/{ensure-tables-BnEvEJPr.js → ensure-tables-Dhn9KM3B.js} +2 -2
- package/dist/{ensure-tables-BnEvEJPr.js.map → ensure-tables-Dhn9KM3B.js.map} +1 -1
- package/dist/{generate-drizzle-schema-logic-DFa9qy9u.js → generate-drizzle-schema-logic-BfzK7UQd.js} +2 -2
- package/dist/{generate-drizzle-schema-logic-DFa9qy9u.js.map → generate-drizzle-schema-logic-BfzK7UQd.js.map} +1 -1
- package/dist/{generate-drizzle-schema-D2MDdJt-.js → generate-drizzle-schema-yzY_BLhr.js} +2 -2
- package/dist/{generate-drizzle-schema-D2MDdJt-.js.map → generate-drizzle-schema-yzY_BLhr.js.map} +1 -1
- package/dist/{generate-postgres-ddl-logic-CR2xcS7e.js → generate-postgres-ddl-logic-Bt2d2mRH.js} +2 -2
- package/dist/{generate-postgres-ddl-logic-CR2xcS7e.js.map → generate-postgres-ddl-logic-Bt2d2mRH.js.map} +1 -1
- package/dist/index.es.js +1882 -392
- package/dist/index.es.js.map +1 -1
- package/dist/{introspect-db-logic-C6LQdTxj.js → introspect-db-logic-WMuAfxvw.js} +323 -4
- package/dist/introspect-db-logic-WMuAfxvw.js.map +1 -0
- package/dist/{plan-schema-Hgl62S-w.js → plan-schema-C0fxM8dY.js} +170 -38
- package/dist/plan-schema-C0fxM8dY.js.map +1 -0
- package/dist/{policy-drift-B0GDRh7_.js → policy-drift-DljYdrpW.js} +2 -2
- package/dist/{policy-drift-B0GDRh7_.js.map → policy-drift-DljYdrpW.js.map} +1 -1
- package/dist/{rls-bootstrap-sql-BHA6jLtj.js → rls-bootstrap-sql-B8EclDyM.js} +320 -17
- package/dist/rls-bootstrap-sql-B8EclDyM.js.map +1 -0
- package/dist/schema/atlas-argv.d.ts +15 -0
- package/dist/schema/column-plan-helpers.d.ts +23 -4
- package/dist/schema/destructive-sql.d.ts +52 -1
- package/dist/schema/doctor-cli.js +3 -3
- package/dist/schema/generate-drizzle-schema.js +1 -1
- package/dist/schema/generate-postgres-ddl.js +1 -1
- package/dist/schema/introspect-db.js +1 -312
- package/dist/schema/introspect-db.js.map +1 -1
- package/dist/schema/plan/diff-plan.d.ts +3 -2
- package/dist/schema/plan/plan-schema.d.ts +10 -3
- package/dist/schema/plan/types.d.ts +6 -1
- package/dist/schema/search-column.d.ts +21 -1
- package/dist/services/FetchService.d.ts +147 -30
- package/dist/services/PersistService.d.ts +56 -5
- package/dist/services/RelationService.d.ts +42 -1
- package/dist/services/RelationWriteService.d.ts +22 -1
- package/dist/services/cdc/CdcListener.d.ts +6 -1
- package/dist/services/collection-helpers.d.ts +11 -0
- package/dist/services/dataService.d.ts +19 -21
- package/dist/services/field-op-sql.d.ts +71 -0
- package/dist/services/junction-writes.d.ts +19 -3
- package/dist/services/pg-notify-listener.d.ts +7 -0
- package/dist/services/read-field-access.d.ts +19 -0
- package/dist/services/read-scope.d.ts +100 -0
- package/dist/services/realtimeService.d.ts +77 -3
- package/dist/services/row-pipeline.d.ts +5 -0
- package/dist/services/soft-delete.d.ts +0 -2
- package/dist/services/write-transaction-scope.d.ts +39 -0
- package/dist/utils/drizzle-conditions.d.ts +63 -8
- package/dist/websocket.d.ts +10 -3
- package/package.json +9 -9
- package/dist/backup-cli-CkjsJEcu.js.map +0 -1
- package/dist/cli-errors-Dka89exj.js.map +0 -1
- package/dist/column-plan-helpers-CpILzHJS.js.map +0 -1
- package/dist/doctor-Gzzd8wTq.js.map +0 -1
- package/dist/ensure-collection-tables-Cr2ye5JC.js.map +0 -1
- package/dist/introspect-db-logic-C6LQdTxj.js.map +0 -1
- package/dist/plan-schema-Hgl62S-w.js.map +0 -1
- package/dist/rls-bootstrap-sql-BHA6jLtj.js.map +0 -1
|
@@ -1,6 +1,317 @@
|
|
|
1
1
|
import { createRequire as __createRequire } from "module";
|
|
2
2
|
__createRequire(import.meta.url);
|
|
3
3
|
import { firstFreeKey, toWireKey } from "@rebasepro/utils";
|
|
4
|
+
//#region src/schema/introspect-db-queries.ts
|
|
5
|
+
/**
|
|
6
|
+
* A Postgres identifier, quoted so that whatever is inside it stays inside it.
|
|
7
|
+
*
|
|
8
|
+
* Introspection reads table and schema names out of `pg_class` and puts them
|
|
9
|
+
* straight back into SQL, and wrapping a name in double quotes is not the same
|
|
10
|
+
* as escaping it: a table named `x" ; DROP TABLE users; --` closes the quote
|
|
11
|
+
* and then the statement. Postgres's own escape for a quote inside a quoted
|
|
12
|
+
* identifier is to double it, which is what this does.
|
|
13
|
+
*
|
|
14
|
+
* "The catalogue is trusted" is true of your own database, and is exactly the
|
|
15
|
+
* assumption this tool cannot make. Introspection exists to be pointed at
|
|
16
|
+
* databases somebody else built, and it runs with the privileges of whoever
|
|
17
|
+
* ran it — which is usually a superuser, because that is who has a connection
|
|
18
|
+
* string to a database they are adopting.
|
|
19
|
+
*/
|
|
20
|
+
function quoteIdentifier(name) {
|
|
21
|
+
return `"${name.replace(/"/g, "\"\"")}"`;
|
|
22
|
+
}
|
|
23
|
+
/** `"schema"."table"`, both halves quoted. */
|
|
24
|
+
function quoteRelation(schema, table) {
|
|
25
|
+
return `${quoteIdentifier(schema)}.${quoteIdentifier(table)}`;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Can this table's name be the name of the file describing it?
|
|
29
|
+
*
|
|
30
|
+
* Introspection writes `<collectionsDir>/<tableName>.ts`, and the table name
|
|
31
|
+
* came from a database this tool did not create. Postgres will hold a table
|
|
32
|
+
* called `../../../etc/cron.d/x`, and `path.join` resolves it — so without this
|
|
33
|
+
* the generator writes a file wherever the name points, as whoever ran the
|
|
34
|
+
* command.
|
|
35
|
+
*
|
|
36
|
+
* Deliberately narrow, and deliberately not a blocklist of `..` and `/`: the
|
|
37
|
+
* question is "is this a plain file name?", and every answer that is not
|
|
38
|
+
* obviously yes is a table better written by hand. NUL and the Windows
|
|
39
|
+
* separator are in scope because a collections directory is checked out on
|
|
40
|
+
* whatever machine the developer has.
|
|
41
|
+
*/
|
|
42
|
+
function isUsableFileName(name) {
|
|
43
|
+
if (name.length === 0 || name.length > 200) return false;
|
|
44
|
+
if (name === "." || name === "..") return false;
|
|
45
|
+
if (/[/\\:\u0000-\u001f]/.test(name)) return false;
|
|
46
|
+
return !name.startsWith(".") && !name.endsWith(".");
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Base tables, excluding partitions.
|
|
50
|
+
*
|
|
51
|
+
* `relispartition` is the reason this reads `pg_class` rather than
|
|
52
|
+
* `information_schema.tables`, which reports every partition as a base table of
|
|
53
|
+
* its own. Pagila partitions `payment` by month; introspected through
|
|
54
|
+
* information_schema it yields `payment` plus 26 near-identical
|
|
55
|
+
* `payment_p2022_*` collections, each with its own nav entry.
|
|
56
|
+
*
|
|
57
|
+
* `relkind = 'p'` (the partitioned parent) is included and `'r'` partitions are
|
|
58
|
+
* dropped, which is the right way round: the parent is the queryable table.
|
|
59
|
+
*/
|
|
60
|
+
var TABLES_QUERY = `
|
|
61
|
+
SELECT c.relname AS table_name,
|
|
62
|
+
c.relkind = 'p' AS is_partitioned
|
|
63
|
+
FROM pg_class c
|
|
64
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
65
|
+
WHERE n.nspname = $1
|
|
66
|
+
AND c.relkind IN ('r', 'p')
|
|
67
|
+
AND NOT c.relispartition
|
|
68
|
+
AND c.relname NOT LIKE 'drizzle_%'
|
|
69
|
+
AND c.relname NOT LIKE 'rebase_%'
|
|
70
|
+
ORDER BY c.relname
|
|
71
|
+
`;
|
|
72
|
+
/**
|
|
73
|
+
* Columns, with the catalog facts that say a column is not the user's to edit.
|
|
74
|
+
*
|
|
75
|
+
* `is_generated`/`is_identity` are read for that reason: a generated column
|
|
76
|
+
* rejects any write, so a form that offers it is offering a field that always
|
|
77
|
+
* errors. `character_maximum_length` is the declared `varchar(n)` bound —
|
|
78
|
+
* already enforced by the database, and free to surface as validation.
|
|
79
|
+
*/
|
|
80
|
+
var COLUMNS_QUERY = `
|
|
81
|
+
SELECT
|
|
82
|
+
c.table_name,
|
|
83
|
+
c.column_name,
|
|
84
|
+
c.data_type,
|
|
85
|
+
c.udt_name,
|
|
86
|
+
c.is_nullable,
|
|
87
|
+
c.column_default,
|
|
88
|
+
c.ordinal_position,
|
|
89
|
+
c.is_generated,
|
|
90
|
+
c.is_identity,
|
|
91
|
+
c.identity_generation,
|
|
92
|
+
c.character_maximum_length,
|
|
93
|
+
c.numeric_precision,
|
|
94
|
+
c.numeric_scale,
|
|
95
|
+
(SELECT a.atttypmod FROM pg_attribute a
|
|
96
|
+
JOIN pg_class pc ON a.attrelid = pc.oid
|
|
97
|
+
WHERE pc.relname = c.table_name
|
|
98
|
+
AND a.attname = c.column_name
|
|
99
|
+
AND pc.relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = c.table_schema)) as atttypmod
|
|
100
|
+
FROM information_schema.columns c
|
|
101
|
+
WHERE c.table_schema = $1
|
|
102
|
+
ORDER BY c.table_name, c.ordinal_position
|
|
103
|
+
`;
|
|
104
|
+
var ENUMS_QUERY = `
|
|
105
|
+
SELECT t.typname AS enum_name,
|
|
106
|
+
e.enumlabel AS enum_value,
|
|
107
|
+
e.enumsortorder AS sort_order
|
|
108
|
+
FROM pg_type t
|
|
109
|
+
JOIN pg_enum e ON t.oid = e.enumtypid
|
|
110
|
+
JOIN pg_namespace n ON t.typnamespace = n.oid
|
|
111
|
+
WHERE n.nspname = $1
|
|
112
|
+
ORDER BY t.typname, e.enumsortorder
|
|
113
|
+
`;
|
|
114
|
+
/**
|
|
115
|
+
* Primary key columns, in key order.
|
|
116
|
+
*
|
|
117
|
+
* `ORDER BY k.ord` matters for composite keys: the position of a column inside
|
|
118
|
+
* the key is what tells a two-column PK made of two foreign keys apart from an
|
|
119
|
+
* ordinary composite key, and `= ANY(i.indkey)` (the shape this replaced)
|
|
120
|
+
* returns catalog order instead.
|
|
121
|
+
*/
|
|
122
|
+
var PRIMARY_KEYS_QUERY = `
|
|
123
|
+
SELECT t.relname AS table_name,
|
|
124
|
+
a.attname AS column_name
|
|
125
|
+
FROM pg_index i
|
|
126
|
+
JOIN pg_class t ON t.oid = i.indrelid
|
|
127
|
+
JOIN pg_namespace n ON n.oid = t.relnamespace
|
|
128
|
+
CROSS JOIN LATERAL unnest(i.indkey) WITH ORDINALITY AS k(attnum, ord)
|
|
129
|
+
JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = k.attnum
|
|
130
|
+
WHERE i.indisprimary AND n.nspname = $1
|
|
131
|
+
ORDER BY t.relname, k.ord
|
|
132
|
+
`;
|
|
133
|
+
/**
|
|
134
|
+
* Foreign keys, one row per referencing column, with the delete rule.
|
|
135
|
+
*
|
|
136
|
+
* Read from `pg_constraint` rather than through
|
|
137
|
+
* `information_schema.constraint_column_usage`, which does not preserve the
|
|
138
|
+
* pairing on a composite foreign key: it reports the cross product of the
|
|
139
|
+
* constraint's columns, so a two-column FK comes back as four rows, two of them
|
|
140
|
+
* pairing the wrong columns. `unnest(...) WITH ORDINALITY` on both sides joined
|
|
141
|
+
* on the ordinal keeps each referencing column with the column it references.
|
|
142
|
+
*
|
|
143
|
+
* `confdeltype` is carried because `ON DELETE CASCADE` is the schema author
|
|
144
|
+
* saying, in the database, that the child cannot outlive the parent — the one
|
|
145
|
+
* unambiguous declaration of ownership a schema contains.
|
|
146
|
+
*
|
|
147
|
+
* Constraints declared on a *partition* are attributed to the partition root,
|
|
148
|
+
* because that is the table this run generates a collection for. Pagila declares
|
|
149
|
+
* `payment`'s three foreign keys on each monthly partition and none on the
|
|
150
|
+
* parent, so reading `pg_constraint` at face value gives the `payment`
|
|
151
|
+
* collection no relations at all. `pick = 1` then keeps one row per logical key,
|
|
152
|
+
* preferring the root's own constraint when it has one, so a schema that
|
|
153
|
+
* declares the key on the parent *and* inherits it onto 26 partitions does not
|
|
154
|
+
* yield 27 copies of the same relation.
|
|
155
|
+
*/
|
|
156
|
+
var FOREIGN_KEYS_QUERY = `
|
|
157
|
+
WITH fk AS (
|
|
158
|
+
SELECT con.oid,
|
|
159
|
+
con.conname,
|
|
160
|
+
con.conrelid,
|
|
161
|
+
con.confrelid,
|
|
162
|
+
con.conkey,
|
|
163
|
+
con.confkey,
|
|
164
|
+
con.confdeltype,
|
|
165
|
+
COALESCE(pg_partition_root(con.conrelid), con.conrelid) AS root_oid,
|
|
166
|
+
row_number() OVER (
|
|
167
|
+
PARTITION BY COALESCE(pg_partition_root(con.conrelid), con.conrelid),
|
|
168
|
+
con.confrelid, con.conkey, con.confkey
|
|
169
|
+
ORDER BY (con.conrelid = COALESCE(pg_partition_root(con.conrelid), con.conrelid)) DESC,
|
|
170
|
+
con.oid
|
|
171
|
+
) AS pick
|
|
172
|
+
FROM pg_constraint con
|
|
173
|
+
JOIN pg_namespace n ON n.oid = con.connamespace
|
|
174
|
+
WHERE con.contype = 'f' AND n.nspname = $1
|
|
175
|
+
)
|
|
176
|
+
SELECT root.relname AS table_name,
|
|
177
|
+
sa.attname AS column_name,
|
|
178
|
+
tgt.relname AS foreign_table_name,
|
|
179
|
+
ta.attname AS foreign_column_name,
|
|
180
|
+
fk.conname AS constraint_name,
|
|
181
|
+
s.ord::int AS ordinal,
|
|
182
|
+
CASE fk.confdeltype
|
|
183
|
+
WHEN 'a' THEN 'NO ACTION'
|
|
184
|
+
WHEN 'r' THEN 'RESTRICT'
|
|
185
|
+
WHEN 'c' THEN 'CASCADE'
|
|
186
|
+
WHEN 'n' THEN 'SET NULL'
|
|
187
|
+
WHEN 'd' THEN 'SET DEFAULT'
|
|
188
|
+
END AS delete_rule
|
|
189
|
+
FROM fk
|
|
190
|
+
JOIN pg_class root ON root.oid = fk.root_oid
|
|
191
|
+
JOIN pg_class tgt ON tgt.oid = fk.confrelid
|
|
192
|
+
CROSS JOIN LATERAL unnest(fk.conkey) WITH ORDINALITY AS s(attnum, ord)
|
|
193
|
+
JOIN pg_attribute sa ON sa.attrelid = fk.conrelid AND sa.attnum = s.attnum
|
|
194
|
+
CROSS JOIN LATERAL unnest(fk.confkey) WITH ORDINALITY AS f(attnum, ord)
|
|
195
|
+
JOIN pg_attribute ta ON ta.attrelid = fk.confrelid AND ta.attnum = f.attnum
|
|
196
|
+
WHERE fk.pick = 1 AND s.ord = f.ord
|
|
197
|
+
ORDER BY root.relname, fk.conname, s.ord
|
|
198
|
+
`;
|
|
199
|
+
/**
|
|
200
|
+
* Unique constraints and unique indexes, as ordered column lists.
|
|
201
|
+
*
|
|
202
|
+
* Indexes rather than constraints alone, because `CREATE UNIQUE INDEX` and
|
|
203
|
+
* `ADD CONSTRAINT ... UNIQUE` produce the same guarantee and schemas use both.
|
|
204
|
+
* Partial indexes (`indpred IS NOT NULL`) are excluded: they promise uniqueness
|
|
205
|
+
* only over the rows matching their predicate, which is not the promise a
|
|
206
|
+
* `validation: { unique: true }` field makes.
|
|
207
|
+
*/
|
|
208
|
+
var UNIQUE_CONSTRAINTS_QUERY = `
|
|
209
|
+
SELECT t.relname AS table_name,
|
|
210
|
+
ic.relname AS constraint_name,
|
|
211
|
+
-- ::text, because the driver has no parser for an array of \`name\`
|
|
212
|
+
-- and hands back the raw \`{a,b}\` literal as a string. Every consumer
|
|
213
|
+
-- here indexes and compares it as an array, and a string of that
|
|
214
|
+
-- shape fails those silently rather than loudly.
|
|
215
|
+
array_agg(a.attname::text ORDER BY k.ord) AS column_names
|
|
216
|
+
FROM pg_index i
|
|
217
|
+
JOIN pg_class t ON t.oid = i.indrelid
|
|
218
|
+
JOIN pg_class ic ON ic.oid = i.indexrelid
|
|
219
|
+
JOIN pg_namespace n ON n.oid = t.relnamespace
|
|
220
|
+
CROSS JOIN LATERAL unnest(i.indkey) WITH ORDINALITY AS k(attnum, ord)
|
|
221
|
+
JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = k.attnum
|
|
222
|
+
WHERE i.indisunique
|
|
223
|
+
AND NOT i.indisprimary
|
|
224
|
+
AND i.indpred IS NULL
|
|
225
|
+
AND n.nspname = $1
|
|
226
|
+
AND NOT t.relispartition
|
|
227
|
+
GROUP BY t.relname, ic.relname
|
|
228
|
+
ORDER BY t.relname, ic.relname
|
|
229
|
+
`;
|
|
230
|
+
/**
|
|
231
|
+
* CHECK constraints, as the source text Postgres reproduces them from.
|
|
232
|
+
*
|
|
233
|
+
* `pg_get_constraintdef` output is normalized by the server — the parser in
|
|
234
|
+
* {@link ./introspect-db-constraints.ts} reads that normalized form, not
|
|
235
|
+
* whatever the author typed.
|
|
236
|
+
*/
|
|
237
|
+
var CHECK_CONSTRAINTS_QUERY = `
|
|
238
|
+
SELECT DISTINCT
|
|
239
|
+
root.relname AS table_name,
|
|
240
|
+
con.conname AS constraint_name,
|
|
241
|
+
pg_get_constraintdef(con.oid) AS definition
|
|
242
|
+
FROM pg_constraint con
|
|
243
|
+
JOIN pg_namespace n ON n.oid = con.connamespace
|
|
244
|
+
JOIN pg_class root ON root.oid = COALESCE(pg_partition_root(con.conrelid), con.conrelid)
|
|
245
|
+
WHERE con.contype = 'c'
|
|
246
|
+
AND n.nspname = $1
|
|
247
|
+
ORDER BY root.relname, con.conname
|
|
248
|
+
`;
|
|
249
|
+
/**
|
|
250
|
+
* `COMMENT ON TABLE` / `COMMENT ON COLUMN`, which is documentation the author
|
|
251
|
+
* already wrote and which nothing downstream has ever read.
|
|
252
|
+
*/
|
|
253
|
+
var COMMENTS_QUERY = `
|
|
254
|
+
SELECT t.relname AS table_name,
|
|
255
|
+
a.attname AS column_name,
|
|
256
|
+
d.description AS comment
|
|
257
|
+
FROM pg_description d
|
|
258
|
+
JOIN pg_class t ON t.oid = d.objoid
|
|
259
|
+
JOIN pg_namespace n ON n.oid = t.relnamespace
|
|
260
|
+
LEFT JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = d.objsubid
|
|
261
|
+
WHERE n.nspname = $1
|
|
262
|
+
AND t.relkind IN ('r', 'p')
|
|
263
|
+
AND (d.objsubid = 0 OR a.attname IS NOT NULL)
|
|
264
|
+
ORDER BY t.relname, d.objsubid
|
|
265
|
+
`;
|
|
266
|
+
/**
|
|
267
|
+
* Counts a table's rows, stopping once the answer can only be "more than
|
|
268
|
+
* `limit`".
|
|
269
|
+
*
|
|
270
|
+
* Introspection asks this to tell a small reference table from a table that
|
|
271
|
+
* merely looks like one (see `classifyTables`). `reltuples` would answer for
|
|
272
|
+
* free but lies on a table that has never been analyzed — which is every table
|
|
273
|
+
* in a database restored from a dump, i.e. exactly the case introspection meets
|
|
274
|
+
* — and reports -1 there rather than an error, so the lie is silent.
|
|
275
|
+
*
|
|
276
|
+
* The subquery's LIMIT caps the work at `limit + 1` rows however large the
|
|
277
|
+
* table is, so this stays cheap on a table with a billion rows.
|
|
278
|
+
*/
|
|
279
|
+
async function countRowsUpTo(client, schema, table, limit) {
|
|
280
|
+
const { rows } = await client.query(`SELECT count(*)::text AS count FROM (SELECT 1 FROM ${quoteRelation(schema, table)} LIMIT ${limit + 1}) probe`);
|
|
281
|
+
return Number(rows[0]?.count ?? 0);
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Reads everything the generator needs, in one pass.
|
|
285
|
+
*
|
|
286
|
+
* `rowCounts` is deliberately absent here: it costs a query per table and only
|
|
287
|
+
* a handful of tables can possibly need it. The caller fills it in for the
|
|
288
|
+
* candidates `lookupCandidates()` names.
|
|
289
|
+
*/
|
|
290
|
+
async function readSchemaMetadata(client, schema) {
|
|
291
|
+
const tables = await client.query(TABLES_QUERY, [schema]);
|
|
292
|
+
const columns = await client.query(COLUMNS_QUERY, [schema]);
|
|
293
|
+
const enumValues = await client.query(ENUMS_QUERY, [schema]);
|
|
294
|
+
const pks = await client.query(PRIMARY_KEYS_QUERY, [schema]);
|
|
295
|
+
const fks = await client.query(FOREIGN_KEYS_QUERY, [schema]);
|
|
296
|
+
const uniques = await client.query(UNIQUE_CONSTRAINTS_QUERY, [schema]);
|
|
297
|
+
const checks = await client.query(CHECK_CONSTRAINTS_QUERY, [schema]);
|
|
298
|
+
const comments = await client.query(COMMENTS_QUERY, [schema]);
|
|
299
|
+
const known = new Set(tables.rows.map((t) => t.table_name));
|
|
300
|
+
const scoped = (rows) => rows.filter((r) => known.has(r.table_name));
|
|
301
|
+
return {
|
|
302
|
+
schema,
|
|
303
|
+
tables: tables.rows,
|
|
304
|
+
columns: scoped(columns.rows),
|
|
305
|
+
enumValues: enumValues.rows,
|
|
306
|
+
pks: scoped(pks.rows),
|
|
307
|
+
fks: scoped(fks.rows).filter((fk) => known.has(fk.foreign_table_name)),
|
|
308
|
+
uniques: scoped(uniques.rows),
|
|
309
|
+
checks: scoped(checks.rows),
|
|
310
|
+
comments: scoped(comments.rows),
|
|
311
|
+
rowCounts: {}
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
//#endregion
|
|
4
315
|
//#region src/schema/introspect-db-naming.ts
|
|
5
316
|
/**
|
|
6
317
|
* Naming helpers shared by the introspection modules. These live apart from
|
|
@@ -300,6 +611,7 @@ function buildContext(metadata, tables) {
|
|
|
300
611
|
inboundByTable.set(constraint.foreignTable, inbound);
|
|
301
612
|
}
|
|
302
613
|
return {
|
|
614
|
+
schema: metadata.schema,
|
|
303
615
|
tables,
|
|
304
616
|
constraintsByTable,
|
|
305
617
|
inboundByTable,
|
|
@@ -319,9 +631,14 @@ function buildContext(metadata, tables) {
|
|
|
319
631
|
* with attributes, and collapsing it into a many-to-many silently drops those
|
|
320
632
|
* attributes from the UI entirely;
|
|
321
633
|
* - nothing references it. A junction with its own dependents is something rows
|
|
322
|
-
* point *at*, so it needs an identity of its own
|
|
634
|
+
* point *at*, so it needs an identity of its own;
|
|
635
|
+
* - it is in `public`. That is the only schema Rebase reads and creates
|
|
636
|
+
* junction tables in (`resolveJunctionSpecs`), so a `manyToMany` folded from
|
|
637
|
+
* `crm.contacts_tags` would read and write an empty `public.contacts_tags`
|
|
638
|
+
* instead. Kept as a table, its rows stay where they are and are served.
|
|
323
639
|
*/
|
|
324
640
|
function classifyJunction(table, context) {
|
|
641
|
+
if (context.schema !== "public") return null;
|
|
325
642
|
const meta = context.tables.get(table);
|
|
326
643
|
if (!meta) return null;
|
|
327
644
|
const constraints = context.constraintsByTable.get(table) ?? [];
|
|
@@ -1302,6 +1619,8 @@ function generateCollectionFile(tableName, meta, allFks, joinTables, tablesMap,
|
|
|
1302
1619
|
adminEntries.push(`propertiesOrder: ${formatKeyList(sortedPropertiesOrder)}`);
|
|
1303
1620
|
const adminBlock = emitAdmin ? `\n admin: {\n ${adminEntries.join(",\n ")}\n }` : "";
|
|
1304
1621
|
const descriptionBlock = tableComment ? `\n description: ${quote(tableComment)},` : "";
|
|
1622
|
+
const pgSchema = context.metadata?.schema;
|
|
1623
|
+
const schemaBlock = pgSchema && pgSchema !== "public" ? `\n schema: ${quote(pgSchema)},` : "";
|
|
1305
1624
|
const classificationNote = classification && classification.role !== "entity" ? `\n// Introspected as a ${commentText(classification.role)}: ${commentText(classification.reason)}.\n` : "";
|
|
1306
1625
|
const collectionVarName = toCollectionVarName(tableName);
|
|
1307
1626
|
const [open, close] = builder === "annotation" ? [`const ${collectionVarName}: PostgresCollectionConfig = {`, "};"] : [`const ${collectionVarName} = defineCollection({`, "});"];
|
|
@@ -1312,7 +1631,7 @@ ${open}
|
|
|
1312
1631
|
name: ${quote(collectionName)},
|
|
1313
1632
|
singularName: ${quote(singular)},
|
|
1314
1633
|
slug: ${quote(tableName)},
|
|
1315
|
-
table: ${quote(tableName)},${descriptionBlock}
|
|
1634
|
+
table: ${quote(tableName)},${schemaBlock}${descriptionBlock}
|
|
1316
1635
|
properties: {${propsOutput}
|
|
1317
1636
|
},${relationsBlock}${adminBlock}
|
|
1318
1637
|
${close}
|
|
@@ -1373,6 +1692,6 @@ function safeHostFromUrl(url) {
|
|
|
1373
1692
|
return url.includes("@") ? url.split("@")[1] : "(local connection)";
|
|
1374
1693
|
}
|
|
1375
1694
|
//#endregion
|
|
1376
|
-
export { generateCollectionFile as a, identifyJoinTables as c, singularize as d, classifyTables as f, humanize as h, buildTablesMap as i, mergeIndexContent as l, mapPgType as m, COMMON_PACKAGE as n, generateIndexContent as o, lookupCandidates as p, buildEnumMap as r, getIconForTable as s, ADMIN_TYPES_PACKAGE as t, safeHostFromUrl as u };
|
|
1695
|
+
export { isUsableFileName as _, generateCollectionFile as a, identifyJoinTables as c, singularize as d, classifyTables as f, countRowsUpTo as g, humanize as h, buildTablesMap as i, mergeIndexContent as l, mapPgType as m, COMMON_PACKAGE as n, generateIndexContent as o, lookupCandidates as p, buildEnumMap as r, getIconForTable as s, ADMIN_TYPES_PACKAGE as t, safeHostFromUrl as u, quoteRelation as v, readSchemaMetadata as y };
|
|
1377
1696
|
|
|
1378
|
-
//# sourceMappingURL=introspect-db-logic-
|
|
1697
|
+
//# sourceMappingURL=introspect-db-logic-WMuAfxvw.js.map
|