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

Commit

Permalink
fix: apostrophe in Postgres enum strings breaks query (typeorm#4631)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
duckies authored and pleerock committed Sep 5, 2019
1 parent c1406bb commit 445c740
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/driver/mysql/MysqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,7 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
c += " UNSIGNED";
}
if (column.enum)
c += ` (${column.enum.map(value => "'" + value + "'").join(", ")})`;
c += ` (${column.enum.map(value => "'" + value.replace("'", "''") + "'").join(", ")})`;
if (column.charset)
c += ` CHARACTER SET "${column.charset}"`;
if (column.collation)
Expand Down
2 changes: 1 addition & 1 deletion src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1838,7 +1838,7 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
protected createEnumTypeSql(table: Table, column: TableColumn, enumName?: string): Query {
if (!enumName)
enumName = this.buildEnumName(table, column);
const enumValues = column.enum!.map(value => `'${value}'`).join(", ");
const enumValues = column.enum!.map(value => `'${value.replace("'", "''")}'`).join(", ");
return new Query(`CREATE TYPE ${enumName} AS ENUM(${enumValues})`);
}

Expand Down
15 changes: 15 additions & 0 deletions test/github-issues/4630/entity/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Entity, Column, PrimaryGeneratedColumn } from "../../../../src";

export enum Realm {
Blackrock = "Blackrock",
KelThuzad = "Kel'Thuzad",
}

@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;

@Column({ type: "enum", enum: Realm })
realm: Realm;
}
32 changes: 32 additions & 0 deletions test/github-issues/4630/issue-4630.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import "reflect-metadata";
import {createTestingConnections, closeTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
import {Connection} from "../../../src/connection/Connection";
import { Realm } from "./entity/User";
import {User} from "./entity/User";

describe("github issues > #4630 Enum string not escaping resulting in broken migrations.", () => {

let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
enabledDrivers: ["mysql", "postgres"]
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should support enums of strings with apostrophes in them", () => Promise.all(connections.map(async connection => {
const user = new User();
user.realm = Realm.KelThuzad;

await connection.manager.save(user);

const users = await connection.manager.find(User);

users.should.eql([{
id: 1,
realm: "Kel'Thuzad"
}]);
})));
});

0 comments on commit 445c740

Please sign in to comment.