-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: correctly strip type conversion in postgres for default values (#…
…7681) * fix: correctly strip type conversion in postgres for default values Use better regex to remove only type conversion, not the whole string to the end Closes: 7110 * fix: better regex * fix: modify postgres default regex to not break #1532 * fix: modify postgres default regex to support user-defined types This makes sure updated regex does not break migrations chnages introduced in #7647 * test: add test case for #5132
- Loading branch information
Showing
5 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from "../../../../src"; | ||
|
||
@Entity("foo") | ||
export class Foo extends BaseEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({ default: 1, type: "int" }) | ||
bar_default_1: number; | ||
|
||
@Column({ default: -1, type: "int" }) | ||
bar_default_minus_1: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import "reflect-metadata"; | ||
import { Connection } from "../../../src"; | ||
|
||
import { createTestingConnections, closeTestingConnections } from "../../utils/test-utils"; | ||
|
||
import { Foo } from "./entity/foo.entity"; | ||
|
||
describe("github issues > #5132 postgres: Default of -1 (minus 1) generates useless migrations`", () => { | ||
describe("-1 (minus 1) in default value", () => { | ||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
schemaCreate: false, | ||
dropSchema: true, | ||
entities: [Foo], | ||
})); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("can recognize model changes", () => Promise.all(connections.map(async connection => { | ||
if (connection.driver.options.type !== "postgres") { | ||
return; | ||
} | ||
const sqlInMemory = await connection.driver.createSchemaBuilder().log(); | ||
|
||
sqlInMemory.upQueries.length.should.be.greaterThan(0); | ||
sqlInMemory.downQueries.length.should.be.greaterThan(0); | ||
}))); | ||
|
||
it("does not generate when no model changes", () => Promise.all(connections.map(async connection => { | ||
if (connection.driver.options.type !== "postgres") { | ||
return; | ||
} | ||
await connection.driver.createSchemaBuilder().build(); | ||
|
||
const sqlInMemory = await connection.driver.createSchemaBuilder().log(); | ||
|
||
sqlInMemory.upQueries.length.should.be.equal(0); | ||
sqlInMemory.downQueries.length.should.be.equal(0); | ||
}))); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {BaseEntity, Column, Entity, PrimaryGeneratedColumn} from "../../../../src"; | ||
|
||
@Entity("foo_test") | ||
export class Foo extends BaseEntity { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({ | ||
nullable: false, | ||
type: "varchar", | ||
default: () => "TO_CHAR(100, 'FMU000')", | ||
}) | ||
displayId: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import "reflect-metadata"; | ||
import { Connection } from "../../../src"; | ||
|
||
import { createTestingConnections, closeTestingConnections } from "../../utils/test-utils"; | ||
|
||
import { Foo } from "./entity/foo.entity"; | ||
|
||
describe("github issues > #7110 postgres: Typeorm Migrations ignore existing default value on column`", () => { | ||
describe("double type conversion in default value", () => { | ||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
schemaCreate: false, | ||
dropSchema: true, | ||
entities: [Foo], | ||
})); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("can recognize model changes", () => Promise.all(connections.map(async connection => { | ||
if (connection.driver.options.type !== "postgres") { | ||
return; | ||
} | ||
const sqlInMemory = await connection.driver.createSchemaBuilder().log(); | ||
sqlInMemory.upQueries.length.should.be.greaterThan(0); | ||
sqlInMemory.downQueries.length.should.be.greaterThan(0); | ||
}))); | ||
|
||
it("does not generate when no model changes", () => Promise.all(connections.map(async connection => { | ||
if (connection.driver.options.type !== "postgres") { | ||
return; | ||
} | ||
await connection.driver.createSchemaBuilder().build(); | ||
|
||
const sqlInMemory = await connection.driver.createSchemaBuilder().log(); | ||
|
||
sqlInMemory.upQueries.length.should.be.equal(0); | ||
sqlInMemory.downQueries.length.should.be.equal(0); | ||
}))); | ||
}); | ||
}); |