Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Postgres enum generates unnecessary queries on schema sync (#8268)
Co-authored-by: Matthias Helmrich <helmsonsen@gmai.com>
- Loading branch information
1 parent
1f558b1
commit 98d5f39
Showing
3 changed files
with
67 additions
and
1 deletion.
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,26 @@ | ||
import {Column, PrimaryGeneratedColumn} from "../../../../src"; | ||
import {Entity} from "../../../../src"; | ||
|
||
export enum CreationMechanism { | ||
SOURCE_A = 'SOURCE_A', | ||
SOURCE_B = 'SOURCE_B', | ||
SOURCE_C = 'SOURCE_C', | ||
SOURCE_D = 'SOURCE_D' | ||
} | ||
|
||
@Entity({ name: 'some_entity', schema: 'some_schema', }) | ||
export class SomeEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({ | ||
type : 'enum', | ||
enumName : 'creation_mechanism_enum', | ||
enum : CreationMechanism, | ||
default : CreationMechanism.SOURCE_A, | ||
}) | ||
creationMechanism: CreationMechanism; | ||
|
||
@Column({ nullable: false, default: () => 'now()' }) | ||
createdAt: Date; | ||
} |
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 {SomeEntity, CreationMechanism} from "./entity/SomeEntity"; | ||
|
||
describe("github issues > #3076 Postgres enum in schema with default is recreated in every new generated migration", () => { | ||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
migrations: [], | ||
enabledDrivers: ["postgres"], | ||
schemaCreate: false, | ||
dropSchema: true, | ||
entities: [SomeEntity], | ||
})); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should recognize model changes", () => Promise.all(connections.map(async connection => { | ||
const sqlInMemory = await connection.driver.createSchemaBuilder().log(); | ||
sqlInMemory.upQueries.length.should.be.greaterThan(0); | ||
sqlInMemory.downQueries.length.should.be.greaterThan(0); | ||
}))); | ||
|
||
it("should not generate queries when no model changes", () => Promise.all(connections.map(async connection => { | ||
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); | ||
}))); | ||
|
||
it("should handle `enumName` default change", () => Promise.all(connections.map(async connection => { | ||
const entityMetadata = connection.getMetadata(SomeEntity); | ||
const columnMetadata = entityMetadata.columns.find(column => column.databaseName === "creationMechanism") | ||
columnMetadata!.default = CreationMechanism.SOURCE_B; | ||
|
||
const sqlInMemory = await connection.driver.createSchemaBuilder().log(); | ||
sqlInMemory.upQueries.length.should.be.greaterThan(0); | ||
sqlInMemory.downQueries.length.should.be.greaterThan(0); | ||
}))); | ||
}); |