Skip to content

Commit

Permalink
Retrieve charset for tables in MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
joselcvarela committed May 24, 2023
1 parent c50b30d commit 718ddcc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/dialects/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type RawTable = {
TABLE_COMMENT: string | null;
ENGINE: string;
TABLE_COLLATION: string;
TABLE_CHARSET: string;
};

type RawColumn = {
Expand Down Expand Up @@ -104,9 +105,15 @@ export default class MySQL implements SchemaInspector {
'ENGINE',
'TABLE_SCHEMA',
'TABLE_COLLATION',
'TABLE_COMMENT'
'TABLE_COMMENT',
'CHARACTER_SET_NAME as TABLE_CHARSET'
)
.from('information_schema.tables')
.leftJoin(
'information_schema.collation_character_set_applicability',
'tables.table_collation',
'collation_character_set_applicability.collation_name'
)
.where({
table_schema: this.knex.client.database(),
table_type: 'BASE TABLE',
Expand All @@ -122,6 +129,7 @@ export default class MySQL implements SchemaInspector {
schema: rawTable.TABLE_SCHEMA,
comment: rawTable.TABLE_COMMENT,
collation: rawTable.TABLE_COLLATION,
charset: rawTable.TABLE_CHARSET,
engine: rawTable.ENGINE,
} as T extends string ? Table : Table[];
}
Expand All @@ -134,6 +142,7 @@ export default class MySQL implements SchemaInspector {
schema: rawTable.TABLE_SCHEMA,
comment: rawTable.TABLE_COMMENT,
collation: rawTable.TABLE_COLLATION,
charset: rawTable.TABLE_CHARSET,
engine: rawTable.ENGINE,
};
}) as T extends string ? Table : Table[];
Expand Down
1 change: 1 addition & 0 deletions lib/types/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface Table {

// MySQL Only
collation?: string;
charset?: string;
engine?: string;

// Postgres Only
Expand Down
4 changes: 4 additions & 0 deletions test/mysql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,31 @@ describe('mysql', () => {
schema: 'test_db',
comment: '',
collation: 'latin1_swedish_ci',
charset: 'latin1',
engine: 'InnoDB',
},
{
name: 'teams',
schema: 'test_db',
comment: '',
collation: 'latin1_swedish_ci',
charset: 'latin1',
engine: 'InnoDB',
},
{
name: 'users',
schema: 'test_db',
comment: '',
collation: 'latin1_swedish_ci',
charset: 'latin1',
engine: 'InnoDB',
},
]);
});

it('returns information for specific table', async () => {
expect(await inspector.tableInfo('teams')).to.deep.equal({
charset: 'latin1',
collation: 'latin1_swedish_ci',
comment: '',
engine: 'InnoDB',
Expand Down

0 comments on commit 718ddcc

Please sign in to comment.