Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): skip STI discriminator condition when bulk-deleting entities
Closes #5303
- Loading branch information
Showing
2 changed files
with
116 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,115 @@ | ||
import { Cascade, Collection, Entity, ManyToOne, MikroORM, OneToMany, PrimaryKey, Property } from '@mikro-orm/sqlite'; | ||
|
||
@Entity() | ||
class User { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@OneToMany({ | ||
entity: () => CoverPicture, | ||
mappedBy: 'cover_user', | ||
orphanRemoval: true, | ||
eager: true, | ||
cascade: [Cascade.ALL], | ||
}) | ||
cover_pictures: Collection<CoverPicture> = new Collection<CoverPicture>(this); | ||
|
||
@OneToMany({ | ||
entity: () => ProfilePicture, | ||
mappedBy: 'profile_user', | ||
orphanRemoval: true, | ||
eager: true, | ||
cascade: [Cascade.ALL], | ||
}) | ||
profile_pictures: Collection<ProfilePicture> = new Collection<ProfilePicture>(this); | ||
|
||
} | ||
|
||
@Entity({ abstract: true, discriminatorColumn: 'type' }) | ||
abstract class BasePicture { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property({ type: 'string', nullable: false }) | ||
path: string; | ||
|
||
constructor(path: string) { | ||
this.path = path; | ||
} | ||
|
||
} | ||
|
||
@Entity({ discriminatorValue: 'cover' }) | ||
class CoverPicture extends BasePicture { | ||
|
||
@ManyToOne({ entity: () => User }) | ||
cover_user: User; | ||
|
||
constructor(path: string, user: User) { | ||
super(path); | ||
this.cover_user = user; | ||
} | ||
|
||
} | ||
|
||
@Entity({ discriminatorValue: 'profile' }) | ||
class ProfilePicture extends BasePicture { | ||
|
||
@ManyToOne({ entity: () => User }) | ||
profile_user: User; | ||
|
||
constructor(path: string, user: User) { | ||
super(path); | ||
this.profile_user = user; | ||
} | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
dbName: ':memory:', | ||
entities: [User, BasePicture, CoverPicture, ProfilePicture], | ||
}); | ||
await orm.schema.refreshDatabase(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
test('user should be able to create/update pictures', async () => { | ||
let user = new User(); | ||
orm.em.create(User, user); | ||
await orm.em.persist(user).flush(); | ||
orm.em.clear(); | ||
|
||
user = await orm.em.findOneOrFail(User, { id: user.id }); | ||
user.cover_pictures.add(new CoverPicture('/path/1', user)); | ||
user.profile_pictures.add(new ProfilePicture('/path/2', user)); | ||
await orm.em.persist(user).flush(); | ||
orm.em.clear(); | ||
|
||
user = await orm.em.findOneOrFail(User, { id: user.id }); | ||
expect(user.cover_pictures.length).toBe(1); | ||
expect(user.profile_pictures.length).toBe(1); | ||
|
||
// update pictures | ||
user.cover_pictures.removeAll(); | ||
user.profile_pictures.removeAll(); | ||
|
||
user.cover_pictures.add(new CoverPicture('/path/3', user)); | ||
user.profile_pictures.add(new ProfilePicture('/path/4', user)); | ||
|
||
await orm.em.persist(user).flush(); | ||
orm.em.clear(); | ||
|
||
user = await orm.em.findOneOrFail(User, { id: user.id }); | ||
expect(user.cover_pictures.length).toBe(1); | ||
expect(user.profile_pictures.length).toBe(1); | ||
expect(user.cover_pictures[0].path).toBe('/path/3'); | ||
expect(user.profile_pictures[0].path).toBe('/path/4'); | ||
}); |