Skip to content

Commit

Permalink
fix: Incorrect enum default value when table name contains dash chara…
Browse files Browse the repository at this point in the history
…cter (#9685)

* fix: get enum default value

* chore: format
  • Loading branch information
KhaledSMQ committed Feb 7, 2023
1 parent 7c00bb8 commit b3b0c11
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3717,7 +3717,7 @@ export class PostgresQueryRunner
} else {
tableColumn.default = dbColumn[
"column_default"
].replace(/::[\w\s\.\[\]\"]+/g, "")
].replace(/::.+/g, "")
tableColumn.default =
tableColumn.default.replace(
/^(-?\d+)$/,
Expand Down
15 changes: 15 additions & 0 deletions test/github-issues/9684/entity/Foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"

export enum EnumStatus {
DRAFT = "draft",
PUBLISHED = "published",
}

@Entity("module-foo_table_x")
export class Foo {
@PrimaryGeneratedColumn({ name: "id" })
id: number

@Column({ type: "enum", enum: EnumStatus, default: EnumStatus.DRAFT })
enumStatus: Date
}
31 changes: 31 additions & 0 deletions test/github-issues/9684/issue-9684.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import "reflect-metadata"
import { DataSource } from "../../../src/index"
import {
closeTestingConnections,
createTestingConnections,
} from "../../utils/test-utils"
import { Foo } from "./entity/Foo"

describe("github issues > #9684 Incorrect enum default value when table name contains dash character", () => {
let connections: DataSource[]
before(
async () =>
(connections = await createTestingConnections({
entities: [Foo],
schemaCreate: true,
dropSchema: true,
enabledDrivers: ["postgres"],
})),
)
after(() => closeTestingConnections(connections))
it("should get default enum value", () =>
Promise.all(
connections.map(async (connection) => {
const queryRunner = connection.createQueryRunner()
let table = await queryRunner.getTable("module-foo_table_x")

const nameColumn = table!.findColumnByName("enumStatus")!
nameColumn!.default!.should.be.equal("'draft'")
}),
))
})

0 comments on commit b3b0c11

Please sign in to comment.