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

Enum values with multiple apostrophes not properly escaped in MySQL #8011

Closed
ianngiaw opened this issue Aug 3, 2021 · 0 comments · Fixed by #8013
Closed

Enum values with multiple apostrophes not properly escaped in MySQL #8011

ianngiaw opened this issue Aug 3, 2021 · 0 comments · Fixed by #8013

Comments

@ianngiaw
Copy link
Contributor

ianngiaw commented Aug 3, 2021

Issue Description

Expected Behavior

All apostrophes in the enum should be escaped in the generated add column migration.

Actual Behavior

Only the first apostrophe is properly escaped. Subsequent apostrophes are left unescaped, resulting in a QueryFailedError when running the migration.

QueryFailedError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's Clothing', 'Footwear') NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB' at line 1
    at new QueryFailedError (/typeorm-test-proj/src/error/QueryFailedError.ts:9:9)
    at Query.onResult (/typeorm-test-proj/src/driver/mysql/MysqlQueryRunner.ts:193:37)
    at Query.execute (/typeorm-test-proj/node_modules/mysql2/lib/commands/command.js:30:14)
    at PoolConnection.handlePacket (/typeorm-test-proj/node_modules/mysql2/lib/connection.js:425:32)
    at PacketParser.onPacket (/typeorm-test-proj/node_modules/mysql2/lib/connection.js:75:12)
    at PacketParser.executeStart (/typeorm-test-proj/node_modules/mysql2/lib/packet_parser.js:75:16)
    at Socket.<anonymous> (/typeorm-test-proj/node_modules/mysql2/lib/connection.js:82:25)

Steps to Reproduce

  1. Create an enum with multiple apostrophes in a single value
enum Category {
  MensAndWomensClothing = "Men's and Women's Clothing",
  Footwear = 'Footwear',
}
  1. Create an entity that uses the enum in a column.
@Entity()
export class Test {
  @PrimaryGeneratedColumn('increment')
  id: number;

  @Column({ type: 'enum', enum: Category })
  category: Category;
}
  1. Generate migration a migration for the new entity

Output:

import { MigrationInterface, QueryRunner } from 'typeorm';

export class Testing1628004031988 implements MigrationInterface {
  name = 'Testing1628004031988';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      "CREATE TABLE `test` (`id` int NOT NULL AUTO_INCREMENT, `category` enum ('Men''s and Women's Clothing', 'Footwear') NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB",
    ); //                                                                         issue is here ^
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query('DROP TABLE `test`');
  }
}

My Environment

Dependency Version
Operating System MacOS 10.15.7
Node.js version v14.16.0
Typescript version 4.2.4
TypeORM version 0.2.32

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
aurora-data-api no
aurora-data-api-pg no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql yes
nativescript no
oracle no
postgres no
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✅ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
ianngiaw added a commit to ianngiaw/typeorm that referenced this issue Aug 3, 2021
Current implementation uses `"'"` in replace which only replaces first occurrence of `'`.
Changing to `/'/g` will allow for all instances of apostrophes to be replaced.

Closes: typeorm#8011
imnotjames pushed a commit that referenced this issue Aug 11, 2021
Current implementation uses `"'"` in replace which only replaces first occurrence of `'`.
Changing to `/'/g` will allow for all instances of apostrophes to be replaced.

Closes: #8011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants