67 lines
2.9 KiB
SQL
67 lines
2.9 KiB
SQL
-- Reports what actually exists in the application schema.
|
|
-- Read-only. Uses information_schema and pg_indexes, both standard views;
|
|
-- no system catalog is written and no object is modified.
|
|
|
|
\echo '── Applied migration ─────────────────────────────────────────────────'
|
|
SELECT version, dirty FROM schema_migrations;
|
|
|
|
\echo ''
|
|
\echo '── Tables in the application schema ──────────────────────────────────'
|
|
SELECT table_name
|
|
FROM information_schema.tables
|
|
WHERE table_schema = current_schema() AND table_type = 'BASE TABLE'
|
|
AND table_name <> 'schema_migrations'
|
|
ORDER BY table_name;
|
|
|
|
\echo ''
|
|
\echo '── Enum types and their values ───────────────────────────────────────'
|
|
SELECT t.typname AS enum_type,
|
|
string_agg(e.enumlabel, ', ' ORDER BY e.enumsortorder) AS values
|
|
FROM pg_type t
|
|
JOIN pg_enum e ON e.enumtypid = t.oid
|
|
JOIN pg_namespace n ON n.oid = t.typnamespace
|
|
WHERE n.nspname = current_schema()
|
|
GROUP BY t.typname
|
|
ORDER BY t.typname;
|
|
|
|
\echo ''
|
|
\echo '── Constraint counts by type ─────────────────────────────────────────'
|
|
SELECT CASE contype
|
|
WHEN 'p' THEN 'primary key'
|
|
WHEN 'f' THEN 'foreign key'
|
|
WHEN 'u' THEN 'unique'
|
|
WHEN 'c' THEN 'check'
|
|
END AS constraint_type,
|
|
count(*) AS total
|
|
FROM pg_constraint c
|
|
JOIN pg_namespace n ON n.oid = c.connamespace
|
|
WHERE n.nspname = current_schema() AND contype IN ('p','f','u','c')
|
|
GROUP BY contype
|
|
ORDER BY contype;
|
|
|
|
\echo ''
|
|
\echo '── Foreign keys ──────────────────────────────────────────────────────'
|
|
SELECT conrelid::regclass::text AS child,
|
|
confrelid::regclass::text AS parent,
|
|
conname AS constraint_name
|
|
FROM pg_constraint c
|
|
JOIN pg_namespace n ON n.oid = c.connamespace
|
|
WHERE n.nspname = current_schema() AND contype = 'f'
|
|
ORDER BY child, parent, conname;
|
|
|
|
\echo ''
|
|
\echo '── Indexes ───────────────────────────────────────────────────────────'
|
|
SELECT tablename, indexname
|
|
FROM pg_indexes
|
|
WHERE schemaname = current_schema() AND tablename <> 'schema_migrations'
|
|
ORDER BY tablename, indexname;
|
|
|
|
\echo ''
|
|
\echo '── Objects outside the application schema created by this migration ──'
|
|
\echo '(expected: zero rows)'
|
|
SELECT n.nspname AS schema, c.relname AS object
|
|
FROM pg_class c
|
|
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
WHERE n.nspname NOT IN ('pg_catalog','information_schema','pg_toast', current_schema())
|
|
AND c.relkind IN ('r','i','S');
|