Skip to content
This repository was archived by the owner on Mar 4, 2019. It is now read-only.

Commit da4119c

Browse files
committed
feat: option to exclude materialized views (#392)
1 parent 23525e3 commit da4119c

16 files changed

Lines changed: 255 additions & 241 deletions

File tree

docs/connecting.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,15 @@ massive(connectionInfo, {
8686
// never load functions on the blacklist
8787
functionBlacklist: 'authorizeUser,disableUser',
8888

89+
// streamline function return values: a function with a scalar
90+
// value will return just the scalar instead of an array, etc.
91+
enhancedFunctions: true,
92+
8993
// don't load database functions at all
9094
excludeFunctions: true,
9195

92-
// streamline function return values: a function with a scalar
93-
// value will return just the scalar instead of an array, etc.
94-
enhancedFunctions: true
96+
// don't load materialized views (required for Postgres < 9.3)
97+
excludeMatViews: true
9598
}).then(instance => {...});
9699
```
97100

lib/database.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ const getFilterString = require('./util/get-filter-string');
2929
* @param {Array|String} loader.exceptions - Table/view blacklist exceptions.
3030
* @param {Array|String} loader.functionWhitelist - Function name whitelist.
3131
* @param {Array|String} loader.functionBlacklist - Function name blacklist.
32-
* @param {Boolean} loader.excludeFunctions - Ignore functions entirely.
3332
* @param {Boolean} loader.enhancedFunctions - Streamline function return values.
33+
* @param {Boolean} loader.excludeFunctions - Ignore functions entirely.
34+
* @param {Boolean} loader.excludeMatViews - Ignore materialized views.
3435
* @param {Object} [driverConfig] - A pg-promise configuration object.
3536
*/
3637
const Database = function (connection = {}, loader = {}, driverConfig = {}) {

lib/loader/functions.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ const _ = require('lodash');
55
exports = module.exports = function (instance, config) {
66
if (config.excludeFunctions) { return []; }
77

8-
const parameters = [config.functionBlacklist, config.functionWhitelist];
9-
10-
return instance.query(config.queryFiles['functions.sql'], parameters).then(fns => {
8+
return instance.query(config.queryFiles['functions.sql'], config).then(fns => {
119
return fns.map(fn => {
1210
const params = _.times(fn.paramCount, i => `$${i + 1}`).join(',');
1311

lib/loader/tables.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
'use strict';
22

33
exports = module.exports = function (instance, config) {
4-
if (config.whitelist) {
5-
return instance.query(config.queryFiles['tables-whitelist.sql'], [config.whitelist]);
6-
}
7-
8-
return instance.query(config.queryFiles['tables.sql'], [config.allowedSchemas, config.blacklist, config.exceptions]);
4+
return instance.query(config.queryFiles['tables.sql'], config);
95
};

lib/loader/views.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
'use strict';
22

33
exports = module.exports = function (instance, config) {
4-
if (config.whitelist) {
5-
return instance.query(config.queryFiles['views-whitelist.sql'], [config.whitelist]);
6-
}
4+
const file = config.excludeMatViews ? 'views-legacy.sql' : 'views.sql';
75

8-
return instance.query(config.queryFiles['views.sql'], [config.allowedSchemas, config.blacklist, config.exceptions]);
6+
return instance.query(config.queryFiles[file], config);
97
};

lib/scripts/functions.sql

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
-- Include and/or exclude functions with a name matching the string pattern passed in (i.e. "pg_%")
2-
-- inclusion/exclusion is schema - aspecific, no schema assumes 'public'
1+
-- Load non-system functions. The whitelist and blacklist are overlapped such
2+
-- that more specific blacklist entries may override more general whitelist
3+
-- entries.
4+
--
5+
-- Parameters:
6+
-- functionWhitelist: array or comma-delimited string of LIKE conditions.
7+
-- functionBlacklist: array or comma-delimited string of LIKE conditions.
8+
39
SELECT DISTINCT
410
n.nspname AS schema,
511
(NOT p.proretset) AS "singleRow",
@@ -11,13 +17,15 @@ JOIN pg_namespace n ON p.pronamespace = n.oid
1117
JOIN pg_type t on p.prorettype = t.oid
1218
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
1319
AND n.nspname NOT LIKE 'pgp%'
14-
AND (-- blacklist functions using LIKE by fully-qualified name (no schema assumes public):
15-
CASE WHEN $1 = '' THEN 1=1
16-
ELSE replace((n.nspname || '.'|| p.proname), 'public.', '') NOT LIKE ALL(string_to_array(replace($1, ' ', ''), ','))
17-
END
18-
) AND (-- whitelist functions using LIKE by fully-qualified name (no schema assumes public):
19-
CASE WHEN $2 = '' THEN 1=1
20-
ELSE replace((n.nspname || '.'|| p.proname), 'public.', '') LIKE ANY(string_to_array(replace($2, ' ', ''), ','))
21-
END
20+
AND (
21+
-- blacklist functions using LIKE by fully-qualified name (no schema assumes public):
22+
$(functionBlacklist) = ''
23+
OR
24+
replace((n.nspname || '.'|| p.proname), 'public.', '') NOT LIKE ALL(string_to_array(replace($(functionBlacklist), ' ', ''), ','))
25+
) AND (
26+
-- whitelist functions using LIKE by fully-qualified name (no schema assumes public):
27+
$(functionWhitelist) = ''
28+
OR
29+
replace((n.nspname || '.'|| p.proname), 'public.', '') LIKE ANY(string_to_array(replace($(functionWhitelist), ' ', ''), ','))
2230
)
2331
ORDER BY n.nspname, p.proname;

lib/scripts/tables-whitelist.sql

Lines changed: 0 additions & 16 deletions
This file was deleted.

lib/scripts/tables.sql

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
-- REQUIRES THREE ARGUMENTS:
2-
-- $1, $2, $2 all must be empty string, or comma-delimited string, or array of string:
1+
-- Load tables.
2+
--
3+
-- Parameters:
4+
-- whitelist: array or comma-delimited string of LIKE conditions applied to
5+
-- tables. If specified, overrides all other parameters.
6+
-- allowedSchemas: array or comma-delimited string of LIKE conditions applied
7+
-- to schemas.
8+
-- blacklist: array or comma-delimited string of LIKE conditions applied
9+
-- negatively to tables.
10+
-- exceptions: array or comma-delimited string of LIKE conditions which
11+
-- override blacklisted tables.
12+
313
SELECT * FROM (
414
SELECT tc.table_schema AS schema, tc.table_name AS name, NULL AS parent, kc.column_name AS pk, TRUE AS is_insertable_into
515
FROM information_schema.table_constraints tc
@@ -23,19 +33,23 @@ SELECT * FROM (
2333
SELECT t.table_schema AS schema, t.table_name AS name, NULL AS parent, NULL AS pk, CASE t.is_insertable_into WHEN 'YES' THEN TRUE ELSE FALSE END AS is_insertable_into
2434
FROM information_schema.tables t
2535
WHERE table_type = 'FOREIGN TABLE'
26-
) tables WHERE ((
36+
) tables
37+
WHERE CASE
38+
WHEN $(whitelist) <> '' THEN
39+
-- whitelist specific tables, with fully-qualified name (no schema assumes public).
40+
replace((tables.schema || '.'|| tables.name), 'public.', '') LIKE ANY(string_to_array(replace($(whitelist), ' ', ''), ','))
41+
WHEN $(allowedSchemas) <> '' OR $(blacklist) <> '' THEN ((
42+
$(allowedSchemas) = ''
43+
OR
2744
-- allow specific schemas (none or '' assumes all):
28-
CASE WHEN $1 ='' THEN 1=1
29-
ELSE schema = ANY(string_to_array(replace($1, ' ', ''), ','))
30-
END
45+
schema = ANY(string_to_array(replace($(allowedSchemas), ' ', ''), ','))
3146
) AND (
47+
$(blacklist) = ''
48+
OR
3249
-- blacklist tables using LIKE by fully-qualified name (no schema assumes public):
33-
CASE WHEN $2 = '' THEN 1=1
34-
ELSE replace((schema || '.'|| name), 'public.', '') NOT LIKE ALL(string_to_array(replace($2, ' ', ''), ','))
35-
END
36-
)
37-
) OR (-- make exceptions for specific tables, with fully-qualified name or wildcard pattern (no schema assumes public).
38-
CASE WHEN $3 = '' THEN 1=0
39-
ELSE replace((schema || '.'|| name), 'public.', '') LIKE ANY(string_to_array(replace($3, ' ', ''), ','))
40-
END
41-
);
50+
replace((schema || '.'|| name), 'public.', '') NOT LIKE ALL(string_to_array(replace($(blacklist), ' ', ''), ','))
51+
)) OR
52+
-- make exceptions for specific tables, with fully-qualified name or wildcard pattern (no schema assumes public).
53+
replace((schema || '.'|| name), 'public.', '') LIKE ANY(string_to_array(replace($(exceptions), ' ', ''), ','))
54+
ELSE TRUE
55+
END;

lib/scripts/views-legacy.sql

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- Load views (excluding materialized views).
2+
--
3+
-- Parameters:
4+
-- whitelist: array or comma-delimited string of LIKE conditions applied to
5+
-- tables. If specified, overrides all other parameters.
6+
-- allowedSchemas: array or comma-delimited string of LIKE conditions applied
7+
-- to schemas.
8+
-- blacklist: array or comma-delimited string of LIKE conditions applied
9+
-- negatively to tables.
10+
-- exceptions: array or comma-delimited string of LIKE conditions which
11+
-- override blacklisted tables.
12+
13+
SELECT * FROM (
14+
SELECT schemaname AS schema, viewname AS name
15+
FROM pg_views
16+
WHERE schemaname <> 'pg_catalog' AND schemaname <> 'information_schema'
17+
) views
18+
WHERE CASE
19+
WHEN $(whitelist) <> '' THEN
20+
-- whitelist specific tables, with fully-qualified name (no schema assumes public).
21+
replace((views.schema || '.'|| views.name), 'public.', '') LIKE ANY(string_to_array(replace($(whitelist), ' ', ''), ','))
22+
WHEN $(allowedSchemas) <> '' OR $(blacklist) <> '' THEN ((
23+
$(allowedSchemas) = ''
24+
OR
25+
-- allow specific schemas (none or '' assumes all):
26+
schema = ANY(string_to_array(replace($(allowedSchemas), ' ', ''), ','))
27+
) AND (
28+
$(blacklist) = ''
29+
OR
30+
-- blacklist tables using LIKE by fully-qualified name (no schema assumes public):
31+
replace((schema || '.'|| name), 'public.', '') NOT LIKE ALL(string_to_array(replace($(blacklist), ' ', ''), ','))
32+
)) OR
33+
-- make exceptions for specific tables, with fully-qualified name or wildcard pattern (no schema assumes public).
34+
replace((schema || '.'|| name), 'public.', '') LIKE ANY(string_to_array(replace($(exceptions), ' ', ''), ','))
35+
ELSE TRUE
36+
END;

lib/scripts/views-whitelist.sql

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)