prostgles-server 4.2.242 → 4.2.243
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/DboBuilder/getFkeys.d.ts +16 -0
- package/dist/DboBuilder/getFkeys.d.ts.map +1 -0
- package/dist/DboBuilder/getFkeys.js +44 -0
- package/dist/DboBuilder/getFkeys.js.map +1 -0
- package/dist/DboBuilder/getMaterialViews.d.ts +4 -0
- package/dist/DboBuilder/getMaterialViews.d.ts.map +1 -0
- package/dist/DboBuilder/getMaterialViews.js +93 -0
- package/dist/DboBuilder/getMaterialViews.js.map +1 -0
- package/dist/DboBuilder/getTablesForSchemaPostgresSQL.d.ts.map +1 -1
- package/dist/DboBuilder/getTablesForSchemaPostgresSQL.js +9 -127
- package/dist/DboBuilder/getTablesForSchemaPostgresSQL.js.map +1 -1
- package/lib/DboBuilder/getFkeys.ts +55 -0
- package/lib/DboBuilder/getMaterialViews.ts +94 -0
- package/lib/DboBuilder/getTablesForSchemaPostgresSQL.ts +11 -141
- package/package.json +1 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type pgPromise from "pg-promise";
|
|
2
|
+
import type { getSchemaFilter } from "./getTablesForSchemaPostgresSQL";
|
|
3
|
+
export declare const getFkeys: (t: pgPromise.ITask<{}>, { schemaNames, sql }: ReturnType<typeof getSchemaFilter>) => Promise<{
|
|
4
|
+
data: {
|
|
5
|
+
fkeys: {
|
|
6
|
+
oid: number;
|
|
7
|
+
ftable: string;
|
|
8
|
+
cols: string[];
|
|
9
|
+
fcols: string[];
|
|
10
|
+
}[];
|
|
11
|
+
};
|
|
12
|
+
hasError?: false | undefined;
|
|
13
|
+
error?: undefined;
|
|
14
|
+
duration: number;
|
|
15
|
+
}>;
|
|
16
|
+
//# sourceMappingURL=getFkeys.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getFkeys.d.ts","sourceRoot":"","sources":["../../lib/DboBuilder/getFkeys.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,YAAY,CAAC;AAExC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AAEvE,eAAO,MAAM,QAAQ,MAChB,UAAU,KAAK,CAAC,EAAE,CAAC,wBACA,WAAW,sBAAsB,CAAC;;;iBAI/C,MAAM;oBACH,MAAM;kBACR,MAAM,EAAE;mBACP,MAAM,EAAE;;;;;;EAyCpB,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getFkeys = void 0;
|
|
4
|
+
const prostgles_types_1 = require("prostgles-types");
|
|
5
|
+
const getFkeys = async (t, { schemaNames, sql }) => {
|
|
6
|
+
const getFkeys = await (0, prostgles_types_1.tryCatchV2)(async () => {
|
|
7
|
+
const fkeys = await t.any(`
|
|
8
|
+
WITH pg_class_schema AS (
|
|
9
|
+
SELECT c.oid, c.relname, nspname as schema
|
|
10
|
+
,CASE WHEN current_schema() = nspname
|
|
11
|
+
THEN format('%I', c.relname)
|
|
12
|
+
ELSE format('%I.%I', nspname, c.relname)
|
|
13
|
+
END as escaped_identifier
|
|
14
|
+
FROM pg_catalog.pg_class AS c
|
|
15
|
+
LEFT JOIN pg_catalog.pg_namespace AS ns
|
|
16
|
+
ON c.relnamespace = ns.oid
|
|
17
|
+
WHERE nspname ${sql}
|
|
18
|
+
), fk AS (
|
|
19
|
+
SELECT conrelid as oid
|
|
20
|
+
, escaped_identifier as ftable
|
|
21
|
+
, array_agg(c1.attname::text ORDER BY ordinality) as cols
|
|
22
|
+
, array_agg(c2.attname::text ORDER BY ordinality) as fcols
|
|
23
|
+
FROM pg_catalog.pg_constraint c
|
|
24
|
+
INNER JOIN pg_class_schema pc
|
|
25
|
+
ON confrelid = pc.oid
|
|
26
|
+
CROSS JOIN LATERAL unnest(c.conkey, c.confkey) WITH ORDINALITY as key_pairs(attnum, fkattnum, ordinality)
|
|
27
|
+
LEFT JOIN pg_attribute c1
|
|
28
|
+
ON c1.attrelid = c.conrelid and c1.attnum = key_pairs.attnum
|
|
29
|
+
LEFT JOIN pg_attribute c2
|
|
30
|
+
ON c2.attrelid = c.confrelid and c2.attnum = key_pairs.fkattnum
|
|
31
|
+
WHERE contype = 'f'
|
|
32
|
+
GROUP BY conrelid, conname, pc.escaped_identifier
|
|
33
|
+
)
|
|
34
|
+
SELECT * FROM fk
|
|
35
|
+
`, { schemaNames });
|
|
36
|
+
return { fkeys };
|
|
37
|
+
});
|
|
38
|
+
if (getFkeys.hasError) {
|
|
39
|
+
throw getFkeys.error;
|
|
40
|
+
}
|
|
41
|
+
return getFkeys;
|
|
42
|
+
};
|
|
43
|
+
exports.getFkeys = getFkeys;
|
|
44
|
+
//# sourceMappingURL=getFkeys.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getFkeys.js","sourceRoot":"","sources":["../../lib/DboBuilder/getFkeys.ts"],"names":[],"mappings":";;;AACA,qDAA6C;AAGtC,MAAM,QAAQ,GAAG,KAAK,EAC3B,CAAsB,EACtB,EAAE,WAAW,EAAE,GAAG,EAAsC,EACxD,EAAE;IACF,MAAM,QAAQ,GAAG,MAAM,IAAA,4BAAU,EAAC,KAAK,IAAI,EAAE;QAC3C,MAAM,KAAK,GAKL,MAAM,CAAC,CAAC,GAAG,CACf;;;;;;;;;;wBAUkB,GAAG;;;;;;;;;;;;;;;;;;KAkBtB,EACC,EAAE,WAAW,EAAE,CAChB,CAAC;QAEF,OAAO,EAAE,KAAK,EAAE,CAAC;IACnB,CAAC,CAAC,CAAC;IACH,IAAI,QAAQ,CAAC,QAAQ,EAAE,CAAC;QACtB,MAAM,QAAQ,CAAC,KAAK,CAAC;IACvB,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC;AAlDW,QAAA,QAAQ,YAkDnB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getMaterialViews.d.ts","sourceRoot":"","sources":["../../lib/DboBuilder/getMaterialViews.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAGzD,eAAO,MAAM,gBAAgB,OAAQ,MAAM,UAAU,oBAAoB,CAAC,cAAc,CAAC,mBAyFxF,CAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMaterialViews = void 0;
|
|
4
|
+
const getTablesForSchemaPostgresSQL_1 = require("./getTablesForSchemaPostgresSQL");
|
|
5
|
+
const getMaterialViews = (db, schema) => {
|
|
6
|
+
const { sql, schemaNames } = (0, getTablesForSchemaPostgresSQL_1.getSchemaFilter)(schema);
|
|
7
|
+
const query = `
|
|
8
|
+
SELECT
|
|
9
|
+
c.oid,
|
|
10
|
+
schema,
|
|
11
|
+
escaped_identifier,
|
|
12
|
+
true as is_view,
|
|
13
|
+
true as is_mat_view,
|
|
14
|
+
obj_description(c.oid) as comment,
|
|
15
|
+
c.table_name as name,
|
|
16
|
+
definition as view_definition,
|
|
17
|
+
jsonb_build_object(
|
|
18
|
+
'insert', FALSE,
|
|
19
|
+
'select', TRUE,
|
|
20
|
+
'update', FALSE,
|
|
21
|
+
'delete', FALSE
|
|
22
|
+
) as privileges,
|
|
23
|
+
json_agg(json_build_object(
|
|
24
|
+
'name', column_name,
|
|
25
|
+
'table_oid', c.oid,
|
|
26
|
+
'is_pkey', false,
|
|
27
|
+
'data_type', data_type,
|
|
28
|
+
'udt_name', udt_name,
|
|
29
|
+
'element_udt_name',
|
|
30
|
+
CASE WHEN LEFT(udt_name, 1) = '_'
|
|
31
|
+
THEN RIGHT(udt_name, -1) END,
|
|
32
|
+
'element_type',
|
|
33
|
+
CASE WHEN RIGHT(data_type, 2) = '[]'
|
|
34
|
+
THEN LEFT(data_type, -2) END,
|
|
35
|
+
'is_nullable', nullable,
|
|
36
|
+
'is_generated', true,
|
|
37
|
+
'references', null,
|
|
38
|
+
'has_default', false,
|
|
39
|
+
'column_default', null,
|
|
40
|
+
'is_updatable', false,
|
|
41
|
+
'privileges', $$ { "SELECT": true } $$::jsonb
|
|
42
|
+
)) as columns
|
|
43
|
+
FROM pg_catalog.pg_matviews m
|
|
44
|
+
INNER JOIN (
|
|
45
|
+
SELECT
|
|
46
|
+
t.oid,
|
|
47
|
+
CASE WHEN current_schema() = s.nspname
|
|
48
|
+
THEN format('%I', t.relname)
|
|
49
|
+
ELSE format('%I.%I', s.nspname, t.relname)
|
|
50
|
+
END as escaped_identifier,
|
|
51
|
+
t.relname as table_name,
|
|
52
|
+
s.nspname as schema,
|
|
53
|
+
a.attname as column_name,
|
|
54
|
+
pg_catalog.format_type(a.atttypid, a.atttypmod) as data_type,
|
|
55
|
+
typname as udt_name,
|
|
56
|
+
a.attnotnull as nullable,
|
|
57
|
+
a.attnum as ordinal_position,
|
|
58
|
+
col_description(t.oid, attnum) as comment
|
|
59
|
+
FROM pg_catalog.pg_attribute a
|
|
60
|
+
JOIN pg_catalog.pg_class t on a.attrelid = t.oid
|
|
61
|
+
JOIN pg_catalog.pg_namespace s on t.relnamespace = s.oid
|
|
62
|
+
JOIN pg_catalog.pg_type pt ON pt.oid = a.atttypid
|
|
63
|
+
WHERE a.attnum > 0
|
|
64
|
+
AND NOT a.attisdropped
|
|
65
|
+
AND relkind = 'm'
|
|
66
|
+
ORDER BY a.attnum
|
|
67
|
+
) c
|
|
68
|
+
ON matviewname = table_name
|
|
69
|
+
AND schemaname = schema
|
|
70
|
+
WHERE schema ${sql}
|
|
71
|
+
GROUP BY c.oid, escaped_identifier, c.table_name, schema, definition
|
|
72
|
+
`;
|
|
73
|
+
/** TODO: check privileges
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
select
|
|
78
|
+
coalesce(nullif(s[1], ''), 'public') as grantee,
|
|
79
|
+
s[2] as privileges
|
|
80
|
+
from
|
|
81
|
+
pg_class c
|
|
82
|
+
join pg_namespace n on n.oid = relnamespace
|
|
83
|
+
join pg_roles r on r.oid = relowner,
|
|
84
|
+
unnest(coalesce(relacl::text[], format('{%s=arwdDxt/%s}', rolname, rolname)::text[])) acl,
|
|
85
|
+
regexp_split_to_array(acl, '=|/') s
|
|
86
|
+
where nspname = 'public' and relname = 'test_view';
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
*/
|
|
90
|
+
return db.any(query, { schemaNames });
|
|
91
|
+
};
|
|
92
|
+
exports.getMaterialViews = getMaterialViews;
|
|
93
|
+
//# sourceMappingURL=getMaterialViews.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getMaterialViews.js","sourceRoot":"","sources":["../../lib/DboBuilder/getMaterialViews.ts"],"names":[],"mappings":";;;AAEA,mFAAkE;AAE3D,MAAM,gBAAgB,GAAG,CAAC,EAAU,EAAE,MAA4C,EAAE,EAAE;IAC3F,MAAM,EAAE,GAAG,EAAE,WAAW,EAAE,GAAG,IAAA,+CAAe,EAAC,MAAM,CAAC,CAAC;IAErD,MAAM,KAAK,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBA+DG,GAAG;;GAEnB,CAAC;IAEF;;;;;;;;;;;;;;;;MAgBE;IAEF,OAAO,EAAE,CAAC,GAAG,CAAC,KAAK,EAAE,EAAE,WAAW,EAAE,CAAC,CAAC;AACxC,CAAC,CAAC;AAzFW,QAAA,gBAAgB,oBAyF3B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getTablesForSchemaPostgresSQL.d.ts","sourceRoot":"","sources":["../../lib/DboBuilder/getTablesForSchemaPostgresSQL.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAGtD,OAAO,EAAE,WAAW,EAAqB,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"getTablesForSchemaPostgresSQL.d.ts","sourceRoot":"","sources":["../../lib/DboBuilder/getTablesForSchemaPostgresSQL.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAGtD,OAAO,EAAE,WAAW,EAAqB,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAIzD,eAAO,MAAM,eAAe,YAAY,oBAAoB,CAAC,cAAc,CAAC;;;CAW3E,CAAC;AAIF,wBAAsB,6BAA6B,CACjD,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,UAAU,EAC1B,MAAM,EAAE,oBAAoB,CAAC,cAAc,CAAC,GAC3C,OAAO,CAAC;IACT,MAAM,EAAE,WAAW,EAAE,CAAC;IACtB,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACnC,CAAC,CA8TD"}
|
|
@@ -4,93 +4,8 @@ exports.getTablesForSchemaPostgresSQL = exports.getSchemaFilter = void 0;
|
|
|
4
4
|
const prostgles_types_1 = require("prostgles-types");
|
|
5
5
|
const util_1 = require("prostgles-types/dist/util");
|
|
6
6
|
const utils_1 = require("../utils");
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
const query = `
|
|
10
|
-
SELECT
|
|
11
|
-
c.oid,
|
|
12
|
-
schema,
|
|
13
|
-
escaped_identifier,
|
|
14
|
-
true as is_view,
|
|
15
|
-
true as is_mat_view,
|
|
16
|
-
obj_description(c.oid) as comment,
|
|
17
|
-
c.table_name as name,
|
|
18
|
-
definition as view_definition,
|
|
19
|
-
jsonb_build_object(
|
|
20
|
-
'insert', FALSE,
|
|
21
|
-
'select', TRUE,
|
|
22
|
-
'update', FALSE,
|
|
23
|
-
'delete', FALSE
|
|
24
|
-
) as privileges,
|
|
25
|
-
json_agg(json_build_object(
|
|
26
|
-
'name', column_name,
|
|
27
|
-
'table_oid', c.oid,
|
|
28
|
-
'is_pkey', false,
|
|
29
|
-
'data_type', data_type,
|
|
30
|
-
'udt_name', udt_name,
|
|
31
|
-
'element_udt_name',
|
|
32
|
-
CASE WHEN LEFT(udt_name, 1) = '_'
|
|
33
|
-
THEN RIGHT(udt_name, -1) END,
|
|
34
|
-
'element_type',
|
|
35
|
-
CASE WHEN RIGHT(data_type, 2) = '[]'
|
|
36
|
-
THEN LEFT(data_type, -2) END,
|
|
37
|
-
'is_nullable', nullable,
|
|
38
|
-
'is_generated', true,
|
|
39
|
-
'references', null,
|
|
40
|
-
'has_default', false,
|
|
41
|
-
'column_default', null,
|
|
42
|
-
'is_updatable', false,
|
|
43
|
-
'privileges', $$ { "SELECT": true } $$::jsonb
|
|
44
|
-
)) as columns
|
|
45
|
-
FROM pg_catalog.pg_matviews m
|
|
46
|
-
INNER JOIN (
|
|
47
|
-
SELECT
|
|
48
|
-
t.oid,
|
|
49
|
-
CASE WHEN current_schema() = s.nspname
|
|
50
|
-
THEN format('%I', t.relname)
|
|
51
|
-
ELSE format('%I.%I', s.nspname, t.relname)
|
|
52
|
-
END as escaped_identifier,
|
|
53
|
-
t.relname as table_name,
|
|
54
|
-
s.nspname as schema,
|
|
55
|
-
a.attname as column_name,
|
|
56
|
-
pg_catalog.format_type(a.atttypid, a.atttypmod) as data_type,
|
|
57
|
-
typname as udt_name,
|
|
58
|
-
a.attnotnull as nullable,
|
|
59
|
-
a.attnum as ordinal_position,
|
|
60
|
-
col_description(t.oid, attnum) as comment
|
|
61
|
-
FROM pg_catalog.pg_attribute a
|
|
62
|
-
JOIN pg_catalog.pg_class t on a.attrelid = t.oid
|
|
63
|
-
JOIN pg_catalog.pg_namespace s on t.relnamespace = s.oid
|
|
64
|
-
JOIN pg_catalog.pg_type pt ON pt.oid = a.atttypid
|
|
65
|
-
WHERE a.attnum > 0
|
|
66
|
-
AND NOT a.attisdropped
|
|
67
|
-
AND relkind = 'm'
|
|
68
|
-
ORDER BY a.attnum
|
|
69
|
-
) c
|
|
70
|
-
ON matviewname = table_name
|
|
71
|
-
AND schemaname = schema
|
|
72
|
-
WHERE schema ${sql}
|
|
73
|
-
GROUP BY c.oid, escaped_identifier, c.table_name, schema, definition
|
|
74
|
-
`;
|
|
75
|
-
/** TODO: check privileges
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
select
|
|
80
|
-
coalesce(nullif(s[1], ''), 'public') as grantee,
|
|
81
|
-
s[2] as privileges
|
|
82
|
-
from
|
|
83
|
-
pg_class c
|
|
84
|
-
join pg_namespace n on n.oid = relnamespace
|
|
85
|
-
join pg_roles r on r.oid = relowner,
|
|
86
|
-
unnest(coalesce(relacl::text[], format('{%s=arwdDxt/%s}', rolname, rolname)::text[])) acl,
|
|
87
|
-
regexp_split_to_array(acl, '=|/') s
|
|
88
|
-
where nspname = 'public' and relname = 'test_view';
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
*/
|
|
92
|
-
return db.any(query, { schemaNames });
|
|
93
|
-
};
|
|
7
|
+
const getFkeys_1 = require("./getFkeys");
|
|
8
|
+
const getMaterialViews_1 = require("./getMaterialViews");
|
|
94
9
|
const getSchemaFilter = (schema = { public: 1 }) => {
|
|
95
10
|
const schemaNames = Object.keys(schema);
|
|
96
11
|
const isInclusive = Object.values(schema).every((v) => v);
|
|
@@ -104,47 +19,14 @@ const getSchemaFilter = (schema = { public: 1 }) => {
|
|
|
104
19
|
};
|
|
105
20
|
exports.getSchemaFilter = getSchemaFilter;
|
|
106
21
|
// TODO: Add a onSocketConnect timeout for this query.
|
|
107
|
-
//
|
|
22
|
+
// Reason: this query gets blocked by prostgles.app_triggers from PubSubManager.addTrigger in some cases (pg_dump locks that table)
|
|
108
23
|
async function getTablesForSchemaPostgresSQL({ db, runSQL }, schema) {
|
|
109
24
|
const { sql, schemaNames } = (0, exports.getSchemaFilter)(schema);
|
|
110
25
|
return db.tx(async (t) => {
|
|
111
26
|
/**
|
|
112
27
|
* Multiple queries to reduce load on low power machines
|
|
113
28
|
*/
|
|
114
|
-
const
|
|
115
|
-
const fkeys = await t.any(`
|
|
116
|
-
WITH pg_class_schema AS (
|
|
117
|
-
SELECT c.oid, c.relname, nspname as schema
|
|
118
|
-
,CASE WHEN current_schema() = nspname
|
|
119
|
-
THEN format('%I', c.relname)
|
|
120
|
-
ELSE format('%I.%I', nspname, c.relname)
|
|
121
|
-
END as escaped_identifier
|
|
122
|
-
FROM pg_catalog.pg_class AS c
|
|
123
|
-
LEFT JOIN pg_catalog.pg_namespace AS ns
|
|
124
|
-
ON c.relnamespace = ns.oid
|
|
125
|
-
WHERE nspname ${sql}
|
|
126
|
-
), fk AS (
|
|
127
|
-
SELECT conrelid as oid
|
|
128
|
-
, escaped_identifier as ftable
|
|
129
|
-
, array_agg(DISTINCT c1.attname::text) as cols
|
|
130
|
-
, array_agg(DISTINCT c2.attname::text) as fcols
|
|
131
|
-
FROM pg_catalog.pg_constraint c
|
|
132
|
-
INNER JOIN pg_class_schema pc
|
|
133
|
-
ON confrelid = pc.oid
|
|
134
|
-
LEFT JOIN pg_attribute c1
|
|
135
|
-
ON c1.attrelid = c.conrelid and ARRAY[c1.attnum] <@ c.conkey
|
|
136
|
-
LEFT JOIN pg_attribute c2
|
|
137
|
-
ON c2.attrelid = c.confrelid and ARRAY[c2.attnum] <@ c.confkey
|
|
138
|
-
WHERE contype = 'f'
|
|
139
|
-
GROUP BY conrelid, conname, pc.escaped_identifier
|
|
140
|
-
)
|
|
141
|
-
SELECT * FROM fk
|
|
142
|
-
`, { schemaNames });
|
|
143
|
-
return { fkeys };
|
|
144
|
-
});
|
|
145
|
-
if (getFkeys.error !== undefined) {
|
|
146
|
-
throw getFkeys.error;
|
|
147
|
-
}
|
|
29
|
+
const { data: { fkeys }, duration: fkeysResponseDuration, } = await (0, getFkeys_1.getFkeys)(t, { sql, schemaNames });
|
|
148
30
|
const uniqueColsReq = await (0, util_1.tryCatchV2)(async () => {
|
|
149
31
|
const res = (await t.any(`
|
|
150
32
|
select
|
|
@@ -174,9 +56,9 @@ async function getTablesForSchemaPostgresSQL({ db, runSQL }, schema) {
|
|
|
174
56
|
if (uniqueColsReq.error) {
|
|
175
57
|
throw uniqueColsReq.error;
|
|
176
58
|
}
|
|
177
|
-
const badFkey =
|
|
59
|
+
const badFkey = fkeys.find((r) => r.fcols.includes(null));
|
|
178
60
|
if (badFkey) {
|
|
179
|
-
throw `Invalid table column schema. Null or empty fcols for ${JSON.stringify(
|
|
61
|
+
throw `Invalid table column schema. Null or empty fcols for ${JSON.stringify(fkeys)}`;
|
|
180
62
|
}
|
|
181
63
|
const getTVColumns = await (0, util_1.tryCatch)(async () => {
|
|
182
64
|
const columns = await t.any(`
|
|
@@ -308,7 +190,7 @@ async function getTablesForSchemaPostgresSQL({ db, runSQL }, schema) {
|
|
|
308
190
|
throw getTablesAndViews.error ?? "No tablesAndViews";
|
|
309
191
|
}
|
|
310
192
|
const getMaterialViewsReq = await (0, util_1.tryCatch)(async () => {
|
|
311
|
-
const materialViews = await getMaterialViews(t, schema);
|
|
193
|
+
const materialViews = await (0, getMaterialViews_1.getMaterialViews)(t, schema);
|
|
312
194
|
return { materialViews };
|
|
313
195
|
});
|
|
314
196
|
if (getMaterialViewsReq.error || !getMaterialViewsReq.materialViews) {
|
|
@@ -333,7 +215,7 @@ async function getTablesForSchemaPostgresSQL({ db, runSQL }, schema) {
|
|
|
333
215
|
table.privileges.update =
|
|
334
216
|
allowAllIfNoColumns ?? table.columns.some((c) => c.privileges.UPDATE);
|
|
335
217
|
table.columns = table.columns.map((c) => {
|
|
336
|
-
const refs =
|
|
218
|
+
const refs = fkeys.filter((fc) => fc.oid === table.oid && fc.cols.includes(c.name));
|
|
337
219
|
if (refs.length)
|
|
338
220
|
c.references = refs.map((_ref) => {
|
|
339
221
|
const ref = { ..._ref };
|
|
@@ -407,7 +289,7 @@ async function getTablesForSchemaPostgresSQL({ db, runSQL }, schema) {
|
|
|
407
289
|
matv: getMaterialViewsReq.duration,
|
|
408
290
|
columns: getTVColumns.duration,
|
|
409
291
|
tablesAndViews: getTablesAndViews.duration,
|
|
410
|
-
fkeys:
|
|
292
|
+
fkeys: fkeysResponseDuration,
|
|
411
293
|
getHyperTbls: getHyperTablesReq.duration,
|
|
412
294
|
viewParentTbls: getViewParentTables.duration,
|
|
413
295
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getTablesForSchemaPostgresSQL.js","sourceRoot":"","sources":["../../lib/DboBuilder/getTablesForSchemaPostgresSQL.ts"],"names":[],"mappings":";;;AAAA,qDAAoD;AACpD,oDAA2E;AAG3E,oCAAiC;
|
|
1
|
+
{"version":3,"file":"getTablesForSchemaPostgresSQL.js","sourceRoot":"","sources":["../../lib/DboBuilder/getTablesForSchemaPostgresSQL.ts"],"names":[],"mappings":";;;AAAA,qDAAoD;AACpD,oDAA2E;AAG3E,oCAAiC;AAGjC,yCAAsC;AACtC,yDAAsD;AAE/C,MAAM,eAAe,GAAG,CAAC,SAA+C,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,EAAE;IAC9F,MAAM,WAAW,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACxC,MAAM,WAAW,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;IAC1D,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,CAAC;QACxB,MAAM,kCAAkC,CAAC;IAC3C,CAAC;IAED,OAAO;QACL,GAAG,EAAE,IAAI,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,0BAA0B;QAC5D,WAAW;KACZ,CAAC;AACJ,CAAC,CAAC;AAXW,QAAA,eAAe,mBAW1B;AAEF,sDAAsD;AACtD,oIAAoI;AAC7H,KAAK,UAAU,6BAA6B,CACjD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AAG,IAAA,wBAAM,EAAC,MAAM,CAAC,GAAG,cAAc,CAC5D,CAAC;QACF,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC;IACzC,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type pgPromise from "pg-promise";
|
|
2
|
+
import { tryCatchV2 } from "prostgles-types";
|
|
3
|
+
import type { getSchemaFilter } from "./getTablesForSchemaPostgresSQL";
|
|
4
|
+
|
|
5
|
+
export const getFkeys = async (
|
|
6
|
+
t: pgPromise.ITask<{}>,
|
|
7
|
+
{ schemaNames, sql }: ReturnType<typeof getSchemaFilter>
|
|
8
|
+
) => {
|
|
9
|
+
const getFkeys = await tryCatchV2(async () => {
|
|
10
|
+
const fkeys: {
|
|
11
|
+
oid: number;
|
|
12
|
+
ftable: string;
|
|
13
|
+
cols: string[];
|
|
14
|
+
fcols: string[];
|
|
15
|
+
}[] = await t.any(
|
|
16
|
+
`
|
|
17
|
+
WITH pg_class_schema AS (
|
|
18
|
+
SELECT c.oid, c.relname, nspname as schema
|
|
19
|
+
,CASE WHEN current_schema() = nspname
|
|
20
|
+
THEN format('%I', c.relname)
|
|
21
|
+
ELSE format('%I.%I', nspname, c.relname)
|
|
22
|
+
END as escaped_identifier
|
|
23
|
+
FROM pg_catalog.pg_class AS c
|
|
24
|
+
LEFT JOIN pg_catalog.pg_namespace AS ns
|
|
25
|
+
ON c.relnamespace = ns.oid
|
|
26
|
+
WHERE nspname ${sql}
|
|
27
|
+
), fk AS (
|
|
28
|
+
SELECT conrelid as oid
|
|
29
|
+
, escaped_identifier as ftable
|
|
30
|
+
, array_agg(c1.attname::text ORDER BY ordinality) as cols
|
|
31
|
+
, array_agg(c2.attname::text ORDER BY ordinality) as fcols
|
|
32
|
+
FROM pg_catalog.pg_constraint c
|
|
33
|
+
INNER JOIN pg_class_schema pc
|
|
34
|
+
ON confrelid = pc.oid
|
|
35
|
+
CROSS JOIN LATERAL unnest(c.conkey, c.confkey) WITH ORDINALITY as key_pairs(attnum, fkattnum, ordinality)
|
|
36
|
+
LEFT JOIN pg_attribute c1
|
|
37
|
+
ON c1.attrelid = c.conrelid and c1.attnum = key_pairs.attnum
|
|
38
|
+
LEFT JOIN pg_attribute c2
|
|
39
|
+
ON c2.attrelid = c.confrelid and c2.attnum = key_pairs.fkattnum
|
|
40
|
+
WHERE contype = 'f'
|
|
41
|
+
GROUP BY conrelid, conname, pc.escaped_identifier
|
|
42
|
+
)
|
|
43
|
+
SELECT * FROM fk
|
|
44
|
+
`,
|
|
45
|
+
{ schemaNames }
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
return { fkeys };
|
|
49
|
+
});
|
|
50
|
+
if (getFkeys.hasError) {
|
|
51
|
+
throw getFkeys.error;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return getFkeys;
|
|
55
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { DBorTx } from "../Prostgles";
|
|
2
|
+
import { ProstglesInitOptions } from "../ProstglesTypes";
|
|
3
|
+
import { getSchemaFilter } from "./getTablesForSchemaPostgresSQL";
|
|
4
|
+
|
|
5
|
+
export const getMaterialViews = (db: DBorTx, schema: ProstglesInitOptions["schemaFilter"]) => {
|
|
6
|
+
const { sql, schemaNames } = getSchemaFilter(schema);
|
|
7
|
+
|
|
8
|
+
const query = `
|
|
9
|
+
SELECT
|
|
10
|
+
c.oid,
|
|
11
|
+
schema,
|
|
12
|
+
escaped_identifier,
|
|
13
|
+
true as is_view,
|
|
14
|
+
true as is_mat_view,
|
|
15
|
+
obj_description(c.oid) as comment,
|
|
16
|
+
c.table_name as name,
|
|
17
|
+
definition as view_definition,
|
|
18
|
+
jsonb_build_object(
|
|
19
|
+
'insert', FALSE,
|
|
20
|
+
'select', TRUE,
|
|
21
|
+
'update', FALSE,
|
|
22
|
+
'delete', FALSE
|
|
23
|
+
) as privileges,
|
|
24
|
+
json_agg(json_build_object(
|
|
25
|
+
'name', column_name,
|
|
26
|
+
'table_oid', c.oid,
|
|
27
|
+
'is_pkey', false,
|
|
28
|
+
'data_type', data_type,
|
|
29
|
+
'udt_name', udt_name,
|
|
30
|
+
'element_udt_name',
|
|
31
|
+
CASE WHEN LEFT(udt_name, 1) = '_'
|
|
32
|
+
THEN RIGHT(udt_name, -1) END,
|
|
33
|
+
'element_type',
|
|
34
|
+
CASE WHEN RIGHT(data_type, 2) = '[]'
|
|
35
|
+
THEN LEFT(data_type, -2) END,
|
|
36
|
+
'is_nullable', nullable,
|
|
37
|
+
'is_generated', true,
|
|
38
|
+
'references', null,
|
|
39
|
+
'has_default', false,
|
|
40
|
+
'column_default', null,
|
|
41
|
+
'is_updatable', false,
|
|
42
|
+
'privileges', $$ { "SELECT": true } $$::jsonb
|
|
43
|
+
)) as columns
|
|
44
|
+
FROM pg_catalog.pg_matviews m
|
|
45
|
+
INNER JOIN (
|
|
46
|
+
SELECT
|
|
47
|
+
t.oid,
|
|
48
|
+
CASE WHEN current_schema() = s.nspname
|
|
49
|
+
THEN format('%I', t.relname)
|
|
50
|
+
ELSE format('%I.%I', s.nspname, t.relname)
|
|
51
|
+
END as escaped_identifier,
|
|
52
|
+
t.relname as table_name,
|
|
53
|
+
s.nspname as schema,
|
|
54
|
+
a.attname as column_name,
|
|
55
|
+
pg_catalog.format_type(a.atttypid, a.atttypmod) as data_type,
|
|
56
|
+
typname as udt_name,
|
|
57
|
+
a.attnotnull as nullable,
|
|
58
|
+
a.attnum as ordinal_position,
|
|
59
|
+
col_description(t.oid, attnum) as comment
|
|
60
|
+
FROM pg_catalog.pg_attribute a
|
|
61
|
+
JOIN pg_catalog.pg_class t on a.attrelid = t.oid
|
|
62
|
+
JOIN pg_catalog.pg_namespace s on t.relnamespace = s.oid
|
|
63
|
+
JOIN pg_catalog.pg_type pt ON pt.oid = a.atttypid
|
|
64
|
+
WHERE a.attnum > 0
|
|
65
|
+
AND NOT a.attisdropped
|
|
66
|
+
AND relkind = 'm'
|
|
67
|
+
ORDER BY a.attnum
|
|
68
|
+
) c
|
|
69
|
+
ON matviewname = table_name
|
|
70
|
+
AND schemaname = schema
|
|
71
|
+
WHERE schema ${sql}
|
|
72
|
+
GROUP BY c.oid, escaped_identifier, c.table_name, schema, definition
|
|
73
|
+
`;
|
|
74
|
+
|
|
75
|
+
/** TODO: check privileges
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
select
|
|
80
|
+
coalesce(nullif(s[1], ''), 'public') as grantee,
|
|
81
|
+
s[2] as privileges
|
|
82
|
+
from
|
|
83
|
+
pg_class c
|
|
84
|
+
join pg_namespace n on n.oid = relnamespace
|
|
85
|
+
join pg_roles r on r.oid = relowner,
|
|
86
|
+
unnest(coalesce(relacl::text[], format('{%s=arwdDxt/%s}', rolname, rolname)::text[])) acl,
|
|
87
|
+
regexp_split_to_array(acl, '=|/') s
|
|
88
|
+
where nspname = 'public' and relname = 'test_view';
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
return db.any(query, { schemaNames });
|
|
94
|
+
};
|
|
@@ -5,97 +5,8 @@ import { DBorTx } from "../Prostgles";
|
|
|
5
5
|
import { clone } from "../utils";
|
|
6
6
|
import { TableSchema, TableSchemaColumn } from "./DboBuilderTypes";
|
|
7
7
|
import { ProstglesInitOptions } from "../ProstglesTypes";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const { sql, schemaNames } = getSchemaFilter(schema);
|
|
11
|
-
|
|
12
|
-
const query = `
|
|
13
|
-
SELECT
|
|
14
|
-
c.oid,
|
|
15
|
-
schema,
|
|
16
|
-
escaped_identifier,
|
|
17
|
-
true as is_view,
|
|
18
|
-
true as is_mat_view,
|
|
19
|
-
obj_description(c.oid) as comment,
|
|
20
|
-
c.table_name as name,
|
|
21
|
-
definition as view_definition,
|
|
22
|
-
jsonb_build_object(
|
|
23
|
-
'insert', FALSE,
|
|
24
|
-
'select', TRUE,
|
|
25
|
-
'update', FALSE,
|
|
26
|
-
'delete', FALSE
|
|
27
|
-
) as privileges,
|
|
28
|
-
json_agg(json_build_object(
|
|
29
|
-
'name', column_name,
|
|
30
|
-
'table_oid', c.oid,
|
|
31
|
-
'is_pkey', false,
|
|
32
|
-
'data_type', data_type,
|
|
33
|
-
'udt_name', udt_name,
|
|
34
|
-
'element_udt_name',
|
|
35
|
-
CASE WHEN LEFT(udt_name, 1) = '_'
|
|
36
|
-
THEN RIGHT(udt_name, -1) END,
|
|
37
|
-
'element_type',
|
|
38
|
-
CASE WHEN RIGHT(data_type, 2) = '[]'
|
|
39
|
-
THEN LEFT(data_type, -2) END,
|
|
40
|
-
'is_nullable', nullable,
|
|
41
|
-
'is_generated', true,
|
|
42
|
-
'references', null,
|
|
43
|
-
'has_default', false,
|
|
44
|
-
'column_default', null,
|
|
45
|
-
'is_updatable', false,
|
|
46
|
-
'privileges', $$ { "SELECT": true } $$::jsonb
|
|
47
|
-
)) as columns
|
|
48
|
-
FROM pg_catalog.pg_matviews m
|
|
49
|
-
INNER JOIN (
|
|
50
|
-
SELECT
|
|
51
|
-
t.oid,
|
|
52
|
-
CASE WHEN current_schema() = s.nspname
|
|
53
|
-
THEN format('%I', t.relname)
|
|
54
|
-
ELSE format('%I.%I', s.nspname, t.relname)
|
|
55
|
-
END as escaped_identifier,
|
|
56
|
-
t.relname as table_name,
|
|
57
|
-
s.nspname as schema,
|
|
58
|
-
a.attname as column_name,
|
|
59
|
-
pg_catalog.format_type(a.atttypid, a.atttypmod) as data_type,
|
|
60
|
-
typname as udt_name,
|
|
61
|
-
a.attnotnull as nullable,
|
|
62
|
-
a.attnum as ordinal_position,
|
|
63
|
-
col_description(t.oid, attnum) as comment
|
|
64
|
-
FROM pg_catalog.pg_attribute a
|
|
65
|
-
JOIN pg_catalog.pg_class t on a.attrelid = t.oid
|
|
66
|
-
JOIN pg_catalog.pg_namespace s on t.relnamespace = s.oid
|
|
67
|
-
JOIN pg_catalog.pg_type pt ON pt.oid = a.atttypid
|
|
68
|
-
WHERE a.attnum > 0
|
|
69
|
-
AND NOT a.attisdropped
|
|
70
|
-
AND relkind = 'm'
|
|
71
|
-
ORDER BY a.attnum
|
|
72
|
-
) c
|
|
73
|
-
ON matviewname = table_name
|
|
74
|
-
AND schemaname = schema
|
|
75
|
-
WHERE schema ${sql}
|
|
76
|
-
GROUP BY c.oid, escaped_identifier, c.table_name, schema, definition
|
|
77
|
-
`;
|
|
78
|
-
|
|
79
|
-
/** TODO: check privileges
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
select
|
|
84
|
-
coalesce(nullif(s[1], ''), 'public') as grantee,
|
|
85
|
-
s[2] as privileges
|
|
86
|
-
from
|
|
87
|
-
pg_class c
|
|
88
|
-
join pg_namespace n on n.oid = relnamespace
|
|
89
|
-
join pg_roles r on r.oid = relowner,
|
|
90
|
-
unnest(coalesce(relacl::text[], format('{%s=arwdDxt/%s}', rolname, rolname)::text[])) acl,
|
|
91
|
-
regexp_split_to_array(acl, '=|/') s
|
|
92
|
-
where nspname = 'public' and relname = 'test_view';
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
*/
|
|
96
|
-
|
|
97
|
-
return db.any(query, { schemaNames });
|
|
98
|
-
};
|
|
8
|
+
import { getFkeys } from "./getFkeys";
|
|
9
|
+
import { getMaterialViews } from "./getMaterialViews";
|
|
99
10
|
|
|
100
11
|
export const getSchemaFilter = (schema: ProstglesInitOptions["schemaFilter"] = { public: 1 }) => {
|
|
101
12
|
const schemaNames = Object.keys(schema);
|
|
@@ -111,7 +22,7 @@ export const getSchemaFilter = (schema: ProstglesInitOptions["schemaFilter"] = {
|
|
|
111
22
|
};
|
|
112
23
|
|
|
113
24
|
// TODO: Add a onSocketConnect timeout for this query.
|
|
114
|
-
//
|
|
25
|
+
// Reason: this query gets blocked by prostgles.app_triggers from PubSubManager.addTrigger in some cases (pg_dump locks that table)
|
|
115
26
|
export async function getTablesForSchemaPostgresSQL(
|
|
116
27
|
{ db, runSQL }: DboBuilder,
|
|
117
28
|
schema: ProstglesInitOptions["schemaFilter"]
|
|
@@ -125,49 +36,10 @@ export async function getTablesForSchemaPostgresSQL(
|
|
|
125
36
|
/**
|
|
126
37
|
* Multiple queries to reduce load on low power machines
|
|
127
38
|
*/
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
cols: string[];
|
|
133
|
-
fcols: string[];
|
|
134
|
-
}[] = await t.any(
|
|
135
|
-
`
|
|
136
|
-
WITH pg_class_schema AS (
|
|
137
|
-
SELECT c.oid, c.relname, nspname as schema
|
|
138
|
-
,CASE WHEN current_schema() = nspname
|
|
139
|
-
THEN format('%I', c.relname)
|
|
140
|
-
ELSE format('%I.%I', nspname, c.relname)
|
|
141
|
-
END as escaped_identifier
|
|
142
|
-
FROM pg_catalog.pg_class AS c
|
|
143
|
-
LEFT JOIN pg_catalog.pg_namespace AS ns
|
|
144
|
-
ON c.relnamespace = ns.oid
|
|
145
|
-
WHERE nspname ${sql}
|
|
146
|
-
), fk AS (
|
|
147
|
-
SELECT conrelid as oid
|
|
148
|
-
, escaped_identifier as ftable
|
|
149
|
-
, array_agg(DISTINCT c1.attname::text) as cols
|
|
150
|
-
, array_agg(DISTINCT c2.attname::text) as fcols
|
|
151
|
-
FROM pg_catalog.pg_constraint c
|
|
152
|
-
INNER JOIN pg_class_schema pc
|
|
153
|
-
ON confrelid = pc.oid
|
|
154
|
-
LEFT JOIN pg_attribute c1
|
|
155
|
-
ON c1.attrelid = c.conrelid and ARRAY[c1.attnum] <@ c.conkey
|
|
156
|
-
LEFT JOIN pg_attribute c2
|
|
157
|
-
ON c2.attrelid = c.confrelid and ARRAY[c2.attnum] <@ c.confkey
|
|
158
|
-
WHERE contype = 'f'
|
|
159
|
-
GROUP BY conrelid, conname, pc.escaped_identifier
|
|
160
|
-
)
|
|
161
|
-
SELECT * FROM fk
|
|
162
|
-
`,
|
|
163
|
-
{ schemaNames }
|
|
164
|
-
);
|
|
165
|
-
|
|
166
|
-
return { fkeys };
|
|
167
|
-
});
|
|
168
|
-
if (getFkeys.error !== undefined) {
|
|
169
|
-
throw getFkeys.error;
|
|
170
|
-
}
|
|
39
|
+
const {
|
|
40
|
+
data: { fkeys },
|
|
41
|
+
duration: fkeysResponseDuration,
|
|
42
|
+
} = await getFkeys(t, { sql, schemaNames });
|
|
171
43
|
|
|
172
44
|
const uniqueColsReq = await tryCatchV2(async () => {
|
|
173
45
|
const res = (await t.any(`
|
|
@@ -206,9 +78,9 @@ export async function getTablesForSchemaPostgresSQL(
|
|
|
206
78
|
throw uniqueColsReq.error;
|
|
207
79
|
}
|
|
208
80
|
|
|
209
|
-
const badFkey =
|
|
81
|
+
const badFkey = fkeys.find((r) => r.fcols.includes(null as any));
|
|
210
82
|
if (badFkey) {
|
|
211
|
-
throw `Invalid table column schema. Null or empty fcols for ${JSON.stringify(
|
|
83
|
+
throw `Invalid table column schema. Null or empty fcols for ${JSON.stringify(fkeys)}`;
|
|
212
84
|
}
|
|
213
85
|
|
|
214
86
|
const getTVColumns = await tryCatch(async () => {
|
|
@@ -377,9 +249,7 @@ export async function getTablesForSchemaPostgresSQL(
|
|
|
377
249
|
table.privileges.update =
|
|
378
250
|
allowAllIfNoColumns ?? table.columns.some((c) => c.privileges.UPDATE);
|
|
379
251
|
table.columns = table.columns.map((c) => {
|
|
380
|
-
const refs =
|
|
381
|
-
(fc) => fc.oid === table.oid && fc.cols.includes(c.name)
|
|
382
|
-
);
|
|
252
|
+
const refs = fkeys.filter((fc) => fc.oid === table.oid && fc.cols.includes(c.name));
|
|
383
253
|
if (refs.length)
|
|
384
254
|
c.references = refs.map((_ref) => {
|
|
385
255
|
const ref = { ..._ref };
|
|
@@ -469,7 +339,7 @@ export async function getTablesForSchemaPostgresSQL(
|
|
|
469
339
|
matv: getMaterialViewsReq.duration,
|
|
470
340
|
columns: getTVColumns.duration,
|
|
471
341
|
tablesAndViews: getTablesAndViews.duration,
|
|
472
|
-
fkeys:
|
|
342
|
+
fkeys: fkeysResponseDuration,
|
|
473
343
|
getHyperTbls: getHyperTablesReq.duration,
|
|
474
344
|
viewParentTbls: getViewParentTables.duration,
|
|
475
345
|
},
|