-
-
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: add missing support for primaryKeyConstraintName property in Ent…
…itySchema adds missing support for primaryKeyConstraintName property in EntitySchema definition; adds tests for another custom constraint names in EntitySchema definition;
- Loading branch information
1 parent
e3aac27
commit cc63961
Showing
14 changed files
with
771 additions
and
0 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
49 changes: 49 additions & 0 deletions
49
test/functional/entity-schema/custom-constraint-names/foreign-key/entity/Animal.ts
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,49 @@ | ||
import { EntitySchema } from "../../../../../../src" | ||
|
||
export const AnimalSchema = new EntitySchema<any>({ | ||
name: "animal", | ||
columns: { | ||
id: { | ||
primary: true, | ||
type: Number, | ||
generated: "increment", | ||
}, | ||
}, | ||
relations: { | ||
categories: { | ||
type: "many-to-many", | ||
target: "category", | ||
joinTable: { | ||
name: "animal_category", | ||
joinColumn: { | ||
name: "categoryId", | ||
referencedColumnName: "id", | ||
foreignKeyConstraintName: "fk_animal_category_categoryId", | ||
}, | ||
inverseJoinColumn: { | ||
name: "animalId", | ||
referencedColumnName: "id", | ||
foreignKeyConstraintName: "fk_animal_category_animalId", | ||
}, | ||
}, | ||
}, | ||
breed: { | ||
type: "many-to-one", | ||
target: "breed", | ||
joinColumn: { | ||
name: "breedId", | ||
referencedColumnName: "id", | ||
foreignKeyConstraintName: "fk_animal_breedId", | ||
}, | ||
}, | ||
name: { | ||
type: "one-to-one", | ||
target: "name", | ||
joinColumn: { | ||
name: "nameId", | ||
referencedColumnName: "id", | ||
foreignKeyConstraintName: "fk_animal_nameId", | ||
}, | ||
}, | ||
}, | ||
}) |
12 changes: 12 additions & 0 deletions
12
test/functional/entity-schema/custom-constraint-names/foreign-key/entity/Breed.ts
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,12 @@ | ||
import { EntitySchema } from "../../../../../../src" | ||
|
||
export const BreedSchema = new EntitySchema<any>({ | ||
name: "breed", | ||
columns: { | ||
id: { | ||
primary: true, | ||
type: Number, | ||
generated: "increment", | ||
}, | ||
}, | ||
}) |
12 changes: 12 additions & 0 deletions
12
test/functional/entity-schema/custom-constraint-names/foreign-key/entity/Category.ts
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,12 @@ | ||
import { EntitySchema } from "../../../../../../src" | ||
|
||
export const CategorySchema = new EntitySchema<any>({ | ||
name: "category", | ||
columns: { | ||
id: { | ||
primary: true, | ||
type: Number, | ||
generated: "increment", | ||
}, | ||
}, | ||
}) |
12 changes: 12 additions & 0 deletions
12
test/functional/entity-schema/custom-constraint-names/foreign-key/entity/Name.ts
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,12 @@ | ||
import { EntitySchema } from "../../../../../../src" | ||
|
||
export const NameSchema = new EntitySchema<any>({ | ||
name: "name", | ||
columns: { | ||
id: { | ||
primary: true, | ||
type: Number, | ||
generated: "increment", | ||
}, | ||
}, | ||
}) |
208 changes: 208 additions & 0 deletions
208
test/functional/entity-schema/custom-constraint-names/foreign-key/foreign-key.ts
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,208 @@ | ||
import "reflect-metadata" | ||
import { expect } from "chai" | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../../../utils/test-utils" | ||
import { DataSource } from "../../../../../src" | ||
import { AnimalSchema } from "./entity/Animal" | ||
|
||
describe("entity schema > custom constraint names > foreign key", () => { | ||
let dataSources: DataSource[] | ||
|
||
before( | ||
async () => | ||
(dataSources = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
})), | ||
) | ||
beforeEach(() => reloadTestingDatabases(dataSources)) | ||
after(() => closeTestingConnections(dataSources)) | ||
|
||
it("should set custom constraint names", () => | ||
Promise.all( | ||
dataSources.map(async (dataSource) => { | ||
let metadata = dataSource.getMetadata(AnimalSchema) | ||
|
||
// check ManyToMany constraints | ||
const joinTable = metadata.ownRelations[0] | ||
const mtmFk1 = joinTable.foreignKeys.find( | ||
(fk) => fk.name === "fk_animal_category_categoryId", | ||
) | ||
const mtmFk2 = joinTable.foreignKeys.find( | ||
(fk) => fk.name === "fk_animal_category_animalId", | ||
) | ||
|
||
expect(mtmFk1).to.exist | ||
expect(mtmFk2).to.exist | ||
|
||
// check ManyToOne constraint | ||
const mtoFk = metadata.foreignKeys.find( | ||
(fk) => fk.name === "fk_animal_breedId", | ||
) | ||
expect(mtoFk).to.exist | ||
|
||
// check OneToOne constraint | ||
const otoFk = metadata.foreignKeys.find( | ||
(fk) => fk.name === "fk_animal_nameId", | ||
) | ||
expect(otoFk).to.exist | ||
}), | ||
)) | ||
|
||
it("should load constraints with custom names", () => | ||
Promise.all( | ||
dataSources.map(async (dataSource) => { | ||
const queryRunner = dataSource.createQueryRunner() | ||
const table = await queryRunner.getTable("animal") | ||
const joinTable = await queryRunner.getTable("animal_category") | ||
await queryRunner.release() | ||
|
||
// check ManyToMany constraints | ||
const mtmFk1 = joinTable!.foreignKeys.find( | ||
(fk) => fk.name === "fk_animal_category_categoryId", | ||
) | ||
const mtmFk2 = joinTable!.foreignKeys.find( | ||
(fk) => fk.name === "fk_animal_category_animalId", | ||
) | ||
|
||
expect(mtmFk1).to.exist | ||
expect(mtmFk2).to.exist | ||
|
||
// check ManyToOne constraint | ||
const mtoFk = table!.foreignKeys.find( | ||
(fk) => fk.name === "fk_animal_breedId", | ||
) | ||
expect(mtoFk).to.exist | ||
|
||
// check OneToOne constraint | ||
const otoFk = table!.foreignKeys.find( | ||
(fk) => fk.name === "fk_animal_nameId", | ||
) | ||
expect(otoFk).to.exist | ||
}), | ||
)) | ||
|
||
it("should not change constraint names when table renamed", () => | ||
Promise.all( | ||
dataSources.map(async (dataSource) => { | ||
const queryRunner = dataSource.createQueryRunner() | ||
await queryRunner.renameTable("animal", "animal_renamed") | ||
await queryRunner.renameTable( | ||
"animal_category", | ||
"animal_category_renamed", | ||
) | ||
|
||
const table = await queryRunner.getTable("animal_renamed") | ||
const joinTable = await queryRunner.getTable( | ||
"animal_category_renamed", | ||
) | ||
|
||
await queryRunner.release() | ||
|
||
// check ManyToMany constraints | ||
const mtmFk1 = joinTable!.foreignKeys.find( | ||
(fk) => fk.name === "fk_animal_category_categoryId", | ||
) | ||
const mtmFk2 = joinTable!.foreignKeys.find( | ||
(fk) => fk.name === "fk_animal_category_animalId", | ||
) | ||
|
||
expect(mtmFk1).to.exist | ||
expect(mtmFk2).to.exist | ||
|
||
// check ManyToOne constraint | ||
const mtoFk = table!.foreignKeys.find( | ||
(fk) => fk.name === "fk_animal_breedId", | ||
) | ||
expect(mtoFk).to.exist | ||
|
||
// check OneToOne constraint | ||
const otoFk = table!.foreignKeys.find( | ||
(fk) => fk.name === "fk_animal_nameId", | ||
) | ||
expect(otoFk).to.exist | ||
}), | ||
)) | ||
|
||
it("should not change constraint names when column renamed", () => | ||
Promise.all( | ||
dataSources.map(async (dataSource) => { | ||
// in SqlServer we can't change column that is used in FK. | ||
if (dataSource.driver.options.type === "mssql") return | ||
|
||
const queryRunner = dataSource.createQueryRunner() | ||
|
||
let table = await queryRunner.getTable("animal") | ||
|
||
const breedIdColumn = table!.findColumnByName("breedId")! | ||
const changedBreedIdColumn = breedIdColumn.clone() | ||
changedBreedIdColumn.name = "breedId_renamed" | ||
|
||
const nameIdColumn = table!.findColumnByName("nameId")! | ||
const changedNameIdColumn = nameIdColumn.clone() | ||
changedNameIdColumn.name = "nameId_renamed" | ||
|
||
await queryRunner.changeColumns(table!, [ | ||
{ | ||
oldColumn: breedIdColumn, | ||
newColumn: changedBreedIdColumn, | ||
}, | ||
{ | ||
oldColumn: nameIdColumn, | ||
newColumn: changedNameIdColumn, | ||
}, | ||
]) | ||
|
||
let joinTable = await queryRunner.getTable("animal_category") | ||
const categoryIdColumn = | ||
joinTable!.findColumnByName("categoryId")! | ||
const changedCategoryIdColumn = categoryIdColumn.clone() | ||
changedCategoryIdColumn.name = "categoryId_renamed" | ||
|
||
const animalIdColumn = joinTable!.findColumnByName("animalId")! | ||
const changedAnimalIdColumn = animalIdColumn.clone() | ||
changedAnimalIdColumn.name = "animalId_renamed" | ||
|
||
await queryRunner.changeColumns(joinTable!, [ | ||
{ | ||
oldColumn: categoryIdColumn, | ||
newColumn: changedCategoryIdColumn, | ||
}, | ||
{ | ||
oldColumn: animalIdColumn, | ||
newColumn: changedAnimalIdColumn, | ||
}, | ||
]) | ||
|
||
table = await queryRunner.getTable("animal") | ||
joinTable = await queryRunner.getTable("animal_category") | ||
|
||
await queryRunner.release() | ||
|
||
// check ManyToMany constraints | ||
const mtmFk1 = joinTable!.foreignKeys.find( | ||
(fk) => fk.name === "fk_animal_category_categoryId", | ||
) | ||
const mtmFk2 = joinTable!.foreignKeys.find( | ||
(fk) => fk.name === "fk_animal_category_animalId", | ||
) | ||
|
||
expect(mtmFk1).to.exist | ||
expect(mtmFk2).to.exist | ||
|
||
// check ManyToOne constraint | ||
const mtoFk = table!.foreignKeys.find( | ||
(fk) => fk.name === "fk_animal_breedId", | ||
) | ||
expect(mtoFk).to.exist | ||
|
||
// check OneToOne constraint | ||
const otoFk = table!.foreignKeys.find( | ||
(fk) => fk.name === "fk_animal_nameId", | ||
) | ||
expect(otoFk).to.exist | ||
}), | ||
)) | ||
}) |
28 changes: 28 additions & 0 deletions
28
test/functional/entity-schema/custom-constraint-names/index/entity/Post.ts
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,28 @@ | ||
import { EntitySchema } from "../../../../../../src" | ||
|
||
export const PostSchema = new EntitySchema<any>({ | ||
name: "post", | ||
columns: { | ||
id: { | ||
primary: true, | ||
type: Number, | ||
generated: "increment", | ||
}, | ||
name: { | ||
type: String, | ||
}, | ||
header: { | ||
type: String, | ||
}, | ||
}, | ||
indices: [ | ||
{ | ||
name: "IDX_NAME", | ||
columns: ["name"], | ||
}, | ||
{ | ||
name: "IDX_HEADER", | ||
columns: ["header"], | ||
}, | ||
], | ||
}) |
Oops, something went wrong.