MOBILE-4640 db: Fix bug when updating primary key in memory database

Before this fix, the record was updated but the key was still the old primary key, so the new record wasn't found when using getOneByPrimaryKey.
main
Dani Palou 2024-08-09 14:43:43 +02:00
parent 9a1cd83207
commit 2abe483c6d
3 changed files with 24 additions and 4 deletions

View File

@ -172,7 +172,7 @@ export class CoreEagerDatabaseTable<
continue;
}
Object.assign(record, updates);
this.updateMemoryRecord(record, updates, this.records);
}
}
@ -187,7 +187,7 @@ export class CoreEagerDatabaseTable<
continue;
}
Object.assign(record, updates);
this.updateMemoryRecord(record, updates, this.records);
}
}

View File

@ -96,4 +96,24 @@ export abstract class CoreInMemoryDatabaseTable<
return rowId;
}
/**
* Update a record in memory.
*
* @param record Record to update.
* @param updates New values.
* @param records Records object.
*/
protected updateMemoryRecord(record: DBRecord, updates: Partial<DBRecord>, records: Record<string, DBRecord | null>): void {
const previousPrimaryKey = this.serializePrimaryKey(this.getPrimaryKeyFromRecord(record));
Object.assign(record, updates);
const newPrimaryKey = this.serializePrimaryKey(this.getPrimaryKeyFromRecord(record));
if (newPrimaryKey !== previousPrimaryKey) {
delete records[previousPrimaryKey];
records[newPrimaryKey] = record;
}
}
}

View File

@ -156,7 +156,7 @@ export class CoreLazyDatabaseTable<
continue;
}
Object.assign(record, updates);
this.updateMemoryRecord(record, updates, this.records);
}
}
@ -171,7 +171,7 @@ export class CoreLazyDatabaseTable<
continue;
}
Object.assign(record, updates);
this.updateMemoryRecord(record, updates, this.records);
}
}