Created
June 15, 2015 10:39
-
-
Save xivSolutions/08a7a597721117564f8e to your computer and use it in GitHub Desktop.
MassiveJS Table Keys v3
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
deallocate tables; | |
prepare tables(text, text, text) as | |
SELECT c.TABLE_NAME, c.COLUMN_NAME, kcu.CONSTRAINT_NAME, c.DATA_TYPE, c.CHARACTER_MAXIMUM_LENGTH, tc.CONSTRAINT_TYPE, | |
CASE tc.CONSTRAINT_TYPE WHEN 'PRIMARY KEY' THEN CAST(1 AS BIt) ELSE CAST(0 AS Bit) END AS IsPrimaryKey, | |
c.column_default is not null as is_auto, | |
c.column_default | |
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 | |
((case -- allow specific schemas (none or '' assumes all): | |
when $1 ='' then 1=1 | |
else tc.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((tc.table_schema || '.'|| tc.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((tc.table_schema || '.'|| tc.table_name), 'public.', '') like any(string_to_array(replace($3, ' ', ''), ',')) end) | |
order by tc.table_schema, | |
tc.table_name, | |
kcu.position_in_unique_constraint; | |
execute tables('', '', ''); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment