Skip to content
This repository was archived by the owner on Mar 18, 2022. It is now read-only.

Commit 445c740

Browse files
duckiespleerock
authored andcommitted
fix: apostrophe in Postgres enum strings breaks query (typeorm#4631)
* Patch to allow apostrophes in postgres enum string. * Make linter happy * Testing fix for MySQL * Limit to postgres and mysql, fix test description * Lets not be greedy.
1 parent c1406bb commit 445c740

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

src/driver/mysql/MysqlQueryRunner.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,7 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
16361636
c += " UNSIGNED";
16371637
}
16381638
if (column.enum)
1639-
c += ` (${column.enum.map(value => "'" + value + "'").join(", ")})`;
1639+
c += ` (${column.enum.map(value => "'" + value.replace("'", "''") + "'").join(", ")})`;
16401640
if (column.charset)
16411641
c += ` CHARACTER SET "${column.charset}"`;
16421642
if (column.collation)

src/driver/postgres/PostgresQueryRunner.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1838,7 +1838,7 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
18381838
protected createEnumTypeSql(table: Table, column: TableColumn, enumName?: string): Query {
18391839
if (!enumName)
18401840
enumName = this.buildEnumName(table, column);
1841-
const enumValues = column.enum!.map(value => `'${value}'`).join(", ");
1841+
const enumValues = column.enum!.map(value => `'${value.replace("'", "''")}'`).join(", ");
18421842
return new Query(`CREATE TYPE ${enumName} AS ENUM(${enumValues})`);
18431843
}
18441844

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Entity, Column, PrimaryGeneratedColumn } from "../../../../src";
2+
3+
export enum Realm {
4+
Blackrock = "Blackrock",
5+
KelThuzad = "Kel'Thuzad",
6+
}
7+
8+
@Entity()
9+
export class User {
10+
@PrimaryGeneratedColumn()
11+
id: number;
12+
13+
@Column({ type: "enum", enum: Realm })
14+
realm: Realm;
15+
}

test/github-issues/4630/issue-4630.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import "reflect-metadata";
2+
import {createTestingConnections, closeTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
3+
import {Connection} from "../../../src/connection/Connection";
4+
import { Realm } from "./entity/User";
5+
import {User} from "./entity/User";
6+
7+
describe("github issues > #4630 Enum string not escaping resulting in broken migrations.", () => {
8+
9+
let connections: Connection[];
10+
before(async () => connections = await createTestingConnections({
11+
entities: [__dirname + "/entity/*{.js,.ts}"],
12+
schemaCreate: true,
13+
dropSchema: true,
14+
enabledDrivers: ["mysql", "postgres"]
15+
}));
16+
beforeEach(() => reloadTestingDatabases(connections));
17+
after(() => closeTestingConnections(connections));
18+
19+
it("should support enums of strings with apostrophes in them", () => Promise.all(connections.map(async connection => {
20+
const user = new User();
21+
user.realm = Realm.KelThuzad;
22+
23+
await connection.manager.save(user);
24+
25+
const users = await connection.manager.find(User);
26+
27+
users.should.eql([{
28+
id: 1,
29+
realm: "Kel'Thuzad"
30+
}]);
31+
})));
32+
});

0 commit comments

Comments
 (0)