Skip to content

Commit

Permalink
introspect column comments for mysql and postgres
Browse files Browse the repository at this point in the history
Signed-off-by: Makara Sok <me@maktouch.com>
  • Loading branch information
maktouch committed Jan 11, 2024
1 parent 9deb62a commit 4e14fc1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/dialect/database-introspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ export interface ColumnMetadata {
readonly isAutoIncrementing: boolean
readonly isNullable: boolean
readonly hasDefaultValue: boolean
readonly comment?: string | null
}
3 changes: 3 additions & 0 deletions src/dialect/mysql/mysql-introspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
})
)

Expand All @@ -117,4 +119,5 @@ interface RawColumnMetadata {
IS_NULLABLE: 'YES' | 'NO'
DATA_TYPE: string
EXTRA: string
COLUMN_COMMENT: string
}
3 changes: 3 additions & 0 deletions src/dialect/postgres/postgres-introspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
})
)

Expand All @@ -148,4 +150,5 @@ interface RawColumnMetadata {
type: string
type_schema: string
auto_incrementing: boolean | null
column_description: string | null
}
41 changes: 41 additions & 0 deletions test/node/src/introspect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ for (const dialect of DIALECTS) {
isNullable: false,
isAutoIncrementing: true,
hasDefaultValue: true,
column: null
},
{
name: 'first_name',
Expand All @@ -102,6 +103,7 @@ for (const dialect of DIALECTS) {
isNullable: true,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
{
name: 'middle_name',
Expand All @@ -110,6 +112,7 @@ for (const dialect of DIALECTS) {
isNullable: true,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},

{
Expand All @@ -119,6 +122,7 @@ for (const dialect of DIALECTS) {
isNullable: true,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
{
name: 'gender',
Expand All @@ -127,6 +131,7 @@ for (const dialect of DIALECTS) {
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
{
name: 'marital_status',
Expand All @@ -135,6 +140,7 @@ for (const dialect of DIALECTS) {
isNullable: true,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
{
name: 'children',
Expand All @@ -143,6 +149,7 @@ for (const dialect of DIALECTS) {
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: true,
column: null
},
],
},
Expand All @@ -158,6 +165,7 @@ for (const dialect of DIALECTS) {
isNullable: false,
isAutoIncrementing: true,
hasDefaultValue: true,
column: null
},
{
name: 'name',
Expand All @@ -166,6 +174,7 @@ for (const dialect of DIALECTS) {
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
{
name: 'owner_id',
Expand All @@ -174,6 +183,7 @@ for (const dialect of DIALECTS) {
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
{
name: 'species',
Expand All @@ -182,6 +192,7 @@ for (const dialect of DIALECTS) {
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
],
},
Expand All @@ -197,6 +208,7 @@ for (const dialect of DIALECTS) {
isNullable: false,
isAutoIncrementing: true,
hasDefaultValue: true,
column: null
},
{
name: 'name',
Expand All @@ -205,6 +217,7 @@ for (const dialect of DIALECTS) {
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
{
name: 'pet_id',
Expand All @@ -213,6 +226,7 @@ for (const dialect of DIALECTS) {
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
{
name: 'price',
Expand All @@ -221,6 +235,7 @@ for (const dialect of DIALECTS) {
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
],
},
Expand All @@ -236,6 +251,7 @@ for (const dialect of DIALECTS) {
isNullable: true,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
],
},
Expand All @@ -251,6 +267,7 @@ for (const dialect of DIALECTS) {
isNullable: false,
isAutoIncrementing: true,
hasDefaultValue: true,
column: null
},
{
dataType: 'species',
Expand All @@ -259,6 +276,7 @@ for (const dialect of DIALECTS) {
isAutoIncrementing: false,
isNullable: true,
name: 'spcies',
column: null
},
],
},
Expand All @@ -276,27 +294,31 @@ for (const dialect of DIALECTS) {
isNullable: false,
isAutoIncrementing: true,
hasDefaultValue: false,
column: null
},
{
name: 'first_name',
dataType: 'varchar',
isNullable: true,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
{
name: 'middle_name',
dataType: 'varchar',
isNullable: true,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
{
name: 'last_name',
dataType: 'varchar',
isNullable: true,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},

{
Expand All @@ -305,20 +327,23 @@ for (const dialect of DIALECTS) {
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
{
name: 'marital_status',
dataType: 'varchar',
isNullable: true,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
{
name: 'children',
dataType: 'int',
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: true,
column: null
},
],
},
Expand All @@ -333,27 +358,31 @@ for (const dialect of DIALECTS) {
isNullable: false,
isAutoIncrementing: true,
hasDefaultValue: false,
column: null
},
{
name: 'name',
dataType: 'varchar',
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
{
name: 'owner_id',
dataType: 'int',
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
{
name: 'species',
dataType: 'varchar',
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
],
},
Expand All @@ -368,27 +397,31 @@ for (const dialect of DIALECTS) {
isNullable: false,
isAutoIncrementing: true,
hasDefaultValue: false,
column: null
},
{
name: 'name',
dataType: 'varchar',
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
{
name: 'pet_id',
dataType: 'int',
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
{
name: 'price',
dataType: 'double',
isNullable: false,
isAutoIncrementing: false,
hasDefaultValue: false,
column: null
},
],
},
Expand All @@ -403,6 +436,7 @@ for (const dialect of DIALECTS) {
isAutoIncrementing: false,
isNullable: false,
name: 'name',
column: null
},
],
},
Expand All @@ -421,6 +455,7 @@ for (const dialect of DIALECTS) {
isAutoIncrementing: false,
isNullable: false,
name: 'children',
column: null
},
{
dataType: 'varchar',
Expand All @@ -429,6 +464,7 @@ for (const dialect of DIALECTS) {
isAutoIncrementing: false,
isNullable: true,
name: 'first_name',
column: null
},
{
dataType: 'varchar',
Expand All @@ -437,6 +473,7 @@ for (const dialect of DIALECTS) {
isAutoIncrementing: false,
isNullable: false,
name: 'gender',
column: null
},
{
dataType: 'int',
Expand All @@ -445,6 +482,7 @@ for (const dialect of DIALECTS) {
isAutoIncrementing: true,
isNullable: false,
name: 'id',
column: null
},
{
dataType: 'varchar',
Expand All @@ -453,6 +491,7 @@ for (const dialect of DIALECTS) {
isAutoIncrementing: false,
isNullable: true,
name: 'last_name',
column: null
},
{
dataType: 'varchar',
Expand All @@ -461,6 +500,7 @@ for (const dialect of DIALECTS) {
isAutoIncrementing: false,
isNullable: true,
name: 'marital_status',
column: null
},
{
dataType: 'varchar',
Expand All @@ -469,6 +509,7 @@ for (const dialect of DIALECTS) {
isAutoIncrementing: false,
isNullable: true,
name: 'middle_name',
column: null
},
],
},
Expand Down

0 comments on commit 4e14fc1

Please sign in to comment.