({ schema, table, db }: { schema: string; table: string; db: DB })
| 1592 | }; |
| 1593 | |
| 1594 | const getColumnsInfoQuery = ({ schema, table, db }: { schema: string; table: string; db: DB }) => { |
| 1595 | return db.query( |
| 1596 | `SELECT |
| 1597 | a.attrelid::regclass::text AS table_name, -- Table, view, or materialized view name |
| 1598 | a.attname::text AS column_name, -- Column name |
| 1599 | CASE |
| 1600 | WHEN NOT a.attisdropped THEN |
| 1601 | CASE |
| 1602 | WHEN a.attnotnull THEN 'NO' |
| 1603 | ELSE 'YES' |
| 1604 | END |
| 1605 | ELSE NULL |
| 1606 | END AS is_nullable, -- NULL or NOT NULL constraint |
| 1607 | a.attndims AS array_dimensions, -- Array dimensions |
| 1608 | CASE |
| 1609 | WHEN a.atttypid = ANY ('{int,int8,int2}'::regtype[]) |
| 1610 | AND EXISTS ( |
| 1611 | SELECT FROM pg_attrdef ad |
| 1612 | WHERE ad.adrelid = a.attrelid |
| 1613 | AND ad.adnum = a.attnum |
| 1614 | AND pg_get_expr(ad.adbin, ad.adrelid) = 'nextval(''' |
| 1615 | || pg_get_serial_sequence(a.attrelid::regclass::text, a.attname)::regclass || '''::regclass)' |
| 1616 | ) |
| 1617 | THEN CASE a.atttypid |
| 1618 | WHEN 'int'::regtype THEN 'serial' |
| 1619 | WHEN 'int8'::regtype THEN 'bigserial' |
| 1620 | WHEN 'int2'::regtype THEN 'smallserial' |
| 1621 | END |
| 1622 | ELSE format_type(a.atttypid, a.atttypmod) |
| 1623 | END AS data_type, -- Column data type |
| 1624 | -- ns.nspname AS type_schema, -- Schema name |
| 1625 | c.column_default::text, -- Column default value |
| 1626 | c.data_type::text AS additional_dt, -- Data type from information_schema |
| 1627 | c.udt_name::text AS enum_name, -- Enum type (if applicable) |
| 1628 | c.is_generated::text, -- Is it a generated column? |
| 1629 | c.generation_expression::text, -- Generation expression (if generated) |
| 1630 | c.is_identity::text, -- Is it an identity column? |
| 1631 | c.identity_generation::text, -- Identity generation strategy (ALWAYS or BY DEFAULT) |
| 1632 | c.identity_start::text, -- Start value of identity column |
| 1633 | c.identity_increment::text, -- Increment for identity column |
| 1634 | c.identity_maximum::text, -- Maximum value for identity column |
| 1635 | c.identity_minimum::text, -- Minimum value for identity column |
| 1636 | c.identity_cycle::text, -- Does the identity column cycle? |
| 1637 | ns.nspname::text AS type_schema -- Schema of the enum type |
| 1638 | FROM |
| 1639 | pg_attribute a |
| 1640 | JOIN |
| 1641 | pg_class cls ON cls.oid = a.attrelid -- Join pg_class to get table/view/materialized view info |
| 1642 | JOIN |
| 1643 | pg_namespace ns ON ns.oid = cls.relnamespace -- Join namespace to get schema info |
| 1644 | LEFT JOIN |
| 1645 | information_schema.columns c ON c.column_name = a.attname |
| 1646 | AND c.table_schema = ns.nspname |
| 1647 | AND c.table_name = cls.relname -- Match schema and table/view name |
| 1648 | LEFT JOIN |
| 1649 | pg_type enum_t ON enum_t.oid = a.atttypid -- Join to get the type info |
| 1650 | LEFT JOIN |
| 1651 | pg_namespace enum_ns ON enum_ns.oid = enum_t.typnamespace -- Join to get the enum schema |
no test coverage detected