Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/node-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [Unreleased]
### Fixed
- Updating models updateOnDuplicate field being calculated incorrectly (#2976)

## [18.5.2] - 2025-11-20
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ jest.mock('@subql/x-sequelize', () => {
bulkCreate: jest.fn((records: {id: string}[]) => {
records.map((r) => (pendingData[r.id] = r));
}),
getAttributes: jest.fn(() => ({})),
destroy: jest.fn(({where: {id}}) => pendingDeletes.push(id)),
}),
sync: jest.fn(),
Expand Down Expand Up @@ -197,7 +198,7 @@ describe('cacheModel', () => {
);

/* get usese the get cache */
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion

const entity = (await testModel.get('entity1_id_0x01'))!;

expect(entity).toBeDefined();
Expand All @@ -209,7 +210,7 @@ describe('cacheModel', () => {
expect(entity2?.field1).toEqual(2);

/* getBy methods use set cache */
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion

const [entity3] = (await testModel.getByFields([['field1', '=', 2]], {limit: 1}))!;
expect(entity3?.field1).toEqual(2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,12 @@ export class CachedModel<T extends BaseEntity = BaseEntity>
}
}

const allKeys = Object.keys(this.model.getAttributes()) as (keyof T)[];
dbOperation = Promise.all([
records.length &&
this.model.bulkCreate(records, {
transaction: tx,
updateOnDuplicate: Object.keys(records[0]) as unknown as (keyof T)[],
updateOnDuplicate: allKeys,
}),
Object.keys(removeRecords).length &&
this.model.destroy({where: {id: Object.keys(removeRecords)} as any, transaction: tx}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ describe('Model provider consistency test', () => {
});

describe('disable historical', () => {
let plainModel: PlainModel<{id: string; field1: number}>;
let cacheModel: CachedModel<{id: string; field1: number}>;
let plainModel: PlainModel<{id: string; field1?: number}>;
let cacheModel: CachedModel<{id: string; field1?: number}>;
let i = 0;
beforeAll(() => {
plainModel = new PlainModel(model, false);
Expand Down Expand Up @@ -117,6 +117,28 @@ describe('Model provider consistency test', () => {
expect(result3.length).toEqual(2);
expect(cacheResult3).toEqual(result3);
});

it('update with optional fields', async () => {
// This test checks that `updateOnDuplicate` is correct, previously it would use Object.keys(data[0]) which would cause issues when later data has more keys,
const data = [
{id: '1', field1: 1},
{id: '2', field1: 2},
];

// Set some initial data
await plainModel.bulkUpdate(data, 1);
const initial2 = await plainModel.get('2');
expect(initial2).toEqual({id: '2', field1: 2});

// Set new data + update previous data
const data2 = [
{id: '3'}, // Omit field1 because its optional
{id: '2', field1: 3},
];
await plainModel.bulkUpdate(data2, 2);
const updated2 = await plainModel.get('2');
expect(updated2).toEqual({id: '2', field1: 3});
});
});

describe('alternative id types', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ export class PlainModel<T extends BaseEntity = BaseEntity> implements IModel<T>
);
}

const allKeys = Object.keys(this.model.getAttributes()) as (keyof T)[];
await this.model.bulkCreate(data as CreationAttributes<Model<T, T>>[], {
transaction: tx,
updateOnDuplicate: Object.keys(data[0]) as unknown as (keyof T)[],
updateOnDuplicate: allKeys,
});

if (this.exporters.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jest.mock('@subql/x-sequelize', () => {
]),
bulkCreate: jest.fn(),
destroy: jest.fn(),
getAttributes: jest.fn(() => ({id: null, field1: null})),
}),
sync: jest.fn(),
transaction: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jest.mock('@subql/x-sequelize', () => {
]),
bulkCreate: jest.fn(),
destroy: jest.fn(),
getAttributes: jest.fn(() => ({})),
}),
sync: jest.fn(),
transaction: () => ({
Expand Down
3 changes: 2 additions & 1 deletion packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [Unreleased]
### Fixed
- Updating models updateOnDuplicate field being calculated incorrectly (#2976)

## [6.4.4] - 2025-11-20
### Fixed
Expand Down
Loading