fix: incorrect sorting of entities with multi-inheritances (#9406)

* Fixed sorting of entities with multi-inheritances

* prettier

* added test case

---------

Co-authored-by: Alex Messer <dmzt08@gmail.com>
This commit is contained in:
ZBAGI 2023-02-07 11:42:32 +01:00 committed by GitHub
parent adce6985d8
commit 54ca9dd801
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 95 additions and 2 deletions

View File

@ -69,8 +69,9 @@ export class SubjectTopoligicalSorter {
const entityTargetSubjects = this.subjects.filter(
(subject) =>
subject.metadata.targetName === sortedEntityTarget ||
subject.metadata.parentEntityMetadata?.targetName ===
sortedEntityTarget,
subject.metadata.inheritanceTree.some(
(s) => s.name === sortedEntityTarget,
),
)
sortedSubjects.push(...entityTargetSubjects)
this.removeAlreadySorted(entityTargetSubjects)

View File

@ -0,0 +1,29 @@
import {
Column,
Entity,
ManyToOne,
PrimaryGeneratedColumn,
} from "../../../../src/index.js"
import { ChangeLog } from "./ChangeLog.js"
@Entity()
export class Change<T> {
@PrimaryGeneratedColumn("increment")
id: number
@Column("varchar", { nullable: false, length: 255 })
propertyName: string
@Column("json", { nullable: true })
oldValue?: any
@Column("json", { nullable: true })
newValue?: any
@ManyToOne(() => ChangeLog, {
cascade: false,
nullable: false,
onDelete: "CASCADE",
})
public log: ChangeLog<T>
}

View File

@ -0,0 +1,9 @@
import { OneToMany, ChildEntity } from "../../../../src/index.js"
import { Change } from "./Change.js"
import { Log } from "./Log.js"
@ChildEntity()
export abstract class ChangeLog<T> extends Log {
@OneToMany(() => Change, (change) => change.log, { cascade: true })
changes: Change<T>[]
}

View File

@ -0,0 +1,7 @@
import { ChildEntity } from "../../../../src/index.js"
import { ChangeLog } from "./ChangeLog.js"
export class Email {}
@ChildEntity()
export class EmailChanged extends ChangeLog<Email> {}

View File

@ -0,0 +1,12 @@
import {
Entity,
PrimaryGeneratedColumn,
TableInheritance,
} from "../../../../src/index.js"
@Entity()
@TableInheritance({ column: { type: "varchar", name: "type" } })
export abstract class Log {
@PrimaryGeneratedColumn("increment")
id: number
}

View File

@ -0,0 +1,35 @@
import "reflect-metadata"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { DataSource } from "../../../src/index.js"
import { Email, EmailChanged } from "./entity/EmailChanged.js"
import { Change } from "./entity/Change.js"
import { Log } from "./entity/Log.js"
describe("github issues > #9405 Incorrect subject sorting with multi-inheritance entities", () => {
let dataSources: DataSource[]
before(
async () =>
(dataSources = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
enabledDrivers: ["postgres"],
})),
)
beforeEach(() => reloadTestingDatabases(dataSources))
after(() => closeTestingConnections(dataSources))
it("should correctly sort entities with multi-inheritances", () =>
Promise.all(
dataSources.map(async (dataSource) => {
const emailChanged = new EmailChanged()
const change = new Change<Email>()
change.propertyName = "Example"
emailChanged.changes = [change]
await dataSource.getRepository(Log).save(emailChanged)
}),
))
})