Skip to content

Commit

Permalink
fix: handle enums with multiple apostrophes in MySQL (#8013)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ianngiaw committed Aug 11, 2021
1 parent fbd1ef7 commit 37c40a6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/driver/mysql/MysqlQueryRunner.ts
Expand Up @@ -1879,7 +1879,7 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
c += " UNSIGNED";
}
if (column.enum)
c += ` (${column.enum.map(value => "'" + value.replace("'", "''") + "'").join(", ")})`;
c += ` (${column.enum.map(value => "'" + value.replace(/'/g, "''") + "'").join(", ")})`;
if (column.charset)
c += ` CHARACTER SET "${column.charset}"`;
if (column.collation)
Expand Down
15 changes: 15 additions & 0 deletions test/github-issues/8011/entity/Example.ts
@@ -0,0 +1,15 @@
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src";

enum Category {
MensAndWomensClothing = "Men's and Women's Clothing",
Footwear = "Footwear",
}

@Entity()
export class Example {
@PrimaryGeneratedColumn("increment")
id: number;

@Column({ type: "enum", enum: Category })
category: Category;
}
40 changes: 40 additions & 0 deletions test/github-issues/8011/issue-8011.ts
@@ -0,0 +1,40 @@
import "reflect-metadata";
import { expect } from "chai";
import { Connection } from "../../../src";
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils";
import { Example } from "./entity/Example";

describe("github issues > #8011 Enum values with multiple apostrophes not properly escaped in MySQL", () => {
let connections: Connection[];

before(async () => {
connections = await createTestingConnections({
enabledDrivers: ["mysql"],
entities: [Example],
schemaCreate: true,
dropSchema: true,
});
});
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should properly escape all apostrophes", () =>
Promise.all(
connections.map(async (connection) => {
await connection.driver.createSchemaBuilder().build();
const sqlInMemory = await connection.driver
.createSchemaBuilder()
.log();
expect(sqlInMemory.upQueries.length).to.be.greaterThan(0);
expect(
sqlInMemory.upQueries.some(({ query }) =>
query.includes("Men''s and Women''s Clothing")
)
).to.be.true;
})
));
});

0 comments on commit 37c40a6

Please sign in to comment.