Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL Introspector fails because of incorrect casing #135

Closed
Devvypaws opened this issue Aug 9, 2022 · 4 comments
Closed

MySQL Introspector fails because of incorrect casing #135

Devvypaws opened this issue Aug 9, 2022 · 4 comments
Labels
bug Something isn't working built-in dialect Related to a built-in dialect mysql Related to MySQL

Comments

@Devvypaws
Copy link
Contributor

Overview

The MySQL Introspector selects columns from information_schema.columns in lowercase, but parseTableMetadata() expects the columns to be uppercase, causing it to throw.

Environment

Node: 18.6.0
Kysely: 0.19.12 and 0.21.1
MySQL: 5.7.33

Research

The result of the query MySQL Introspector uses was inspected and found to output the following:

{
  column_name: 'id',
  column_default: null,
  table_name: 'addresses',
  table_schema: '[REDACTED]',
  is_nullable: 'NO',
  data_type: 'int',
  extra: 'auto_increment'
}

However, parseTableMetadata() expects these fields to be uppercase:

#parseTableMetadata(columns) {
// ...
        let table = tables.find((tbl) => tbl.name === it.TABLE_NAME);
// ...
                name: it.TABLE_NAME,
                schema: it.TABLE_SCHEMA,
// ...
            name: it.COLUMN_NAME,
            dataType: it.DATA_TYPE,
            isNullable: it.IS_NULLABLE === 'YES',
            isAutoIncrementing: it.EXTRA.toLowerCase().includes('auto_increment'),
            hasDefaultValue: it.COLUMN_DEFAULT !== null,
// ...
}

This causes it to throw the following error:

[REDACTED]/node_modules/kysely/dist/cjs/dialect/mysql/mysql-introspector.js:63
                isAutoIncrementing: it.EXTRA.toLowerCase().includes('auto_increment'),
                                             ^

TypeError: Cannot read properties of undefined (reading 'toLowerCase')
    at [REDACTED]/node_modules/kysely/dist/cjs/dialect/mysql/mysql-introspector.js:63:46

It appears the casing of the SELECT statement affects the returned results, as confirmed by both a manual query of the database and also modifying

let query = this.#db
  .selectFrom('information_schema.columns')
  .select([
      'column_name',
      'column_default',
      'table_name',
      'table_schema',
      'is_nullable',
      'data_type',
      'extra',
  ])

to use uppercase, which returns the expected column names and succeeds.

@koskimas
Copy link
Member

koskimas commented Aug 9, 2022

Kysely has tests for this and they all pass.

@koskimas koskimas closed this as completed Aug 9, 2022
@koskimas
Copy link
Member

koskimas commented Aug 9, 2022

Well, your fix seems to also work so I'll merge it.

@koskimas
Copy link
Member

Fixed in 0.21.2. Thank you!

@Devvypaws
Copy link
Contributor Author

Yeah, I was confused as to why it was happening because I expected the returned columns to just match the case they are in the system. But even testing it out myself, I got that same behavior. Nonetheless, the casings matching the expected output couldn't hurt. :-)

@igalklebanov igalklebanov added bug Something isn't working mysql Related to MySQL built-in dialect Related to a built-in dialect labels Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working built-in dialect Related to a built-in dialect mysql Related to MySQL
Projects
None yet
Development

No branches or pull requests

3 participants