Last active
August 29, 2015 14:23
-
-
Save xivSolutions/5c7a18da106d39d29bc4 to your computer and use it in GitHub Desktop.
MassiveJS Tables with PK JSON Field
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_maximim_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 -- allow specific schemas (none or '' assumes all): | |
WHEN $1 ='' THEN 1=1 | |
ELSE t.table_schema = ANY(string_to_array(REPLACE($1, ' ', ''), ',')) END) | |
AND | |
(CASE -- blacklist tables using LIKE by fully-qualified name (no schema assumes public): | |
WHEN $2 = '' THEN 1=1 | |
ELSE REPLACE((t.table_schema || '.'|| t.table_name), 'public.', '') NOT LIKE ALL(string_to_array(REPLACE($2, ' ', ''), ',')) END)) | |
OR | |
(CASE -- make exceptions for specific tables, with fully-qualified name or wildcard pattern (no schema assumes public). | |
WHEN $3 = '' THEN 1=0 | |
-- Below can use '%' as wildcard. Change 'like' to '=' to require exact names: | |
ELSE REPLACE((t.table_schema || '.'|| t.table_name), 'public.', '') LIKE ANY(string_to_array(REPLACE($3, ' ', ''), ',')) 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