Skip to content

Commit

Permalink
Comment introspection implementation for mssql, 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 12, 2024
1 parent 9deb62a commit b2dd8a3
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 4 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
}
23 changes: 23 additions & 0 deletions src/dialect/mssql/mssql-introspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ export class MssqlIntrospector implements DatabaseIntrospector {
'type_schemas.schema_id',
'types.schema_id'
)
.leftJoin(
'sys.extended_properties as comments',
join => join
.onRef('comments.major_id', '=', 'tables.object_id')
.onRef('comments.minor_id', '=', 'columns.column_id')
.on('comments.name', '=', 'MS_Description')
)
.$if(!options.withInternalKyselyTables, (qb) =>
qb
.where('tables.name', '!=', DEFAULT_MIGRATION_TABLE)
Expand All @@ -74,6 +81,7 @@ export class MssqlIntrospector implements DatabaseIntrospector {
'types.is_nullable as type_is_nullable',
'types.name as type_name',
'type_schemas.name as type_schema_name',
'comments.value as column_comment'
])
.unionAll(
this.#db
Expand All @@ -98,6 +106,13 @@ export class MssqlIntrospector implements DatabaseIntrospector {
'type_schemas.schema_id',
'types.schema_id'
)
.leftJoin(
'sys.extended_properties as comments',
join => join
.onRef('comments.major_id', '=', 'views.object_id')
.onRef('comments.minor_id', '=', 'columns.column_id')
.on('comments.name', '=', 'MS_Description')
)
.select([
'views.name as table_name',
'views.type as table_type',
Expand All @@ -112,6 +127,7 @@ export class MssqlIntrospector implements DatabaseIntrospector {
'types.is_nullable as type_is_nullable',
'types.name as type_name',
'type_schemas.name as type_schema_name',
'comments.value as column_comment'
])
)
.orderBy('table_schema_name')
Expand Down Expand Up @@ -147,6 +163,7 @@ export class MssqlIntrospector implements DatabaseIntrospector {
isNullable:
rawColumn.column_is_nullable && rawColumn.type_is_nullable,
name: rawColumn.column_name,
comment: rawColumn.column_comment ?? undefined,
})
)
}
Expand Down Expand Up @@ -204,6 +221,12 @@ interface MssqlSysTables {
// scale: number
system_type_id: number
}
'sys.extended_properties': {
major_id: number
minor_id: number
name: string
value: string
}
'sys.schemas': {
name: string
// principal_id: number
Expand Down
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 === '' ? undefined : it.COLUMN_COMMENT
})
)

Expand All @@ -117,4 +119,5 @@ interface RawColumnMetadata {
IS_NULLABLE: 'YES' | 'NO'
DATA_TYPE: string
EXTRA: string
COLUMN_COMMENT: string
}
4 changes: 3 additions & 1 deletion src/dialect/postgres/postgres-introspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class PostgresIntrospector implements DatabaseIntrospector {
'ns.nspname as schema',
'typ.typname as type',
'dtns.nspname as type_schema',

sql<string | null>`col_description(a.attrelid, a.attnum)`.as('column_description'),
// Detect if the column is auto incrementing by finding the sequence
// that is created for `serial` and `bigserial` columns.
this.#db
Expand Down Expand Up @@ -126,6 +126,7 @@ export class PostgresIntrospector implements DatabaseIntrospector {
isNullable: !it.not_null,
isAutoIncrementing: !!it.auto_incrementing,
hasDefaultValue: it.has_default,
comment: it.column_description ?? undefined
})
)

Expand All @@ -148,4 +149,5 @@ interface RawColumnMetadata {
type: string
type_schema: string
auto_incrementing: boolean | null
column_description: string | null
}
1 change: 1 addition & 0 deletions src/dialect/sqlite/sqlite-introspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class SqliteIntrospector implements DatabaseIntrospector {
isNullable: !col.notnull,
isAutoIncrementing: col.name === autoIncrementCol,
hasDefaultValue: col.dflt_value != null,
comment: undefined
})),
}
}
Expand Down
Loading

0 comments on commit b2dd8a3

Please sign in to comment.