Skip to content

Commit

Permalink
fix(core): do not issue extra updates when there are no matching chan…
Browse files Browse the repository at this point in the history
…gesets

Closes #5510
  • Loading branch information
B4nan committed May 1, 2024
1 parent 5ef57b6 commit 03934d0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/core/src/unit-of-work/UnitOfWork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,18 @@ export class UnitOfWork {
return;
}

let conflicts = false;

for (const cs of this.changeSets.values()) {
if (cs.rootName === changeSet.rootName) {
conflicts = true;
}
}

if (!conflicts) {
return;
}

this.extraUpdates.add([changeSet.entity, props.map(p => p.name), props.map(p => changeSet.entity[p.name]), changeSet]);

for (const p of props) {
Expand Down
63 changes: 63 additions & 0 deletions tests/issues/GH5510.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { MikroORM, Entity, OneToOne, PrimaryKey, Property } from '@mikro-orm/postgresql';
import { mockLogger } from '../helpers';

@Entity()
class Other {

@PrimaryKey()
id!: number;

@Property()
name!: string;

@Property({ nullable: true, onUpdate: () => new Date() })
updatedAt?: Date;

}

@Entity()
class Test {

@PrimaryKey()
id!: number;

@Property()
name!: string;

@Property({ nullable: true, onUpdate: () => new Date() })
updatedAt?: Date;

@OneToOne(() => Other, { nullable: true })
other?: Other;

}

let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
dbName: '5510',
entities: [Test],
});
await orm.schema.refreshDatabase();
});

afterAll(async () => {
await orm.close(true);
});

it('should merge all changes of one record into one sql update statement', async () => {
const tid = await orm.em.insert(Test, { name: 'Foo' });
const test = await orm.em.findOne(Test, { id: tid });

const other = new Other();
other.name = 'Foo';
test!.other = other;

const mock = mockLogger(orm);
await orm.em.flush();

expect(mock.mock.calls).toHaveLength(
[['begin'], ['insert into other'], ['update test'], ['commit']].length,
);
});

0 comments on commit 03934d0

Please sign in to comment.