From 4e14fc14690ce41892d587c469469d3aca4cb3ca Mon Sep 17 00:00:00 2001 From: Makara Sok Date: Thu, 11 Jan 2024 12:25:25 -0500 Subject: [PATCH] introspect column comments for mysql and postgres Signed-off-by: Makara Sok --- src/dialect/database-introspector.ts | 1 + src/dialect/mysql/mysql-introspector.ts | 3 ++ src/dialect/postgres/postgres-introspector.ts | 3 ++ test/node/src/introspect.test.ts | 41 +++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/src/dialect/database-introspector.ts b/src/dialect/database-introspector.ts index 2ef58ea10..fec125a2d 100644 --- a/src/dialect/database-introspector.ts +++ b/src/dialect/database-introspector.ts @@ -68,4 +68,5 @@ export interface ColumnMetadata { readonly isAutoIncrementing: boolean readonly isNullable: boolean readonly hasDefaultValue: boolean + readonly comment?: string | null } diff --git a/src/dialect/mysql/mysql-introspector.ts b/src/dialect/mysql/mysql-introspector.ts index 19dd3f836..62eee3aa5 100644 --- a/src/dialect/mysql/mysql-introspector.ts +++ b/src/dialect/mysql/mysql-introspector.ts @@ -50,6 +50,7 @@ export class MysqlIntrospector implements DatabaseIntrospector { 'columns.IS_NULLABLE', 'columns.DATA_TYPE', 'columns.EXTRA', + 'columns.COLUMN_COMMENT', ]) .where('columns.TABLE_SCHEMA', '=', sql`database()`) .orderBy('columns.TABLE_NAME') @@ -96,6 +97,7 @@ export class MysqlIntrospector implements DatabaseIntrospector { isNullable: it.IS_NULLABLE === 'YES', isAutoIncrementing: it.EXTRA.toLowerCase().includes('auto_increment'), hasDefaultValue: it.COLUMN_DEFAULT !== null, + comment: it.COLUMN_COMMENT === '' ? null : it.COLUMN_COMMENT }) ) @@ -117,4 +119,5 @@ interface RawColumnMetadata { IS_NULLABLE: 'YES' | 'NO' DATA_TYPE: string EXTRA: string + COLUMN_COMMENT: string } diff --git a/src/dialect/postgres/postgres-introspector.ts b/src/dialect/postgres/postgres-introspector.ts index 579afff51..01bf37055 100644 --- a/src/dialect/postgres/postgres-introspector.ts +++ b/src/dialect/postgres/postgres-introspector.ts @@ -57,6 +57,7 @@ export class PostgresIntrospector implements DatabaseIntrospector { 'ns.nspname as schema', 'typ.typname as type', 'dtns.nspname as type_schema', + sql`pg_catalog.col_description(format('%s.%s',isc.table_schema,isc.table_name)::regclass::oid,isc.ordinal_position)`.as('column_description'), // Detect if the column is auto incrementing by finding the sequence // that is created for `serial` and `bigserial` columns. @@ -126,6 +127,7 @@ export class PostgresIntrospector implements DatabaseIntrospector { isNullable: !it.not_null, isAutoIncrementing: !!it.auto_incrementing, hasDefaultValue: it.has_default, + comment: it.column_description }) ) @@ -148,4 +150,5 @@ interface RawColumnMetadata { type: string type_schema: string auto_incrementing: boolean | null + column_description: string | null } diff --git a/test/node/src/introspect.test.ts b/test/node/src/introspect.test.ts index f2fe4e116..2ea1aa97a 100644 --- a/test/node/src/introspect.test.ts +++ b/test/node/src/introspect.test.ts @@ -94,6 +94,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: true, hasDefaultValue: true, + column: null }, { name: 'first_name', @@ -102,6 +103,7 @@ for (const dialect of DIALECTS) { isNullable: true, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { name: 'middle_name', @@ -110,6 +112,7 @@ for (const dialect of DIALECTS) { isNullable: true, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { @@ -119,6 +122,7 @@ for (const dialect of DIALECTS) { isNullable: true, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { name: 'gender', @@ -127,6 +131,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { name: 'marital_status', @@ -135,6 +140,7 @@ for (const dialect of DIALECTS) { isNullable: true, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { name: 'children', @@ -143,6 +149,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: false, hasDefaultValue: true, + column: null }, ], }, @@ -158,6 +165,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: true, hasDefaultValue: true, + column: null }, { name: 'name', @@ -166,6 +174,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { name: 'owner_id', @@ -174,6 +183,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { name: 'species', @@ -182,6 +192,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, ], }, @@ -197,6 +208,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: true, hasDefaultValue: true, + column: null }, { name: 'name', @@ -205,6 +217,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { name: 'pet_id', @@ -213,6 +226,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { name: 'price', @@ -221,6 +235,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, ], }, @@ -236,6 +251,7 @@ for (const dialect of DIALECTS) { isNullable: true, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, ], }, @@ -251,6 +267,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: true, hasDefaultValue: true, + column: null }, { dataType: 'species', @@ -259,6 +276,7 @@ for (const dialect of DIALECTS) { isAutoIncrementing: false, isNullable: true, name: 'spcies', + column: null }, ], }, @@ -276,6 +294,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: true, hasDefaultValue: false, + column: null }, { name: 'first_name', @@ -283,6 +302,7 @@ for (const dialect of DIALECTS) { isNullable: true, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { name: 'middle_name', @@ -290,6 +310,7 @@ for (const dialect of DIALECTS) { isNullable: true, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { name: 'last_name', @@ -297,6 +318,7 @@ for (const dialect of DIALECTS) { isNullable: true, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { @@ -305,6 +327,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { name: 'marital_status', @@ -312,6 +335,7 @@ for (const dialect of DIALECTS) { isNullable: true, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { name: 'children', @@ -319,6 +343,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: false, hasDefaultValue: true, + column: null }, ], }, @@ -333,6 +358,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: true, hasDefaultValue: false, + column: null }, { name: 'name', @@ -340,6 +366,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { name: 'owner_id', @@ -347,6 +374,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { name: 'species', @@ -354,6 +382,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, ], }, @@ -368,6 +397,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: true, hasDefaultValue: false, + column: null }, { name: 'name', @@ -375,6 +405,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { name: 'pet_id', @@ -382,6 +413,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, { name: 'price', @@ -389,6 +421,7 @@ for (const dialect of DIALECTS) { isNullable: false, isAutoIncrementing: false, hasDefaultValue: false, + column: null }, ], }, @@ -403,6 +436,7 @@ for (const dialect of DIALECTS) { isAutoIncrementing: false, isNullable: false, name: 'name', + column: null }, ], }, @@ -421,6 +455,7 @@ for (const dialect of DIALECTS) { isAutoIncrementing: false, isNullable: false, name: 'children', + column: null }, { dataType: 'varchar', @@ -429,6 +464,7 @@ for (const dialect of DIALECTS) { isAutoIncrementing: false, isNullable: true, name: 'first_name', + column: null }, { dataType: 'varchar', @@ -437,6 +473,7 @@ for (const dialect of DIALECTS) { isAutoIncrementing: false, isNullable: false, name: 'gender', + column: null }, { dataType: 'int', @@ -445,6 +482,7 @@ for (const dialect of DIALECTS) { isAutoIncrementing: true, isNullable: false, name: 'id', + column: null }, { dataType: 'varchar', @@ -453,6 +491,7 @@ for (const dialect of DIALECTS) { isAutoIncrementing: false, isNullable: true, name: 'last_name', + column: null }, { dataType: 'varchar', @@ -461,6 +500,7 @@ for (const dialect of DIALECTS) { isAutoIncrementing: false, isNullable: true, name: 'marital_status', + column: null }, { dataType: 'varchar', @@ -469,6 +509,7 @@ for (const dialect of DIALECTS) { isAutoIncrementing: false, isNullable: true, name: 'middle_name', + column: null }, ], },