-
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): fix conditions in
em.upsertMany
with composite keys
Closes #4786
- Loading branch information
Showing
2 changed files
with
145 additions
and
3 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,132 @@ | ||
import { | ||
Collection, | ||
Entity, | ||
ManyToOne, | ||
OneToMany, | ||
OptionalProps, | ||
PrimaryKey, | ||
Property, | ||
SimpleLogger, | ||
Unique, | ||
} from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/sqlite'; | ||
import { mockLogger } from '../../helpers'; | ||
|
||
@Entity({ abstract: true }) | ||
abstract class ApplicationEntity<Optionals = never> { | ||
|
||
[OptionalProps]?: Optionals | 'createdAt' | 'updatedAt'; | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property({ name: 'created_at', defaultRaw: 'current_timestamp' }) | ||
createdAt!: Date; | ||
|
||
@Property({ name: 'updated_at', defaultRaw: 'current_timestamp', onUpdate: () => new Date() }) | ||
updatedAt!: Date; | ||
|
||
} | ||
|
||
@Entity() | ||
class InternalRole extends ApplicationEntity { | ||
|
||
@Property() | ||
name!: string; | ||
|
||
@OneToMany(() => InternalRolePermission, permission => permission.internalRole, { orphanRemoval: true }) | ||
permissions = new Collection<InternalRolePermission>(this); | ||
|
||
} | ||
|
||
@Entity() | ||
@Unique({ properties: ['subject', 'action', 'internalRole'] }) | ||
class InternalRolePermission extends ApplicationEntity { | ||
|
||
@Property() | ||
subject!: string; | ||
|
||
@Property() | ||
action!: string; | ||
|
||
@ManyToOne(() => InternalRole, { onDelete: 'cascade' }) | ||
internalRole!: InternalRole; | ||
|
||
} | ||
|
||
let orm: MikroORM; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
dbName: ':memory:', | ||
entities: [InternalRole], | ||
loggerFactory: options => new SimpleLogger(options), | ||
}); | ||
await orm.schema.createSchema(); | ||
}); | ||
|
||
beforeAll(async () => { | ||
await orm.schema.clearDatabase(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
}); | ||
|
||
test('4786 (em.upsert)', async () => { | ||
orm.em.create(InternalRole, { id: 1, name: 'role' }); | ||
await orm.em.flush(); | ||
orm.em.clear(); | ||
|
||
const mock = mockLogger(orm); | ||
let role = await orm.em.findOneOrFail(InternalRole, 1); | ||
await orm.em.upsert(InternalRolePermission, { subject: 'User', action: 'read', internalRole: role }); | ||
|
||
expect(mock.mock.calls).toEqual([ | ||
['[query] select `i0`.* from `internal_role` as `i0` where `i0`.`id` = 1 limit 1'], | ||
['[query] insert into `internal_role_permission` (`action`, `internal_role_id`, `subject`) values (\'read\', 1, \'User\') on conflict (`subject`, `action`, `internal_role_id`) do nothing returning `id`, `created_at`, `updated_at`'], | ||
]); | ||
|
||
mock.mockReset(); | ||
orm.em.clear(); | ||
role = await orm.em.findOneOrFail(InternalRole, 1); | ||
await orm.em.upsert(InternalRolePermission, { subject: 'User', action: 'read', internalRole: role }); | ||
|
||
expect(mock.mock.calls).toEqual([ | ||
['[query] select `i0`.* from `internal_role` as `i0` where `i0`.`id` = 1 limit 1'], | ||
['[query] insert into `internal_role_permission` (`action`, `internal_role_id`, `subject`) values (\'read\', 1, \'User\') on conflict (`subject`, `action`, `internal_role_id`) do nothing returning `id`, `created_at`, `updated_at`'], | ||
['[query] select `i0`.`id`, `i0`.`created_at`, `i0`.`updated_at` from `internal_role_permission` as `i0` where `i0`.`subject` = \'User\' and `i0`.`action` = \'read\' and `i0`.`internal_role_id` = 1 limit 1'], | ||
]); | ||
}); | ||
|
||
test('4786 (em.upsertMany)', async () => { | ||
orm.em.create(InternalRole, { id: 1, name: 'role' }); | ||
await orm.em.flush(); | ||
orm.em.clear(); | ||
|
||
const mock = mockLogger(orm); | ||
let role = await orm.em.findOneOrFail(InternalRole, 1); | ||
await orm.em.upsertMany(InternalRolePermission, [ | ||
{ subject: 'User', action: 'read', internalRole: role }, | ||
{ subject: 'User', action: 'update', internalRole: role }, | ||
]); | ||
|
||
expect(mock.mock.calls).toEqual([ | ||
['[query] select `i0`.* from `internal_role` as `i0` where `i0`.`id` = 1 limit 1'], | ||
['[query] insert into `internal_role_permission` (`action`, `internal_role_id`, `subject`) select \'read\' as `action`, 1 as `internal_role_id`, \'User\' as `subject` union all select \'update\' as `action`, 1 as `internal_role_id`, \'User\' as `subject` where true on conflict (`subject`, `action`, `internal_role_id`) do nothing returning `id`, `created_at`, `updated_at`'], | ||
]); | ||
|
||
mock.mockReset(); | ||
orm.em.clear(); | ||
role = await orm.em.findOneOrFail(InternalRole, 1); | ||
await orm.em.upsertMany(InternalRolePermission, [ | ||
{ subject: 'User', action: 'read', internalRole: role }, | ||
{ subject: 'User', action: 'update', internalRole: role }, | ||
]); | ||
|
||
expect(mock.mock.calls).toEqual([ | ||
['[query] select `i0`.* from `internal_role` as `i0` where `i0`.`id` = 1 limit 1'], | ||
['[query] insert into `internal_role_permission` (`action`, `internal_role_id`, `subject`) select \'read\' as `action`, 1 as `internal_role_id`, \'User\' as `subject` union all select \'update\' as `action`, 1 as `internal_role_id`, \'User\' as `subject` where true on conflict (`subject`, `action`, `internal_role_id`) do nothing returning `id`, `created_at`, `updated_at`'], | ||
['[query] select `i0`.`id`, `i0`.`created_at`, `i0`.`updated_at`, `i0`.`subject`, `i0`.`action`, `i0`.`internal_role_id` from `internal_role_permission` as `i0` where ((`i0`.`subject` = \'User\' and `i0`.`action` = \'read\' and `i0`.`internal_role_id` = 1) or (`i0`.`subject` = \'User\' and `i0`.`action` = \'update\' and `i0`.`internal_role_id` = 1))'], | ||
]); | ||
}); |