fix: proper default value on generating migration when default value is a function calling [Postgres] (#9830)

Co-authored-by: Dmytro Boiko <dmitriy.b@tracktica.com>
This commit is contained in:
dmytroboiko 2023-04-06 09:49:25 +03:00 committed by GitHub
parent f7f6817864
commit bebba05388
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 1 deletions

View File

@ -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+)$/,

View File

@ -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
}

View File

@ -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'))",
)
}),
))
})