graphql-engine/server/src-rsr/migrations/6_to_7.sql
Ajeet D'Souza a66fb42ce2 Make catalog metadata migrations work on all schema versions (fix #2826) (#2379)
* Separate DB and metadata migrations
* Refactor Migrate.hs to generate list of migrations at compile-time
* Replace ginger with shakespeare to improve performance
* Improve migration log messages
2019-10-11 00:13:57 -05:00

107 lines
3.0 KiB
SQL

CREATE OR REPLACE VIEW hdb_catalog.hdb_primary_key AS
SELECT
tc.table_schema,
tc.table_name,
tc.constraint_name,
json_agg(constraint_column_usage.column_name) AS columns
FROM
(
information_schema.table_constraints tc
JOIN (
SELECT
x.tblschema AS table_schema,
x.tblname AS table_name,
x.colname AS column_name,
x.cstrname AS constraint_name
FROM
(
SELECT
DISTINCT nr.nspname,
r.relname,
a.attname,
c.conname
FROM
pg_namespace nr,
pg_class r,
pg_attribute a,
pg_depend d,
pg_namespace nc,
pg_constraint c
WHERE
(
(nr.oid = r.relnamespace)
AND (r.oid = a.attrelid)
AND (d.refclassid = ('pg_class' :: regclass) :: oid)
AND (d.refobjid = r.oid)
AND (d.refobjsubid = a.attnum)
AND (d.classid = ('pg_constraint' :: regclass) :: oid)
AND (d.objid = c.oid)
AND (c.connamespace = nc.oid)
AND (c.contype = 'c' :: "char")
AND (
r.relkind = ANY (ARRAY ['r'::"char", 'p'::"char"])
)
AND (NOT a.attisdropped)
)
UNION ALL
SELECT
nr.nspname,
r.relname,
a.attname,
c.conname
FROM
pg_namespace nr,
pg_class r,
pg_attribute a,
pg_namespace nc,
pg_constraint c
WHERE
(
(nr.oid = r.relnamespace)
AND (r.oid = a.attrelid)
AND (nc.oid = c.connamespace)
AND (
r.oid = CASE
c.contype
WHEN 'f' :: "char" THEN c.confrelid
ELSE c.conrelid
END
)
AND (
a.attnum = ANY (
CASE
c.contype
WHEN 'f' :: "char" THEN c.confkey
ELSE c.conkey
END
)
)
AND (NOT a.attisdropped)
AND (
c.contype = ANY (ARRAY ['p'::"char", 'u'::"char", 'f'::"char"])
)
AND (
r.relkind = ANY (ARRAY ['r'::"char", 'p'::"char"])
)
)
) x(
tblschema,
tblname,
colname,
cstrname
)
) constraint_column_usage ON (
(
(tc.constraint_name) :: text = (constraint_column_usage.constraint_name) :: text
AND (tc.table_schema) :: text = (constraint_column_usage.table_schema) :: text
AND (tc.table_name) :: text = (constraint_column_usage.table_name) :: text
)
)
)
WHERE
((tc.constraint_type) :: text = 'PRIMARY KEY' :: text)
GROUP BY
tc.table_schema,
tc.table_name,
tc.constraint_name;