Skip to content

Commit

Permalink
fix: allow special keyword as column name for simple-enum type on sql…
Browse files Browse the repository at this point in the history
…ite (#8645)

* fix: Allow special keyword as column name for simple-enum type on sqlite

* fix: Fix tests

* fix: Fix tests
  • Loading branch information
metiftikci committed Feb 16, 2022
1 parent b93416d commit 93bf96e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/driver/sqlite-abstract/AbstractSqliteQueryRunner.ts
Expand Up @@ -859,7 +859,7 @@ export abstract class AbstractSqliteQueryRunner extends BaseQueryRunner implemen

if (tableColumn.type === "varchar") {
// Check if this is an enum
const enumMatch = sql.match(new RegExp("\"(" + tableColumn.name + ")\" varchar CHECK\\s*\\(\\s*\\1\\s+IN\\s*\\(('[^']+'(?:\\s*,\\s*'[^']+')+)\\s*\\)\\s*\\)"));
const enumMatch = sql.match(new RegExp("\"(" + tableColumn.name + ")\" varchar CHECK\\s*\\(\\s*\"\\1\"\\s+IN\\s*\\(('[^']+'(?:\\s*,\\s*'[^']+')+)\\s*\\)\\s*\\)"));
if (enumMatch) {
// This is an enum
tableColumn.enum = enumMatch[2].substr(1, enumMatch[2].length - 2).split("','");
Expand Down Expand Up @@ -1147,7 +1147,7 @@ export abstract class AbstractSqliteQueryRunner extends BaseQueryRunner implemen
}

if (column.enum)
c += " CHECK( " + column.name + " IN (" + column.enum.map(val => "'" + val + "'").join(",") + ") )";
c += " CHECK( \"" + column.name + "\" IN (" + column.enum.map(val => "'" + val + "'").join(",") + ") )";
if (column.isPrimary && !skipPrimary)
c += " PRIMARY KEY";
if (column.isGenerated === true && column.generationStrategy === "increment") // don't use skipPrimary here since updates can update already exist primary without auto inc.
Expand Down
16 changes: 16 additions & 0 deletions test/github-issues/8644/entity/my-table.entity.ts
@@ -0,0 +1,16 @@
import {Column, PrimaryGeneratedColumn} from "../../../../src";
import {Entity} from "../../../../src/decorator/entity/Entity";

export enum Limit {
Foo = "foo",
Bar = "bar",
}

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

@Column({ type: "simple-enum", enum: Limit })
limit: Limit;
}
32 changes: 32 additions & 0 deletions test/github-issues/8644/issue-8644.ts
@@ -0,0 +1,32 @@
import { Connection } from "../../../src";
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils";
import { Limit, MyTable } from "./entity/my-table.entity";

describe("github issues > #8644 BUG - Special keyword column name for simple-enum in sqlite", () => {
let connections: Connection[];

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

it("it should be able to set special keyword as column name for simple-enum types", () =>
Promise.all(
connections.map(async (connection) => {
const repository = connection.getRepository(MyTable);

await repository.insert({ limit: Limit.Bar });
})
));
});

0 comments on commit 93bf96e

Please sign in to comment.