@absolutejs/dbml 0.2.0 → 0.3.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/CHANGELOG.md +13 -0
- package/README.md +45 -7
- package/changelog.json +44 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +1267 -121
- package/dist/drizzle/index.js +59 -8
- package/dist/drizzle/relations.d.ts +1 -0
- package/dist/index.js +66 -9
- package/dist/model.d.ts +10 -0
- package/dist/mysql/index.d.ts +20 -0
- package/dist/mysql/index.js +452 -0
- package/dist/postgres/details.d.ts +40 -0
- package/dist/postgres/index.d.ts +5 -0
- package/dist/postgres/index.js +367 -26
- package/dist/postgres/queries.d.ts +13 -4
- package/dist/postgres/rows.d.ts +66 -0
- package/dist/sqlite/index.d.ts +14 -0
- package/dist/sqlite/index.js +420 -0
- package/dist/sqlite/text.d.ts +22 -0
- package/package.json +16 -3
package/dist/postgres/index.js
CHANGED
|
@@ -50,6 +50,127 @@ var withDefaultSchema = (model, schema) => {
|
|
|
50
50
|
};
|
|
51
51
|
return result;
|
|
52
52
|
};
|
|
53
|
+
var groupBySchema = (tables, defaultSchema = "public") => {
|
|
54
|
+
const byName = new Map;
|
|
55
|
+
tables.forEach((table) => {
|
|
56
|
+
const name = table.schema ?? defaultSchema;
|
|
57
|
+
byName.set(name, [
|
|
58
|
+
...byName.get(name) ?? [],
|
|
59
|
+
{
|
|
60
|
+
...table.schema ? { schema: table.schema } : {},
|
|
61
|
+
table: table.name
|
|
62
|
+
}
|
|
63
|
+
]);
|
|
64
|
+
});
|
|
65
|
+
return [...byName].map(([name, members]) => {
|
|
66
|
+
const group = { name, tables: members };
|
|
67
|
+
return group;
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
var sortedColumns = (index) => index.parts.map((part) => part.kind === "column" ? part.column : "").sort().join("\x00");
|
|
71
|
+
var coversAll = (index) => index.pk === true || index.unique === true && index.note?.includes("Where ") !== true;
|
|
72
|
+
var isUniqueSet = (table, columns) => {
|
|
73
|
+
const [only] = columns;
|
|
74
|
+
const single = columns.length === 1 ? table.columns.find((column) => column.name === only) : undefined;
|
|
75
|
+
if (single?.pk === true || single?.unique === true)
|
|
76
|
+
return true;
|
|
77
|
+
const wanted = [...columns].sort().join("\x00");
|
|
78
|
+
return table.indexes.some((index) => coversAll(index) && index.parts.every((part) => part.kind === "column") && sortedColumns(index) === wanted);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// src/postgres/details.ts
|
|
82
|
+
var present = (lines) => lines.filter((line) => line !== undefined && line !== "");
|
|
83
|
+
var withMetadata = (metadata) => Object.keys(metadata).length > 0 ? { metadata } : {};
|
|
84
|
+
var DEFAULT_IDENTITY = "1 1";
|
|
85
|
+
var columnMetadata = (row) => {
|
|
86
|
+
const [start, increment] = (row.identitySequence ?? "").split(" ");
|
|
87
|
+
const identity = row.identitySequence && row.identitySequence !== DEFAULT_IDENTITY ? {
|
|
88
|
+
identity_increment: Number.parseInt(increment ?? "1", 10),
|
|
89
|
+
identity_start: Number.parseInt(start ?? "1", 10)
|
|
90
|
+
} : {};
|
|
91
|
+
const metadata = {
|
|
92
|
+
...row.collation ? { collation: row.collation } : {},
|
|
93
|
+
...row.compression ? { compression: row.compression } : {},
|
|
94
|
+
...row.domainBase ? { domain_of: row.domainBase } : {},
|
|
95
|
+
...identity
|
|
96
|
+
};
|
|
97
|
+
return withMetadata(metadata);
|
|
98
|
+
};
|
|
99
|
+
var nullsOrdering = (row, part, name) => {
|
|
100
|
+
const descending = row.descending[part] === true;
|
|
101
|
+
const nullsFirst = row.nullsFirst[part] === true;
|
|
102
|
+
if (descending === nullsFirst)
|
|
103
|
+
return;
|
|
104
|
+
return `${name} nulls ${nullsFirst ? "first" : "last"}`;
|
|
105
|
+
};
|
|
106
|
+
var indexNotes = (row, partName) => {
|
|
107
|
+
const parts = Array.from({ length: row.keyCount }, (_, part) => part);
|
|
108
|
+
const nulls = present(parts.map((part) => nullsOrdering(row, part, partName(part))));
|
|
109
|
+
const classes = present(parts.map((part) => row.opclasses[part] ? `${partName(part)} ${row.opclasses[part]}` : undefined));
|
|
110
|
+
const collations = present(parts.map((part) => row.collations[part] ? `${partName(part)} ${row.collations[part]}` : undefined));
|
|
111
|
+
return present([
|
|
112
|
+
nulls.length > 0 ? `Sorted with ${nulls.join(", ")}` : undefined,
|
|
113
|
+
classes.length > 0 ? `Operator class: ${classes.join(", ")}` : undefined,
|
|
114
|
+
collations.length > 0 ? `Collation: ${collations.join(", ")}` : undefined,
|
|
115
|
+
row.nullsNotDistinct ? "Nulls not distinct" : undefined
|
|
116
|
+
]);
|
|
117
|
+
};
|
|
118
|
+
var tableMetadata = (relation, statistics) => {
|
|
119
|
+
const metadata = {
|
|
120
|
+
...relation.owner ? { owner: relation.owner } : {},
|
|
121
|
+
...relation.grants ? { grants: relation.grants } : {},
|
|
122
|
+
...relation.persistence === "u" ? { unlogged: true } : {},
|
|
123
|
+
...relation.options ? { options: relation.options } : {},
|
|
124
|
+
...relation.foreignServer ? { foreign_server: relation.foreignServer } : {},
|
|
125
|
+
...relation.foreignOptions ? { foreign_options: relation.foreignOptions } : {},
|
|
126
|
+
...relation.inherits ? { inherits: relation.inherits } : {},
|
|
127
|
+
...statistics && relation.rowEstimate ? { rows: Number.parseInt(relation.rowEstimate, 10) } : {},
|
|
128
|
+
...statistics && relation.sizeBytes ? { size_bytes: Number.parseInt(relation.sizeBytes, 10) } : {}
|
|
129
|
+
};
|
|
130
|
+
return withMetadata(metadata);
|
|
131
|
+
};
|
|
132
|
+
var DISABLED = "D";
|
|
133
|
+
var definitionLine = (kind, entry) => {
|
|
134
|
+
const body = entry.definition.replace(/^CREATE (OR REPLACE )?(CONSTRAINT )?(TRIGGER|RULE) /u, "");
|
|
135
|
+
return `${kind} ${body}${entry.enabled === DISABLED ? " (disabled)" : ""}`;
|
|
136
|
+
};
|
|
137
|
+
var tableNoteLines = (input) => present([
|
|
138
|
+
input.inherits ? `Inherits from ${input.inherits}` : undefined,
|
|
139
|
+
...input.partitions.map((partition) => `Partition ${partition.name}: ${partition.bound}`),
|
|
140
|
+
...input.triggers.map((trigger) => definitionLine("Trigger", trigger)),
|
|
141
|
+
...input.rules.map((rule) => definitionLine("Rule", rule))
|
|
142
|
+
]);
|
|
143
|
+
var KINDS = {
|
|
144
|
+
a: "aggregate",
|
|
145
|
+
f: "function",
|
|
146
|
+
p: "procedure",
|
|
147
|
+
w: "window function"
|
|
148
|
+
};
|
|
149
|
+
var qualified = (schema, name, defaultSchema) => schema === defaultSchema ? name : `${schema}.${name}`;
|
|
150
|
+
var sticky = (name, title, lines) => {
|
|
151
|
+
if (lines.length === 0)
|
|
152
|
+
return [];
|
|
153
|
+
const note = {
|
|
154
|
+
content: `${title}
|
|
155
|
+
${lines.join(`
|
|
156
|
+
`)}`,
|
|
157
|
+
name
|
|
158
|
+
};
|
|
159
|
+
return [note];
|
|
160
|
+
};
|
|
161
|
+
var domainLine = (domain, defaultSchema) => [
|
|
162
|
+
`${qualified(domain.schema, domain.name, defaultSchema)}: ${domain.base}`,
|
|
163
|
+
domain.notNull ? "not null" : undefined,
|
|
164
|
+
domain.default ? `default ${domain.default}` : undefined,
|
|
165
|
+
domain.constraints
|
|
166
|
+
].filter((part) => part !== undefined).join(", ");
|
|
167
|
+
var extraNotes = (extras, defaultSchema) => [
|
|
168
|
+
...sticky("functions", "Functions and procedures", extras.functions.map((entry) => `${qualified(entry.schema, entry.name, defaultSchema)}(${entry.arguments})${entry.kind === "p" ? "" : ` \u2192 ${entry.result}`}, ${KINDS[entry.kind] ?? "function"} in ${entry.language}`)),
|
|
169
|
+
...sticky("domains", "Domains", extras.domains.map((domain) => domainLine(domain, defaultSchema))),
|
|
170
|
+
...sticky("composite_types", "Composite types", extras.composites.map((entry) => `${qualified(entry.schema, entry.name, defaultSchema)}: ${entry.fields}`)),
|
|
171
|
+
...sticky("extensions", "Extensions", extras.extensions.map((entry) => `${entry.name} ${entry.version}, in ${entry.schema}`)),
|
|
172
|
+
...sticky("event_triggers", "Event triggers", extras.eventTriggers.map((entry) => `${entry.name}: on ${entry.event}, runs ${entry.function}${entry.enabled === DISABLED ? " (disabled)" : ""}`))
|
|
173
|
+
];
|
|
53
174
|
|
|
54
175
|
// src/postgres/queries.ts
|
|
55
176
|
var SCHEMAS = `
|
|
@@ -65,6 +186,7 @@ from pg_type t
|
|
|
65
186
|
join pg_namespace n on n.oid = t.typnamespace
|
|
66
187
|
join pg_enum e on e.enumtypid = t.oid
|
|
67
188
|
where n.nspname = any($1)
|
|
189
|
+
and not exists (select 1 from pg_depend x where x.classid = 'pg_type'::regclass and x.objid = t.oid and x.deptype = 'e')
|
|
68
190
|
group by n.nspname, t.typname
|
|
69
191
|
order by n.nspname, t.typname`;
|
|
70
192
|
var RELATIONS = `
|
|
@@ -73,10 +195,23 @@ select c.oid::int as id, n.nspname as schema, c.relname as name, c.relkind as ki
|
|
|
73
195
|
c.relrowsecurity as rls, c.relforcerowsecurity as force_rls,
|
|
74
196
|
case when c.relkind = 'p' then pg_get_partkeydef(c.oid) end as partition_key,
|
|
75
197
|
case when c.relkind in ('v', 'm') then pg_get_viewdef(c.oid, true) end as definition,
|
|
76
|
-
(select count(*)::int from pg_inherits i where i.inhparent = c.oid) as partitions
|
|
198
|
+
(select count(*)::int from pg_inherits i where i.inhparent = c.oid) as partitions,
|
|
199
|
+
pg_get_userbyid(c.relowner) as owner,
|
|
200
|
+
c.relpersistence as persistence,
|
|
201
|
+
array_to_string(c.reloptions, ', ') as options,
|
|
202
|
+
case when c.reltuples >= 0 then c.reltuples::bigint::text end as row_estimate,
|
|
203
|
+
case when c.relkind in ('r', 'p', 'm') then pg_total_relation_size(c.oid)::text end as size_bytes,
|
|
204
|
+
(select string_agg(g.privilege_type || ' to ' || case when g.grantee = 0 then 'public' else pg_get_userbyid(g.grantee) end, ', ' order by g.grantee, g.privilege_type)
|
|
205
|
+
from aclexplode(c.relacl) as g where g.grantee <> c.relowner) as grants,
|
|
206
|
+
(select s.srvname from pg_foreign_table ft join pg_foreign_server s on s.oid = ft.ftserver where ft.ftrelid = c.oid) as foreign_server,
|
|
207
|
+
(select array_to_string(ft.ftoptions, ', ') from pg_foreign_table ft where ft.ftrelid = c.oid) as foreign_options,
|
|
208
|
+
(select string_agg(quote_ident(pn.nspname) || '.' || quote_ident(p.relname), ', ')
|
|
209
|
+
from pg_inherits i join pg_class p on p.oid = i.inhparent join pg_namespace pn on pn.oid = p.relnamespace
|
|
210
|
+
where i.inhrelid = c.oid and p.relkind <> 'p') as inherits
|
|
77
211
|
from pg_class c
|
|
78
212
|
join pg_namespace n on n.oid = c.relnamespace
|
|
79
213
|
where n.nspname = any($1) and c.relkind in ('r', 'p', 'v', 'm', 'f') and not c.relispartition
|
|
214
|
+
and not exists (select 1 from pg_depend e where e.classid = 'pg_class'::regclass and e.objid = c.oid and e.deptype = 'e')
|
|
80
215
|
order by n.nspname, c.relname`;
|
|
81
216
|
var COLUMNS = `
|
|
82
217
|
select a.attrelid::int as table_id, a.attnum::int as position, a.attname as name,
|
|
@@ -88,7 +223,12 @@ select a.attrelid::int as table_id, a.attnum::int as position, a.attname as name
|
|
|
88
223
|
when et.typtype = 'e' then etn.nspname end as enum_schema,
|
|
89
224
|
coalesce(case when t.typtype = 'e' then t.typname end,
|
|
90
225
|
case when et.typtype = 'e' then et.typname || '[]' end) as enum_type,
|
|
91
|
-
pg_get_serial_sequence(quote_ident(n.nspname) || '.' || quote_ident(c.relname), a.attname) as serial_sequence
|
|
226
|
+
pg_get_serial_sequence(quote_ident(n.nspname) || '.' || quote_ident(c.relname), a.attname) as serial_sequence,
|
|
227
|
+
case when a.attcollation <> 0 and a.attcollation <> t.typcollation then (select collname from pg_collation where oid = a.attcollation) end as collation,
|
|
228
|
+
case to_jsonb(a) ->> 'attcompression' when 'p' then 'pglz' when 'l' then 'lz4' end as compression,
|
|
229
|
+
case when a.attidentity <> '' then (select s.seqstart::text || ' ' || s.seqincrement::text from pg_sequence s
|
|
230
|
+
where s.seqrelid = pg_get_serial_sequence(quote_ident(n.nspname) || '.' || quote_ident(c.relname), a.attname)::regclass) end as identity_sequence,
|
|
231
|
+
case when t.typtype = 'd' then format_type(t.typbasetype, t.typtypmod) end as domain_base
|
|
92
232
|
from pg_attribute a
|
|
93
233
|
join pg_class c on c.oid = a.attrelid
|
|
94
234
|
join pg_namespace n on n.oid = c.relnamespace
|
|
@@ -126,6 +266,17 @@ select i.indrelid::int as table_id, ic.relname as name, am.amname as method,
|
|
|
126
266
|
from generate_series(1, i.indnatts) as k) as attnums,
|
|
127
267
|
(select array_agg((i.indoption[k - 1] & 1) = 1 order by k)
|
|
128
268
|
from generate_series(1, i.indnkeyatts) as k) as descending,
|
|
269
|
+
(select array_agg((i.indoption[k - 1] & 2) = 2 order by k)
|
|
270
|
+
from generate_series(1, i.indnkeyatts) as k) as nulls_first,
|
|
271
|
+
/* An operator class or collation that isn't the column's own, per key
|
|
272
|
+
* part, empty where it is. */
|
|
273
|
+
(select array_agg(case when opc.opcdefault then '' else opc.opcname end order by k)
|
|
274
|
+
from generate_series(1, i.indnkeyatts) as k join pg_opclass opc on opc.oid = i.indclass[k - 1]) as opclasses,
|
|
275
|
+
(select array_agg(coalesce((select co.collname from pg_collation co where co.oid = i.indcollation[k - 1]
|
|
276
|
+
and i.indcollation[k - 1] <> coalesce((select a.attcollation from pg_attribute a where a.attrelid = i.indrelid and a.attnum = i.indkey[k - 1]), 0)
|
|
277
|
+
and co.collname <> 'default'), '') order by k)
|
|
278
|
+
from generate_series(1, i.indnkeyatts) as k) as collations,
|
|
279
|
+
(to_jsonb(i) ->> 'indnullsnotdistinct')::boolean as nulls_not_distinct,
|
|
129
280
|
pg_get_expr(i.indpred, i.indrelid, true) as predicate,
|
|
130
281
|
obj_description(i.indexrelid, 'pg_class') as comment
|
|
131
282
|
from pg_index i
|
|
@@ -167,6 +318,81 @@ order by n.nspname, c.relname`;
|
|
|
167
318
|
var SCHEMA_COMMENTS = `
|
|
168
319
|
select nspname as name, obj_description(oid, 'pg_namespace') as comment
|
|
169
320
|
from pg_namespace where nspname = any($1)`;
|
|
321
|
+
var TRIGGERS = `
|
|
322
|
+
select t.tgrelid::int as table_id, t.tgname as name, pg_get_triggerdef(t.oid, true) as definition,
|
|
323
|
+
t.tgenabled as enabled
|
|
324
|
+
from pg_trigger t
|
|
325
|
+
join pg_class c on c.oid = t.tgrelid
|
|
326
|
+
join pg_namespace n on n.oid = c.relnamespace
|
|
327
|
+
where n.nspname = any($1) and not t.tgisinternal
|
|
328
|
+
order by t.tgrelid, t.tgname`;
|
|
329
|
+
var RULES = `
|
|
330
|
+
select r.ev_class::int as table_id, r.rulename as name, pg_get_ruledef(r.oid, true) as definition
|
|
331
|
+
from pg_rewrite r
|
|
332
|
+
join pg_class c on c.oid = r.ev_class
|
|
333
|
+
join pg_namespace n on n.oid = c.relnamespace
|
|
334
|
+
where n.nspname = any($1) and r.rulename <> '_RETURN'
|
|
335
|
+
order by r.ev_class, r.rulename`;
|
|
336
|
+
var FUNCTIONS = `
|
|
337
|
+
select n.nspname as schema, p.proname as name, p.prokind as kind,
|
|
338
|
+
pg_get_function_identity_arguments(p.oid) as arguments,
|
|
339
|
+
pg_get_function_result(p.oid) as result, l.lanname as language
|
|
340
|
+
from pg_proc p
|
|
341
|
+
join pg_namespace n on n.oid = p.pronamespace
|
|
342
|
+
join pg_language l on l.oid = p.prolang
|
|
343
|
+
where n.nspname = any($1)
|
|
344
|
+
and not exists (select 1 from pg_depend e where e.classid = 'pg_proc'::regclass and e.objid = p.oid and e.deptype = 'e')
|
|
345
|
+
order by n.nspname, p.proname, arguments`;
|
|
346
|
+
var DOMAINS = `
|
|
347
|
+
select n.nspname as schema, t.typname as name, format_type(t.typbasetype, t.typtypmod) as base,
|
|
348
|
+
t.typnotnull as not_null, t.typdefault as "default",
|
|
349
|
+
(select string_agg(pg_get_constraintdef(con.oid, true), ' and ' order by con.conname)
|
|
350
|
+
/* Postgres 17 also keeps NOT NULL as a constraint; typnotnull says it. */
|
|
351
|
+
from pg_constraint con where con.contypid = t.oid and con.contype <> 'n') as constraints
|
|
352
|
+
from pg_type t
|
|
353
|
+
join pg_namespace n on n.oid = t.typnamespace
|
|
354
|
+
where n.nspname = any($1) and t.typtype = 'd'
|
|
355
|
+
and not exists (select 1 from pg_depend e where e.classid = 'pg_type'::regclass and e.objid = t.oid and e.deptype = 'e')
|
|
356
|
+
order by n.nspname, t.typname`;
|
|
357
|
+
var COMPOSITES = `
|
|
358
|
+
select n.nspname as schema, t.typname as name,
|
|
359
|
+
string_agg(a.attname || ' ' || format_type(a.atttypid, a.atttypmod), ', ' order by a.attnum) as fields
|
|
360
|
+
from pg_type t
|
|
361
|
+
join pg_namespace n on n.oid = t.typnamespace
|
|
362
|
+
join pg_class c on c.oid = t.typrelid and c.relkind = 'c'
|
|
363
|
+
join pg_attribute a on a.attrelid = c.oid and a.attnum > 0 and not a.attisdropped
|
|
364
|
+
where n.nspname = any($1)
|
|
365
|
+
and not exists (select 1 from pg_depend e where e.classid = 'pg_type'::regclass and e.objid = t.oid and e.deptype = 'e')
|
|
366
|
+
group by n.nspname, t.typname
|
|
367
|
+
order by n.nspname, t.typname`;
|
|
368
|
+
var EXTENSIONS = `
|
|
369
|
+
select e.extname as name, e.extversion as version, n.nspname as schema
|
|
370
|
+
from pg_extension e
|
|
371
|
+
join pg_namespace n on n.oid = e.extnamespace
|
|
372
|
+
where e.extname <> 'plpgsql'
|
|
373
|
+
order by e.extname`;
|
|
374
|
+
var EVENT_TRIGGERS = `
|
|
375
|
+
select evtname as name, evtevent as event, evtfoid::regproc::text as function, evtenabled as enabled
|
|
376
|
+
from pg_event_trigger
|
|
377
|
+
order by evtname`;
|
|
378
|
+
var PARTITIONS = `
|
|
379
|
+
select i.inhparent::int as table_id, c.relname as name, pg_get_expr(c.relpartbound, c.oid, true) as bound
|
|
380
|
+
from pg_inherits i
|
|
381
|
+
join pg_class c on c.oid = i.inhrelid
|
|
382
|
+
join pg_class p on p.oid = i.inhparent
|
|
383
|
+
join pg_namespace n on n.oid = p.relnamespace
|
|
384
|
+
where n.nspname = any($1) and p.relkind = 'p'
|
|
385
|
+
order by i.inhparent, c.relname`;
|
|
386
|
+
var VIEW_COLUMNS = `
|
|
387
|
+
select distinct v.oid::int as view_id, d.refobjid::int as source_id, a.attname as name
|
|
388
|
+
from pg_class v
|
|
389
|
+
join pg_namespace n on n.oid = v.relnamespace
|
|
390
|
+
join pg_rewrite r on r.ev_class = v.oid
|
|
391
|
+
join pg_depend d on d.objid = r.oid and d.classid = 'pg_rewrite'::regclass
|
|
392
|
+
and d.refclassid = 'pg_class'::regclass and d.refobjid <> v.oid and d.refobjsubid > 0
|
|
393
|
+
join pg_attribute a on a.attrelid = d.refobjid and a.attnum = d.refobjsubid
|
|
394
|
+
where n.nspname = any($1) and v.relkind in ('v', 'm')
|
|
395
|
+
order by view_id, source_id, name`;
|
|
170
396
|
|
|
171
397
|
// src/postgres/rows.ts
|
|
172
398
|
var optional = (key, value) => {
|
|
@@ -205,11 +431,20 @@ var relationRow = (row) => {
|
|
|
205
431
|
kind: text(row.kind) ?? "",
|
|
206
432
|
name: text(row.name) ?? "",
|
|
207
433
|
partitions: integer(row.partitions),
|
|
434
|
+
persistence: text(row.persistence) ?? "p",
|
|
208
435
|
rls: flag(row.rls),
|
|
209
436
|
schema: text(row.schema) ?? "",
|
|
210
437
|
...optional("comment", text(row.comment)),
|
|
211
438
|
...optional("definition", text(row.definition)),
|
|
212
|
-
...optional("
|
|
439
|
+
...optional("foreignOptions", setting(row.foreign_options)),
|
|
440
|
+
...optional("foreignServer", text(row.foreign_server)),
|
|
441
|
+
...optional("grants", text(row.grants)),
|
|
442
|
+
...optional("inherits", text(row.inherits)),
|
|
443
|
+
...optional("options", setting(row.options)),
|
|
444
|
+
...optional("owner", text(row.owner)),
|
|
445
|
+
...optional("partitionKey", text(row.partition_key)),
|
|
446
|
+
...optional("rowEstimate", text(row.row_estimate)),
|
|
447
|
+
...optional("sizeBytes", text(row.size_bytes))
|
|
213
448
|
};
|
|
214
449
|
return result;
|
|
215
450
|
};
|
|
@@ -224,12 +459,16 @@ var columnRow = (row) => {
|
|
|
224
459
|
position: integer(row.position),
|
|
225
460
|
tableId: integer(row.table_id),
|
|
226
461
|
type: text(row.type) ?? "",
|
|
462
|
+
...optional("collation", text(row.collation)),
|
|
227
463
|
...optional("comment", text(row.comment)),
|
|
464
|
+
...optional("compression", text(row.compression)),
|
|
228
465
|
...optional("default", text(row.default)),
|
|
466
|
+
...optional("domainBase", text(row.domain_base)),
|
|
229
467
|
...optional("enumSchema", text(row.enum_schema)),
|
|
230
468
|
...optional("enumType", text(row.enum_type)),
|
|
231
469
|
...optional("generated", setting(row.generated)),
|
|
232
470
|
...optional("identity", setting(row.identity)),
|
|
471
|
+
...optional("identitySequence", text(row.identity_sequence)),
|
|
233
472
|
...optional("serialSequence", text(row.serial_sequence))
|
|
234
473
|
};
|
|
235
474
|
return result;
|
|
@@ -256,10 +495,14 @@ var constraintRow = (row) => {
|
|
|
256
495
|
var indexRow = (row) => {
|
|
257
496
|
const result = {
|
|
258
497
|
attnums: integers(row.attnums),
|
|
498
|
+
collations: list(row.collations),
|
|
259
499
|
descending: flags(row.descending),
|
|
260
500
|
keyCount: integer(row.key_count),
|
|
261
501
|
method: text(row.method) ?? "btree",
|
|
262
502
|
name: text(row.name) ?? "",
|
|
503
|
+
nullsFirst: flags(row.nulls_first),
|
|
504
|
+
nullsNotDistinct: flag(row.nulls_not_distinct),
|
|
505
|
+
opclasses: list(row.opclasses),
|
|
263
506
|
parts: list(row.parts),
|
|
264
507
|
tableId: integer(row.table_id),
|
|
265
508
|
unique: flag(row.is_unique),
|
|
@@ -303,6 +546,54 @@ var viewSourceRow = (row) => ({
|
|
|
303
546
|
sourceId: integer(row.source_id),
|
|
304
547
|
viewId: integer(row.view_id)
|
|
305
548
|
});
|
|
549
|
+
var definitionRow = (row) => ({
|
|
550
|
+
definition: text(row.definition) ?? "",
|
|
551
|
+
enabled: text(row.enabled) ?? "O",
|
|
552
|
+
name: text(row.name) ?? "",
|
|
553
|
+
tableId: integer(row.table_id)
|
|
554
|
+
});
|
|
555
|
+
var functionRow = (row) => ({
|
|
556
|
+
arguments: text(row.arguments) ?? "",
|
|
557
|
+
kind: text(row.kind) ?? "f",
|
|
558
|
+
language: text(row.language) ?? "",
|
|
559
|
+
name: text(row.name) ?? "",
|
|
560
|
+
result: text(row.result) ?? "",
|
|
561
|
+
schema: text(row.schema) ?? ""
|
|
562
|
+
});
|
|
563
|
+
var domainRow = (row) => ({
|
|
564
|
+
base: text(row.base) ?? "",
|
|
565
|
+
constraints: text(row.constraints),
|
|
566
|
+
default: text(row.default),
|
|
567
|
+
name: text(row.name) ?? "",
|
|
568
|
+
notNull: flag(row.not_null),
|
|
569
|
+
schema: text(row.schema) ?? ""
|
|
570
|
+
});
|
|
571
|
+
var compositeRow = (row) => ({
|
|
572
|
+
fields: text(row.fields) ?? "",
|
|
573
|
+
name: text(row.name) ?? "",
|
|
574
|
+
schema: text(row.schema) ?? ""
|
|
575
|
+
});
|
|
576
|
+
var extensionRow = (row) => ({
|
|
577
|
+
name: text(row.name) ?? "",
|
|
578
|
+
schema: text(row.schema) ?? "",
|
|
579
|
+
version: text(row.version) ?? ""
|
|
580
|
+
});
|
|
581
|
+
var eventTriggerRow = (row) => ({
|
|
582
|
+
enabled: text(row.enabled) ?? "O",
|
|
583
|
+
event: text(row.event) ?? "",
|
|
584
|
+
function: text(row.function) ?? "",
|
|
585
|
+
name: text(row.name) ?? ""
|
|
586
|
+
});
|
|
587
|
+
var partitionRow = (row) => ({
|
|
588
|
+
bound: text(row.bound) ?? "",
|
|
589
|
+
name: text(row.name) ?? "",
|
|
590
|
+
tableId: integer(row.table_id)
|
|
591
|
+
});
|
|
592
|
+
var viewColumnRow = (row) => ({
|
|
593
|
+
name: text(row.name) ?? "",
|
|
594
|
+
sourceId: integer(row.source_id),
|
|
595
|
+
viewId: integer(row.view_id)
|
|
596
|
+
});
|
|
306
597
|
|
|
307
598
|
// src/postgres/index.ts
|
|
308
599
|
var DEFAULT_SCHEMA = "public";
|
|
@@ -356,7 +647,7 @@ var parseDefault = (expression) => {
|
|
|
356
647
|
return quotedDefault(inner, type ?? "", expression);
|
|
357
648
|
return { kind: "expression", value: expression };
|
|
358
649
|
};
|
|
359
|
-
var
|
|
650
|
+
var KINDS2 = {
|
|
360
651
|
f: "Foreign table",
|
|
361
652
|
m: "Materialized view",
|
|
362
653
|
p: "Partitioned table",
|
|
@@ -401,6 +692,7 @@ var columnOf = (row) => {
|
|
|
401
692
|
...serial || row.identity ? { increment: true } : {},
|
|
402
693
|
...row.notNull ? { notNull: true } : {},
|
|
403
694
|
...fallback ? { default: fallback } : {},
|
|
695
|
+
...columnMetadata(row),
|
|
404
696
|
...withNote(joinNotes([
|
|
405
697
|
row.comment,
|
|
406
698
|
identityNote(row.identity),
|
|
@@ -448,15 +740,15 @@ var applyKey = (table, row, columns) => {
|
|
|
448
740
|
};
|
|
449
741
|
table.indexes.push(index);
|
|
450
742
|
};
|
|
451
|
-
var
|
|
452
|
-
var
|
|
453
|
-
var
|
|
743
|
+
var sortedColumns2 = (index) => index.parts.map((part) => part.kind === "column" ? part.column : "").sort().join("\x00");
|
|
744
|
+
var coversAll2 = (index) => index.pk === true || index.unique === true && index.note?.includes("Where ") !== true;
|
|
745
|
+
var isUniqueSet2 = (table, columns) => {
|
|
454
746
|
const [only] = columns;
|
|
455
747
|
const single = columns.length === 1 ? table.columns.find((column) => column.name === only) : undefined;
|
|
456
748
|
if (single?.pk === true || single?.unique === true)
|
|
457
749
|
return true;
|
|
458
750
|
const wanted = [...columns].sort().join("\x00");
|
|
459
|
-
return table.indexes.some((index) =>
|
|
751
|
+
return table.indexes.some((index) => coversAll2(index) && index.parts.every((part) => part.kind === "column") && sortedColumns2(index) === wanted);
|
|
460
752
|
};
|
|
461
753
|
var refOf = (row, context) => {
|
|
462
754
|
const source = context.relations.get(row.tableId);
|
|
@@ -472,7 +764,7 @@ var refOf = (row, context) => {
|
|
|
472
764
|
...row.name === defaultName(row, source.name, columns) ? {} : { name: row.name },
|
|
473
765
|
...onDelete ? { onDelete } : {},
|
|
474
766
|
...onUpdate ? { onUpdate } : {},
|
|
475
|
-
relation:
|
|
767
|
+
relation: isUniqueSet2(table, columns) ? "-" : ">",
|
|
476
768
|
to: {
|
|
477
769
|
columns: row.foreignColumns.map((position) => context.columnName(target.id, position)),
|
|
478
770
|
...inSchema(target.schema),
|
|
@@ -507,7 +799,11 @@ var indexOf = (row, context) => {
|
|
|
507
799
|
...withNote(joinNotes([
|
|
508
800
|
row.comment,
|
|
509
801
|
row.predicate ? `Where ${row.predicate}` : undefined,
|
|
510
|
-
included.length > 0 ? `Includes ${included.join(", ")}` : undefined
|
|
802
|
+
included.length > 0 ? `Includes ${included.join(", ")}` : undefined,
|
|
803
|
+
...indexNotes(row, (part) => {
|
|
804
|
+
const position = row.attnums[part] ?? 0;
|
|
805
|
+
return position > 0 ? context.columnName(row.tableId, position) : row.parts[part] ?? "";
|
|
806
|
+
})
|
|
511
807
|
]))
|
|
512
808
|
};
|
|
513
809
|
return index;
|
|
@@ -522,7 +818,7 @@ var policyLine = (row) => [
|
|
|
522
818
|
].filter((part) => part !== undefined).join(" ");
|
|
523
819
|
var MAX_DEFINITION = 600;
|
|
524
820
|
var kindLine = (relation) => {
|
|
525
|
-
const kind =
|
|
821
|
+
const kind = KINDS2[relation.kind];
|
|
526
822
|
const definition = relation.definition?.trim();
|
|
527
823
|
if (!kind || !definition)
|
|
528
824
|
return kind;
|
|
@@ -530,10 +826,11 @@ var kindLine = (relation) => {
|
|
|
530
826
|
return `${kind}:
|
|
531
827
|
${shown}`;
|
|
532
828
|
};
|
|
533
|
-
var tableNote = (relation, policies) => joinNotes([
|
|
829
|
+
var tableNote = (relation, policies, extra) => joinNotes([
|
|
534
830
|
relation.comment,
|
|
535
831
|
kindLine(relation),
|
|
536
|
-
relation.partitionKey ? `Partitioned by ${relation.partitionKey}
|
|
832
|
+
relation.partitionKey ? `Partitioned by ${relation.partitionKey}` : undefined,
|
|
833
|
+
...extra,
|
|
537
834
|
relation.rls ? `Row-level security is on${relation.forceRls ? ", for the owner too" : ""}` : undefined,
|
|
538
835
|
...policies.map(policyLine)
|
|
539
836
|
]);
|
|
@@ -565,16 +862,23 @@ var readRecords = (query, tables, limit) => Promise.all(tables.map(async (table)
|
|
|
565
862
|
};
|
|
566
863
|
return records;
|
|
567
864
|
}));
|
|
568
|
-
var tableOf = (relation,
|
|
865
|
+
var tableOf = (relation, catalogs, statistics) => {
|
|
866
|
+
const own = (rows) => rows.filter((row) => row.tableId === relation.id);
|
|
569
867
|
const view = relation.kind === "v" || relation.kind === "m";
|
|
570
868
|
const table = {
|
|
571
869
|
checks: [],
|
|
572
|
-
columns:
|
|
870
|
+
columns: own(catalogs.columns).map(columnOf),
|
|
573
871
|
...view ? { headerColor: VIEW_COLOR } : {},
|
|
574
872
|
indexes: [],
|
|
575
873
|
name: relation.name,
|
|
576
874
|
...inSchema(relation.schema),
|
|
577
|
-
...
|
|
875
|
+
...tableMetadata(relation, statistics),
|
|
876
|
+
...withNote(tableNote(relation, own(catalogs.policies), tableNoteLines({
|
|
877
|
+
...relation.inherits ? { inherits: relation.inherits } : {},
|
|
878
|
+
partitions: own(catalogs.partitions),
|
|
879
|
+
rules: own(catalogs.rules),
|
|
880
|
+
triggers: own(catalogs.triggers)
|
|
881
|
+
})))
|
|
578
882
|
};
|
|
579
883
|
return table;
|
|
580
884
|
};
|
|
@@ -615,7 +919,16 @@ var readCatalogs = async (query, schemas) => {
|
|
|
615
919
|
sources,
|
|
616
920
|
policies,
|
|
617
921
|
sequences,
|
|
618
|
-
comments
|
|
922
|
+
comments,
|
|
923
|
+
triggers,
|
|
924
|
+
rules,
|
|
925
|
+
functions,
|
|
926
|
+
domains,
|
|
927
|
+
composites,
|
|
928
|
+
partitions,
|
|
929
|
+
viewColumns,
|
|
930
|
+
extensions,
|
|
931
|
+
eventTriggers
|
|
619
932
|
] = await Promise.all([
|
|
620
933
|
read(ENUMS),
|
|
621
934
|
read(RELATIONS),
|
|
@@ -625,33 +938,58 @@ var readCatalogs = async (query, schemas) => {
|
|
|
625
938
|
read(VIEW_SOURCES),
|
|
626
939
|
read(POLICIES),
|
|
627
940
|
read(SEQUENCES),
|
|
628
|
-
read(SCHEMA_COMMENTS)
|
|
941
|
+
read(SCHEMA_COMMENTS),
|
|
942
|
+
read(TRIGGERS),
|
|
943
|
+
read(RULES),
|
|
944
|
+
read(FUNCTIONS),
|
|
945
|
+
read(DOMAINS),
|
|
946
|
+
read(COMPOSITES),
|
|
947
|
+
read(PARTITIONS),
|
|
948
|
+
read(VIEW_COLUMNS),
|
|
949
|
+
query(EXTENSIONS, []),
|
|
950
|
+
query(EVENT_TRIGGERS, [])
|
|
629
951
|
]);
|
|
630
952
|
return {
|
|
631
953
|
columns: columns.map(columnRow),
|
|
632
954
|
comments: comments.map(namedRow),
|
|
955
|
+
composites: composites.map(compositeRow),
|
|
633
956
|
constraints: constraints.map(constraintRow),
|
|
957
|
+
domains: domains.map(domainRow),
|
|
634
958
|
enums: enums.map(enumRow),
|
|
959
|
+
eventTriggers: eventTriggers.map(eventTriggerRow),
|
|
960
|
+
extensions: extensions.map(extensionRow),
|
|
961
|
+
functions: functions.map(functionRow),
|
|
635
962
|
indexes: indexes.map(indexRow),
|
|
963
|
+
partitions: partitions.map(partitionRow),
|
|
636
964
|
policies: policies.map(policyRow),
|
|
637
965
|
relations: relations.map(relationRow),
|
|
966
|
+
rules: rules.map(definitionRow),
|
|
638
967
|
sequences: sequences.map(sequenceRow),
|
|
639
|
-
sources: sources.map(viewSourceRow)
|
|
968
|
+
sources: sources.map(viewSourceRow),
|
|
969
|
+
triggers: triggers.map(definitionRow),
|
|
970
|
+
viewColumns: viewColumns.map(viewColumnRow)
|
|
640
971
|
};
|
|
641
972
|
};
|
|
642
973
|
var schemasToRead = async (query, schemas) => schemas ?? (await query(SCHEMAS, [])).map((row) => namedRow(row).name);
|
|
643
974
|
var addDeps = (model, catalogs, relations) => catalogs.sources.forEach((row) => {
|
|
644
975
|
const view = relations.get(row.viewId);
|
|
645
976
|
const source = relations.get(row.sourceId);
|
|
646
|
-
if (view
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
}
|
|
977
|
+
if (!view || !source)
|
|
978
|
+
return;
|
|
979
|
+
const columns = catalogs.viewColumns.filter((entry) => entry.viewId === row.viewId && entry.sourceId === row.sourceId).map((entry) => entry.name);
|
|
980
|
+
model.deps.push({
|
|
981
|
+
downstream: { ...inSchema(view.schema), table: view.name },
|
|
982
|
+
upstream: {
|
|
983
|
+
...columns.length > 0 ? { columns } : {},
|
|
984
|
+
...inSchema(source.schema),
|
|
985
|
+
table: source.name
|
|
986
|
+
}
|
|
987
|
+
});
|
|
651
988
|
});
|
|
652
989
|
var addNotes = (model, catalogs, defaultSchema) => {
|
|
653
990
|
if (catalogs.sequences.length > 0)
|
|
654
991
|
model.notes.push(sequenceNote(catalogs.sequences, defaultSchema));
|
|
992
|
+
model.notes.push(...extraNotes(catalogs, defaultSchema));
|
|
655
993
|
const described = catalogs.comments.filter((row) => row.comment !== undefined && row.name !== defaultSchema);
|
|
656
994
|
if (described.length > 0)
|
|
657
995
|
model.notes.push({
|
|
@@ -679,7 +1017,7 @@ var fromPostgres = async (query, options = {}) => {
|
|
|
679
1017
|
]));
|
|
680
1018
|
const tables = new Map(catalogs.relations.map((row) => [
|
|
681
1019
|
row.id,
|
|
682
|
-
tableOf(row, catalogs
|
|
1020
|
+
tableOf(row, catalogs, options.statistics === true)
|
|
683
1021
|
]));
|
|
684
1022
|
const context = {
|
|
685
1023
|
relations,
|
|
@@ -696,7 +1034,10 @@ var fromPostgres = async (query, options = {}) => {
|
|
|
696
1034
|
const limit = options.records?.limit ?? 0;
|
|
697
1035
|
if (limit > 0)
|
|
698
1036
|
model.records.push(...await readRecords(query, model.tables.filter((table) => !table.headerColor), limit));
|
|
699
|
-
|
|
1037
|
+
const result = withDefaultSchema(model, defaultSchema);
|
|
1038
|
+
if (options.groups === "schema")
|
|
1039
|
+
result.tableGroups.push(...groupBySchema(result.tables, defaultSchema));
|
|
1040
|
+
return result;
|
|
700
1041
|
};
|
|
701
1042
|
export {
|
|
702
1043
|
fromPostgres,
|
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
export declare const SCHEMAS = "\nselect nspname as name\nfrom pg_namespace\nwhere nspname not in ('pg_catalog', 'information_schema', 'pg_toast')\n\tand nspname not like 'pg_temp_%' and nspname not like 'pg_toast_temp_%'\norder by nspname";
|
|
2
|
-
export declare const ENUMS = "\nselect n.nspname as schema, t.typname as name,\n\tarray_agg(e.enumlabel order by e.enumsortorder) as \"values\"\nfrom pg_type t\njoin pg_namespace n on n.oid = t.typnamespace\njoin pg_enum e on e.enumtypid = t.oid\nwhere n.nspname = any($1)\ngroup by n.nspname, t.typname\norder by n.nspname, t.typname";
|
|
3
|
-
export declare const RELATIONS = "\nselect c.oid::int as id, n.nspname as schema, c.relname as name, c.relkind as kind,\n\tobj_description(c.oid, 'pg_class') as comment,\n\tc.relrowsecurity as rls, c.relforcerowsecurity as force_rls,\n\tcase when c.relkind = 'p' then pg_get_partkeydef(c.oid) end as partition_key,\n\tcase when c.relkind in ('v', 'm') then pg_get_viewdef(c.oid, true) end as definition,\n\t(select count(*)::int from pg_inherits i where i.inhparent = c.oid) as partitions\nfrom pg_class c\njoin pg_namespace n on n.oid = c.relnamespace\nwhere n.nspname = any($1) and c.relkind in ('r', 'p', 'v', 'm', 'f') and not c.relispartition\norder by n.nspname, c.relname";
|
|
4
|
-
export declare const COLUMNS = "\nselect a.attrelid::int as table_id, a.attnum::int as position, a.attname as name,\n\tformat_type(a.atttypid, a.atttypmod) as type,\n\ta.attnotnull as not_null, a.attidentity as identity, a.attgenerated as generated,\n\tpg_get_expr(d.adbin, d.adrelid) as \"default\",\n\tcol_description(a.attrelid, a.attnum) as comment,\n\tcase when t.typtype = 'e' then tn.nspname\n\t\twhen et.typtype = 'e' then etn.nspname end as enum_schema,\n\tcoalesce(case when t.typtype = 'e' then t.typname end,\n\t\tcase when et.typtype = 'e' then et.typname || '[]' end) as enum_type,\n\tpg_get_serial_sequence(quote_ident(n.nspname) || '.' || quote_ident(c.relname), a.attname) as serial_sequence\nfrom pg_attribute a\njoin pg_class c on c.oid = a.attrelid\njoin pg_namespace n on n.oid = c.relnamespace\njoin pg_type t on t.oid = a.atttypid\njoin pg_namespace tn on tn.oid = t.typnamespace\nleft join pg_type et on et.oid = t.typelem and t.typcategory = 'A'\nleft join pg_namespace etn on etn.oid = et.typnamespace\nleft join pg_attrdef d on d.adrelid = a.attrelid and d.adnum = a.attnum\nwhere n.nspname = any($1) and c.relkind in ('r', 'p', 'v', 'm', 'f') and not c.relispartition\n\tand a.attnum > 0 and not a.attisdropped\norder by a.attrelid, a.attnum";
|
|
2
|
+
export declare const ENUMS = "\nselect n.nspname as schema, t.typname as name,\n\tarray_agg(e.enumlabel order by e.enumsortorder) as \"values\"\nfrom pg_type t\njoin pg_namespace n on n.oid = t.typnamespace\njoin pg_enum e on e.enumtypid = t.oid\nwhere n.nspname = any($1)\n\tand not exists (select 1 from pg_depend x where x.classid = 'pg_type'::regclass and x.objid = t.oid and x.deptype = 'e')\ngroup by n.nspname, t.typname\norder by n.nspname, t.typname";
|
|
3
|
+
export declare const RELATIONS = "\nselect c.oid::int as id, n.nspname as schema, c.relname as name, c.relkind as kind,\n\tobj_description(c.oid, 'pg_class') as comment,\n\tc.relrowsecurity as rls, c.relforcerowsecurity as force_rls,\n\tcase when c.relkind = 'p' then pg_get_partkeydef(c.oid) end as partition_key,\n\tcase when c.relkind in ('v', 'm') then pg_get_viewdef(c.oid, true) end as definition,\n\t(select count(*)::int from pg_inherits i where i.inhparent = c.oid) as partitions,\n\tpg_get_userbyid(c.relowner) as owner,\n\tc.relpersistence as persistence,\n\tarray_to_string(c.reloptions, ', ') as options,\n\tcase when c.reltuples >= 0 then c.reltuples::bigint::text end as row_estimate,\n\tcase when c.relkind in ('r', 'p', 'm') then pg_total_relation_size(c.oid)::text end as size_bytes,\n\t(select string_agg(g.privilege_type || ' to ' || case when g.grantee = 0 then 'public' else pg_get_userbyid(g.grantee) end, ', ' order by g.grantee, g.privilege_type)\n\t\tfrom aclexplode(c.relacl) as g where g.grantee <> c.relowner) as grants,\n\t(select s.srvname from pg_foreign_table ft join pg_foreign_server s on s.oid = ft.ftserver where ft.ftrelid = c.oid) as foreign_server,\n\t(select array_to_string(ft.ftoptions, ', ') from pg_foreign_table ft where ft.ftrelid = c.oid) as foreign_options,\n\t(select string_agg(quote_ident(pn.nspname) || '.' || quote_ident(p.relname), ', ')\n\t\tfrom pg_inherits i join pg_class p on p.oid = i.inhparent join pg_namespace pn on pn.oid = p.relnamespace\n\t\twhere i.inhrelid = c.oid and p.relkind <> 'p') as inherits\nfrom pg_class c\njoin pg_namespace n on n.oid = c.relnamespace\nwhere n.nspname = any($1) and c.relkind in ('r', 'p', 'v', 'm', 'f') and not c.relispartition\n\tand not exists (select 1 from pg_depend e where e.classid = 'pg_class'::regclass and e.objid = c.oid and e.deptype = 'e')\norder by n.nspname, c.relname";
|
|
4
|
+
export declare const COLUMNS = "\nselect a.attrelid::int as table_id, a.attnum::int as position, a.attname as name,\n\tformat_type(a.atttypid, a.atttypmod) as type,\n\ta.attnotnull as not_null, a.attidentity as identity, a.attgenerated as generated,\n\tpg_get_expr(d.adbin, d.adrelid) as \"default\",\n\tcol_description(a.attrelid, a.attnum) as comment,\n\tcase when t.typtype = 'e' then tn.nspname\n\t\twhen et.typtype = 'e' then etn.nspname end as enum_schema,\n\tcoalesce(case when t.typtype = 'e' then t.typname end,\n\t\tcase when et.typtype = 'e' then et.typname || '[]' end) as enum_type,\n\tpg_get_serial_sequence(quote_ident(n.nspname) || '.' || quote_ident(c.relname), a.attname) as serial_sequence,\n\tcase when a.attcollation <> 0 and a.attcollation <> t.typcollation then (select collname from pg_collation where oid = a.attcollation) end as collation,\n\tcase to_jsonb(a) ->> 'attcompression' when 'p' then 'pglz' when 'l' then 'lz4' end as compression,\n\tcase when a.attidentity <> '' then (select s.seqstart::text || ' ' || s.seqincrement::text from pg_sequence s\n\t\twhere s.seqrelid = pg_get_serial_sequence(quote_ident(n.nspname) || '.' || quote_ident(c.relname), a.attname)::regclass) end as identity_sequence,\n\tcase when t.typtype = 'd' then format_type(t.typbasetype, t.typtypmod) end as domain_base\nfrom pg_attribute a\njoin pg_class c on c.oid = a.attrelid\njoin pg_namespace n on n.oid = c.relnamespace\njoin pg_type t on t.oid = a.atttypid\njoin pg_namespace tn on tn.oid = t.typnamespace\nleft join pg_type et on et.oid = t.typelem and t.typcategory = 'A'\nleft join pg_namespace etn on etn.oid = et.typnamespace\nleft join pg_attrdef d on d.adrelid = a.attrelid and d.adnum = a.attnum\nwhere n.nspname = any($1) and c.relkind in ('r', 'p', 'v', 'm', 'f') and not c.relispartition\n\tand a.attnum > 0 and not a.attisdropped\norder by a.attrelid, a.attnum";
|
|
5
5
|
export declare const CONSTRAINTS = "\nselect con.conrelid::int as table_id, con.conname as name, con.contype as kind,\n\t(select array_agg(k::int order by o) from unnest(con.conkey) with ordinality as u(k, o)) as columns,\n\tcon.confrelid::int as foreign_table_id,\n\t(select array_agg(k::int order by o) from unnest(con.confkey) with ordinality as u(k, o)) as foreign_columns,\n\tcon.confdeltype as on_delete, con.confupdtype as on_update,\n\tpg_get_constraintdef(con.oid, true) as definition,\n\tcase when con.contype = 'c' then pg_get_expr(con.conbin, con.conrelid, true) end as check_expression,\n\tobj_description(con.oid, 'pg_constraint') as comment,\n\tcon.condeferrable as deferrable, con.condeferred as deferred,\n\t/* Read through jsonb so a Postgres older than 15, without the column,\n\t * reads null instead of failing. */\n\t(to_jsonb(con) ->> 'connullsnotdistinct')::boolean as nulls_not_distinct\nfrom pg_constraint con\njoin pg_namespace n on n.oid = con.connamespace\nwhere n.nspname = any($1) and con.contype in ('p', 'u', 'f', 'c', 'x') and con.conrelid <> 0\norder by con.conrelid, con.contype, con.conname";
|
|
6
|
-
export declare const INDEXES = "\nselect i.indrelid::int as table_id, ic.relname as name, am.amname as method,\n\ti.indisunique as is_unique, i.indnkeyatts::int as key_count, i.indnatts::int as count,\n\t(select array_agg(pg_get_indexdef(i.indexrelid, k, true) order by k)\n\t\tfrom generate_series(1, i.indnatts) as k) as parts,\n\t(select array_agg(i.indkey[k - 1]::int order by k)\n\t\tfrom generate_series(1, i.indnatts) as k) as attnums,\n\t(select array_agg((i.indoption[k - 1] & 1) = 1 order by k)\n\t\tfrom generate_series(1, i.indnkeyatts) as k) as descending,\n\tpg_get_expr(i.indpred, i.indrelid, true) as predicate,\n\tobj_description(i.indexrelid, 'pg_class') as comment\nfrom pg_index i\njoin pg_class ic on ic.oid = i.indexrelid\njoin pg_class tc on tc.oid = i.indrelid\njoin pg_namespace n on n.oid = tc.relnamespace\njoin pg_am am on am.oid = ic.relam\nwhere n.nspname = any($1)\n\tand not exists (select 1 from pg_constraint con where con.conindid = i.indexrelid and con.contype in ('p', 'u', 'x'))\norder by i.indrelid, ic.relname";
|
|
6
|
+
export declare const INDEXES = "\nselect i.indrelid::int as table_id, ic.relname as name, am.amname as method,\n\ti.indisunique as is_unique, i.indnkeyatts::int as key_count, i.indnatts::int as count,\n\t(select array_agg(pg_get_indexdef(i.indexrelid, k, true) order by k)\n\t\tfrom generate_series(1, i.indnatts) as k) as parts,\n\t(select array_agg(i.indkey[k - 1]::int order by k)\n\t\tfrom generate_series(1, i.indnatts) as k) as attnums,\n\t(select array_agg((i.indoption[k - 1] & 1) = 1 order by k)\n\t\tfrom generate_series(1, i.indnkeyatts) as k) as descending,\n\t(select array_agg((i.indoption[k - 1] & 2) = 2 order by k)\n\t\tfrom generate_series(1, i.indnkeyatts) as k) as nulls_first,\n\t/* An operator class or collation that isn't the column's own, per key\n\t * part, empty where it is. */\n\t(select array_agg(case when opc.opcdefault then '' else opc.opcname end order by k)\n\t\tfrom generate_series(1, i.indnkeyatts) as k join pg_opclass opc on opc.oid = i.indclass[k - 1]) as opclasses,\n\t(select array_agg(coalesce((select co.collname from pg_collation co where co.oid = i.indcollation[k - 1]\n\t\t\tand i.indcollation[k - 1] <> coalesce((select a.attcollation from pg_attribute a where a.attrelid = i.indrelid and a.attnum = i.indkey[k - 1]), 0)\n\t\t\tand co.collname <> 'default'), '') order by k)\n\t\tfrom generate_series(1, i.indnkeyatts) as k) as collations,\n\t(to_jsonb(i) ->> 'indnullsnotdistinct')::boolean as nulls_not_distinct,\n\tpg_get_expr(i.indpred, i.indrelid, true) as predicate,\n\tobj_description(i.indexrelid, 'pg_class') as comment\nfrom pg_index i\njoin pg_class ic on ic.oid = i.indexrelid\njoin pg_class tc on tc.oid = i.indrelid\njoin pg_namespace n on n.oid = tc.relnamespace\njoin pg_am am on am.oid = ic.relam\nwhere n.nspname = any($1)\n\tand not exists (select 1 from pg_constraint con where con.conindid = i.indexrelid and con.contype in ('p', 'u', 'x'))\norder by i.indrelid, ic.relname";
|
|
7
7
|
export declare const VIEW_SOURCES = "\nselect distinct v.oid::int as view_id, d.refobjid::int as source_id\nfrom pg_class v\njoin pg_namespace n on n.oid = v.relnamespace\njoin pg_rewrite r on r.ev_class = v.oid\njoin pg_depend d on d.objid = r.oid and d.classid = 'pg_rewrite'::regclass\n\tand d.refclassid = 'pg_class'::regclass and d.refobjid <> v.oid\nwhere n.nspname = any($1) and v.relkind in ('v', 'm')";
|
|
8
8
|
export declare const POLICIES = "\nselect p.polrelid::int as table_id, p.polname as name, p.polpermissive as permissive,\n\tp.polcmd as command,\n\t(select array_agg(case when r = 0 then 'public' else pg_get_userbyid(r) end) from unnest(p.polroles) as r) as roles,\n\tpg_get_expr(p.polqual, p.polrelid, true) as \"using\",\n\tpg_get_expr(p.polwithcheck, p.polrelid, true) as with_check\nfrom pg_policy p\njoin pg_class c on c.oid = p.polrelid\njoin pg_namespace n on n.oid = c.relnamespace\nwhere n.nspname = any($1)\norder by p.polrelid, p.polname";
|
|
9
9
|
export declare const SEQUENCES = "\nselect n.nspname as schema, c.relname as name, s.seqstart::text as start,\n\ts.seqincrement::text as increment\nfrom pg_sequence s\njoin pg_class c on c.oid = s.seqrelid\njoin pg_namespace n on n.oid = c.relnamespace\nwhere n.nspname = any($1)\n\tand not exists (select 1 from pg_depend d where d.objid = c.oid and d.deptype in ('a', 'i'))\norder by n.nspname, c.relname";
|
|
10
10
|
export declare const SCHEMA_COMMENTS = "\nselect nspname as name, obj_description(oid, 'pg_namespace') as comment\nfrom pg_namespace where nspname = any($1)";
|
|
11
|
+
export declare const TRIGGERS = "\nselect t.tgrelid::int as table_id, t.tgname as name, pg_get_triggerdef(t.oid, true) as definition,\n\tt.tgenabled as enabled\nfrom pg_trigger t\njoin pg_class c on c.oid = t.tgrelid\njoin pg_namespace n on n.oid = c.relnamespace\nwhere n.nspname = any($1) and not t.tgisinternal\norder by t.tgrelid, t.tgname";
|
|
12
|
+
export declare const RULES = "\nselect r.ev_class::int as table_id, r.rulename as name, pg_get_ruledef(r.oid, true) as definition\nfrom pg_rewrite r\njoin pg_class c on c.oid = r.ev_class\njoin pg_namespace n on n.oid = c.relnamespace\nwhere n.nspname = any($1) and r.rulename <> '_RETURN'\norder by r.ev_class, r.rulename";
|
|
13
|
+
export declare const FUNCTIONS = "\nselect n.nspname as schema, p.proname as name, p.prokind as kind,\n\tpg_get_function_identity_arguments(p.oid) as arguments,\n\tpg_get_function_result(p.oid) as result, l.lanname as language\nfrom pg_proc p\njoin pg_namespace n on n.oid = p.pronamespace\njoin pg_language l on l.oid = p.prolang\nwhere n.nspname = any($1)\n\tand not exists (select 1 from pg_depend e where e.classid = 'pg_proc'::regclass and e.objid = p.oid and e.deptype = 'e')\norder by n.nspname, p.proname, arguments";
|
|
14
|
+
export declare const DOMAINS = "\nselect n.nspname as schema, t.typname as name, format_type(t.typbasetype, t.typtypmod) as base,\n\tt.typnotnull as not_null, t.typdefault as \"default\",\n\t(select string_agg(pg_get_constraintdef(con.oid, true), ' and ' order by con.conname)\n\t\t/* Postgres 17 also keeps NOT NULL as a constraint; typnotnull says it. */\n\t\tfrom pg_constraint con where con.contypid = t.oid and con.contype <> 'n') as constraints\nfrom pg_type t\njoin pg_namespace n on n.oid = t.typnamespace\nwhere n.nspname = any($1) and t.typtype = 'd'\n\tand not exists (select 1 from pg_depend e where e.classid = 'pg_type'::regclass and e.objid = t.oid and e.deptype = 'e')\norder by n.nspname, t.typname";
|
|
15
|
+
export declare const COMPOSITES = "\nselect n.nspname as schema, t.typname as name,\n\tstring_agg(a.attname || ' ' || format_type(a.atttypid, a.atttypmod), ', ' order by a.attnum) as fields\nfrom pg_type t\njoin pg_namespace n on n.oid = t.typnamespace\njoin pg_class c on c.oid = t.typrelid and c.relkind = 'c'\njoin pg_attribute a on a.attrelid = c.oid and a.attnum > 0 and not a.attisdropped\nwhere n.nspname = any($1)\n\tand not exists (select 1 from pg_depend e where e.classid = 'pg_type'::regclass and e.objid = t.oid and e.deptype = 'e')\ngroup by n.nspname, t.typname\norder by n.nspname, t.typname";
|
|
16
|
+
export declare const EXTENSIONS = "\nselect e.extname as name, e.extversion as version, n.nspname as schema\nfrom pg_extension e\njoin pg_namespace n on n.oid = e.extnamespace\nwhere e.extname <> 'plpgsql'\norder by e.extname";
|
|
17
|
+
export declare const EVENT_TRIGGERS = "\nselect evtname as name, evtevent as event, evtfoid::regproc::text as function, evtenabled as enabled\nfrom pg_event_trigger\norder by evtname";
|
|
18
|
+
export declare const PARTITIONS = "\nselect i.inhparent::int as table_id, c.relname as name, pg_get_expr(c.relpartbound, c.oid, true) as bound\nfrom pg_inherits i\njoin pg_class c on c.oid = i.inhrelid\njoin pg_class p on p.oid = i.inhparent\njoin pg_namespace n on n.oid = p.relnamespace\nwhere n.nspname = any($1) and p.relkind = 'p'\norder by i.inhparent, c.relname";
|
|
19
|
+
export declare const VIEW_COLUMNS = "\nselect distinct v.oid::int as view_id, d.refobjid::int as source_id, a.attname as name\nfrom pg_class v\njoin pg_namespace n on n.oid = v.relnamespace\njoin pg_rewrite r on r.ev_class = v.oid\njoin pg_depend d on d.objid = r.oid and d.classid = 'pg_rewrite'::regclass\n\tand d.refclassid = 'pg_class'::regclass and d.refobjid <> v.oid and d.refobjsubid > 0\njoin pg_attribute a on a.attrelid = d.refobjid and a.attnum = d.refobjsubid\nwhere n.nspname = any($1) and v.relkind in ('v', 'm')\norder by view_id, source_id, name";
|