Created
June 20, 2015 22:00
-
-
Save xivSolutions/326e50961f3ab4d6a5bf to your computer and use it in GitHub Desktop.
MassiveJS Whitelisted Tables with PK Field as JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT t.table_schema AS schema, t.table_name AS name, | |
(SELECT json_agg(keys) from ( | |
SELECT c.column_name, c.data_type, c.character_maximum_length AS char_length, | |
c.column_default IS NOT NULL AS is_auto | |
FROM information_schema.columns c | |
LEFT OUTER JOIN information_schema.key_column_usage kcu | |
ON c.table_schema = kcu.constraint_schema AND c.table_name = kcu.table_name AND c.column_name = kcu.column_name | |
LEFT OUTER JOIN information_schema.table_constraints tc | |
ON kcu.constraint_schema = tc.constraint_schema AND kcu.constraint_name = tc.constraint_name | |
WHERE | |
tc.constraint_type = 'PRIMARY KEY' AND c.table_name = t.table_name AND c.table_schema = t.table_schema | |
) AS keys | |
) AS pk_columns | |
FROM information_schema.tables AS t | |
WHERE t.table_schema NOT IN('information_schema', 'pg_catalog') AND | |
(case -- whitelist specific tables, with fully-qualified name (no schema assumes public). | |
when $1 = '' then 1=1 | |
-- Below can use '%' as wildcard. Change '=' to 'like' to allow patterns rather then exact names: | |
else replace((t.table_schema || '.'|| t.table_name), 'public.', '') = any(string_to_array(replace($1, ' ', ''), ',')) end) | |
ORDER BY t.table_schema, | |
t.table_name; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment