diff --git a/src/driver/postgres/PostgresQueryRunner.ts b/src/driver/postgres/PostgresQueryRunner.ts index 551084635..3fa2d3ddb 100644 --- a/src/driver/postgres/PostgresQueryRunner.ts +++ b/src/driver/postgres/PostgresQueryRunner.ts @@ -3717,7 +3717,7 @@ export class PostgresQueryRunner } else { tableColumn.default = dbColumn[ "column_default" - ].replace(/::.+/g, "") + ].replace(/::[\w\s.\[\]\-"]+/g, "") tableColumn.default = tableColumn.default.replace( /^(-?\d+)$/, diff --git a/test/github-issues/9829/entity/ExampleEntity.ts b/test/github-issues/9829/entity/ExampleEntity.ts new file mode 100644 index 000000000..6151e2e4b --- /dev/null +++ b/test/github-issues/9829/entity/ExampleEntity.ts @@ -0,0 +1,17 @@ +import { Entity } from "../../../../src/decorator/entity/Entity" +import { Column } from "../../../../src/decorator/columns/Column" +import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn" + +@Entity() +export class ExampleEntity { + @PrimaryGeneratedColumn() + id: number + + @Column({ + type: "varchar", + length: 3, + unique: true, + default: () => "('AA'|| COALESCE(NULL, '1'))", + }) + someValue: string +} diff --git a/test/github-issues/9829/issue-9829.ts b/test/github-issues/9829/issue-9829.ts new file mode 100644 index 000000000..5415e7ff8 --- /dev/null +++ b/test/github-issues/9829/issue-9829.ts @@ -0,0 +1,33 @@ +import "reflect-metadata" +import { DataSource } from "../../../src/index" +import { + closeTestingConnections, + createTestingConnections, +} from "../../utils/test-utils" +import { ExampleEntity } from "./entity/ExampleEntity" + +describe("github issues > #9829 Incorrect default value with concat value of function", () => { + let connections: DataSource[] + before( + async () => + (connections = await createTestingConnections({ + entities: [ExampleEntity], + schemaCreate: true, + dropSchema: true, + enabledDrivers: ["postgres"], + })), + ) + after(() => closeTestingConnections(connections)) + it("should get default concat value", () => + Promise.all( + connections.map(async (connection) => { + const queryRunner = connection.createQueryRunner() + let table = await queryRunner.getTable("example_entity") + + const nameColumn = table!.findColumnByName("someValue")! + nameColumn!.default!.should.be.equal( + "('AA'|| COALESCE(NULL, '1'))", + ) + }), + )) +})