Skip to content

Database diagrams editor that allows you to visualize and design your DB with a single query.

import script pg - add fetch comments for tables and columns

johnnyfish81c7bcc95927
diff --git a/src/lib/data/import-metadata/metadata-types/column-info.ts b/src/lib/data/import-metadata/metadata-types/column-info.ts
index d8962605..03d3ac14 100644
--- a/src/lib/data/import-metadata/metadata-types/column-info.ts
+++ b/src/lib/data/import-metadata/metadata-types/column-info.ts
@@ -12,4 +12,5 @@ export interface ColumnInfo {
} | null; // Nullable, not all types have precision
default?: string | null; // Default value for the column, nullable
collation?: string | null;
+ comment?: string | null;
}
diff --git a/src/lib/data/import-metadata/metadata-types/table-info.ts b/src/lib/data/import-metadata/metadata-types/table-info.ts
index 494ace7a..088f57eb 100644
--- a/src/lib/data/import-metadata/metadata-types/table-info.ts
+++ b/src/lib/data/import-metadata/metadata-types/table-info.ts
@@ -5,4 +5,5 @@ export interface TableInfo {
type: string;
engine: string;
collation: string;
+ comment?: string;
}
diff --git a/src/lib/data/import-metadata/scripts/postgres-script.ts b/src/lib/data/import-metadata/scripts/postgres-script.ts
index 7ba44bfe..29711394 100644
--- a/src/lib/data/import-metadata/scripts/postgres-script.ts
+++ b/src/lib/data/import-metadata/scripts/postgres-script.ts
@@ -158,8 +158,15 @@ cols AS (
END,
',"nullable":', CASE WHEN (cols.IS_NULLABLE = 'YES') THEN 'true' ELSE 'false' END,
',"default":"', COALESCE(replace(replace(cols.column_default, '"', '\\"'), '\\x', '\\\\x'), ''),
- '","collation":"', COALESCE(cols.COLLATION_NAME, ''), '"}')), ',') AS cols_metadata
+ '","collation":"', COALESCE(cols.COLLATION_NAME, ''),
+ '","comment":"', COALESCE(dsc.description, ''), '"}')), ',') AS cols_metadata
FROM information_schema.columns cols
+ LEFT JOIN pg_catalog.pg_class c
+ ON c.relname = cols.table_name
+ LEFT JOIN pg_catalog.pg_namespace n
+ ON n.oid = c.relnamespace AND n.nspname = cols.table_schema
+ LEFT JOIN pg_catalog.pg_description dsc ON dsc.objoid = c.oid
+ AND dsc.objsubid = cols.ordinal_position
WHERE cols.table_schema NOT IN ('information_schema', 'pg_catalog')${
databaseEdition === DatabaseEdition.POSTGRESQL_TIMESCALE
? timescaleColFilter
@@ -187,20 +194,29 @@ cols AS (
: ''
}
), tbls AS (
- SELECT array_to_string(array_agg(CONCAT('{', '"schema":"', TABLE_SCHEMA, '",', '"table":"', TABLE_NAME, '",', '"rows":',
- COALESCE((SELECT s.n_live_tup
+ SELECT array_to_string(array_agg(CONCAT('{',
+ '"schema":"', tbls.TABLE_SCHEMA, '",',
+ '"table":"', tbls.TABLE_NAME, '",',
+ '"rows":', COALESCE((SELECT s.n_live_tup
FROM pg_stat_user_tables s
WHERE tbls.TABLE_SCHEMA = s.schemaname AND tbls.TABLE_NAME = s.relname),
- 0), ', "type":"', TABLE_TYPE, '",', '"engine":"",', '"collation":""}')),
- ',') AS tbls_metadata
- FROM information_schema.tables tbls
- WHERE tbls.TABLE_SCHEMA NOT IN ('information_schema', 'pg_catalog') ${
- databaseEdition === DatabaseEdition.POSTGRESQL_TIMESCALE
- ? timescaleTableFilter
- : databaseEdition === DatabaseEdition.POSTGRESQL_SUPABASE
- ? supabaseTableFilter
- : ''
- }
+ 0), ', "type":"', tbls.TABLE_TYPE, '",', '"engine":"",', '"collation":"",',
+ '"comment":"', COALESCE(dsc.description, ''), '"}'
+ )),
+ ',') AS tbls_metadata
+ FROM information_schema.tables tbls
+ LEFT JOIN pg_catalog.pg_class c ON c.relname = tbls.TABLE_NAME
+ LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
+ AND n.nspname = tbls.TABLE_SCHEMA
+ LEFT JOIN pg_catalog.pg_description dsc ON dsc.objoid = c.oid
+ AND dsc.objsubid = 0
+ WHERE tbls.TABLE_SCHEMA NOT IN ('information_schema', 'pg_catalog') ${
+ databaseEdition === DatabaseEdition.POSTGRESQL_TIMESCALE
+ ? timescaleTableFilter
+ : databaseEdition === DatabaseEdition.POSTGRESQL_SUPABASE
+ ? supabaseTableFilter
+ : ''
+ }
), config AS (
SELECT array_to_string(
array_agg(CONCAT('{"name":"', conf.name, '","value":"', replace(conf.setting, '"', E'"'), '"}')),