Skip to content

Commit 36de362

Browse files
fix: For the decimal type, HANA and SQLite have different behaviors. (#119)
Co-authored-by: Nick Josipovic <[email protected]>
1 parent b355dcd commit 36de362

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

lib/change-log.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ const { localizeLogFields } = require("./localization")
1818
const isRoot = "change-tracking-isRootEntity"
1919

2020

21+
function formatDecimal(str, scale) {
22+
if (typeof str === "number" && !isNaN(str)) {
23+
str = String(str);
24+
} else return str;
25+
26+
if (scale > 0) {
27+
let parts = str.split(".");
28+
let decimalPart = parts[1] || "";
29+
30+
while (decimalPart.length < scale) {
31+
decimalPart += "0";
32+
}
33+
34+
return `${parts[0]}.${decimalPart}`;
35+
}
36+
37+
return str;
38+
}
39+
2140
const _getRootEntityPathVals = function (txContext, entity, entityKey) {
2241
const serviceEntityPathVals = []
2342
const entityIDs = _getEntityIDs(txContext.params)
@@ -316,6 +335,28 @@ function _trackedChanges4 (srv, target, diff) {
316335
const eleParentKeys = element.parent.keys
317336
if (from === to) return
318337

338+
/**
339+
*
340+
* HANA driver always filling up the defined decimal places with zeros,
341+
* need to skip the change log if the value is not changed.
342+
* Example:
343+
* entity Books : cuid {
344+
* price : Decimal(11, 4);
345+
* }
346+
* When price is updated from 3000.0000 to 3000,
347+
* the change log should not be created.
348+
*/
349+
if (
350+
row._op === "update" &&
351+
element.type === "cds.Decimal" &&
352+
cds.db.kind === "hana" &&
353+
typeof to === "number"
354+
) {
355+
const scaleNum = element.scale || 0;
356+
if (from === formatDecimal(to, scaleNum))
357+
return;
358+
}
359+
319360
/**
320361
*
321362
* For the Inline entity such as Items,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ID;title;descr;isUsed;author_ID;stock;price;genre_ID;bookStore_ID
2-
9d703c23-54a8-4eff-81c1-cdce6b8376b1;Wuthering Heights;"Wuthering Heights, Emily Brontë's only novel, was published in 1847 under the pseudonym ""Ellis Bell"". It was written between October 1845 and June 1846. Wuthering Heights and Anne Brontë's Agnes Grey were accepted by publisher Thomas Newby before the success of their sister Charlotte's novel Jane Eyre. After Emily's death, Charlotte edited the manuscript of Wuthering Heights and arranged for the edited version to be published as a posthumous second edition in 1850.";true;d4d4a1b3-5b83-4814-8a20-f039af6f0387;12;11.11;11;64625905-c234-4d0d-9bc1-283ee8946770
2+
9d703c23-54a8-4eff-81c1-cdce6b8376b1;Wuthering Heights;"Wuthering Heights, Emily Brontë's only novel, was published in 1847 under the pseudonym ""Ellis Bell"". It was written between October 1845 and June 1846. Wuthering Heights and Anne Brontë's Agnes Grey were accepted by publisher Thomas Newby before the success of their sister Charlotte's novel Jane Eyre. After Emily's death, Charlotte edited the manuscript of Wuthering Heights and arranged for the edited version to be published as a posthumous second edition in 1850.";true;d4d4a1b3-5b83-4814-8a20-f039af6f0387;12;3000.0000;11;64625905-c234-4d0d-9bc1-283ee8946770
33
676059d4-8851-47f1-b558-3bdc461bf7d5;Jane Eyre;"Jane Eyre /ɛər/ (originally published as Jane Eyre: An Autobiography) is a novel by English writer Charlotte Brontë, published under the pen name ""Currer Bell"", on 16 October 1847, by Smith, Elder & Co. of London. The first American edition was published the following year by Harper & Brothers of New York. Primarily a bildungsroman, Jane Eyre follows the experiences of its eponymous heroine, including her growth to adulthood and her love for Mr. Rochester, the brooding master of Thornfield Hall. The novel revolutionised prose fiction in that the focus on Jane's moral and spiritual development is told through an intimate, first-person narrative, where actions and events are coloured by a psychological intensity. The book contains elements of social criticism, with a strong sense of Christian morality at its core and is considered by many to be ahead of its time because of Jane's individualistic character and how the novel approaches the topics of class, sexuality, religion and feminism.";true;47f97f40-4f41-488a-b10b-a5725e762d5e;11;12.34;11;5ab2a87b-3a56-4d97-a697-7af72334a384
44
42bc7997-f6ce-4ae9-8a64-ee5e02ef1087;The Raven;"""The Raven"" is a narrative poem by American writer Edgar Allan Poe. First published in January 1845, the poem is often noted for its musicality, stylized language, and supernatural atmosphere. It tells of a talking raven's mysterious visit to a distraught lover, tracing the man's slow fall into madness. The lover, often identified as being a student, is lamenting the loss of his love, Lenore. Sitting on a bust of Pallas, the raven seems to further distress the protagonist with its constant repetition of the word ""Nevermore"". The poem makes use of folk, mythological, religious, and classical references.";true;5c30d395-db0a-4095-bd7e-d4de34646607;333;13.13;16;5ab2a87b-3a56-4d97-a697-7af72334a384
55
9297e4ea-396e-47a4-8815-cd4622dea8b1;Eleonora;"""Eleonora"" is a short story by Edgar Allan Poe, first published in 1842 in Philadelphia in the literary annual The Gift. It is often regarded as somewhat autobiographical and has a relatively ""happy"" ending.";true;5c30d395-db0a-4095-bd7e-d4de34646607;555;14;16;8aaed432-8336-4b0d-be7e-3ef1ce7f13ea
6-
574c8add-0ee3-4175-ab62-ca09a92c723c;Catweazle;Catweazle is a British fantasy television series, starring Geoffrey Bayldon in the title role, and created by Richard Carpenter for London Weekend Television. The first series, produced and directed by Quentin Lawrence, was screened in the UK on ITV in 1970. The second series, directed by David Reid and David Lane, was shown in 1971. Each series had thirteen episodes, most but not all written by Carpenter, who also published two books based on the scripts.;true;a45da28a-7f55-4b53-8a63-48b61132d1b9;22;15;13;8aaed432-8336-4b0d-be7e-3ef1ce7f13ea
6+
574c8add-0ee3-4175-ab62-ca09a92c723c;Catweazle;Catweazle is a British fantasy television series, starring Geoffrey Bayldon in the title role, and created by Richard Carpenter for London Weekend Television. The first series, produced and directed by Quentin Lawrence, was screened in the UK on ITV in 1970. The second series, directed by David Reid and David Lane, was shown in 1971. Each series had thirteen episodes, most but not all written by Carpenter, who also published two books based on the scripts.;true;a45da28a-7f55-4b53-8a63-48b61132d1b9;22;15;13;8aaed432-8336-4b0d-be7e-3ef1ce7f13ea

tests/bookshop/db/schema.cds

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ entity Books : managed, cuid {
126126
@title : '{i18n>books.genre}'
127127
genre : Association to Genres;
128128
stock : Integer;
129-
price : Decimal;
129+
price : Decimal(11, 4);
130130
isUsed : Boolean;
131131
image : LargeBinary @Core.MediaType : 'image/png';
132132
@title : '{i18n>books.bookType}'

tests/integration/fiori-draft-enabled.test.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,14 @@ describe("change log integration test", () => {
251251
});
252252

253253
it("2.2 Child entity update - should log basic data type changes (ERP4SMEPREPWORKAPPPLAT-32 ERP4SMEPREPWORKAPPPLAT-613)", async () => {
254+
cds.services.AdminService.entities.Books.elements.price["@changelog"] = true;
255+
254256
const action = PATCH.bind({}, `/odata/v4/admin/Books(ID=9d703c23-54a8-4eff-81c1-cdce6b8376b1,IsActiveEntity=false)`, {
255257
title: "new title",
256258
author_ID: "47f97f40-4f41-488a-b10b-a5725e762d5e",
257259
genre_ID: 16,
258-
isUsed: false
260+
isUsed: false,
261+
price: 3000
259262
});
260263
await utils.apiAction("admin", "BookStores", "64625905-c234-4d0d-9bc1-283ee8946770", "AdminService", action);
261264
const bookChanges = await adminService.run(
@@ -344,6 +347,17 @@ describe("change log integration test", () => {
344347
expect(isUsedChange.valueChangedFrom).to.equal("true");
345348
expect(isUsedChange.valueChangedTo).to.equal("false");
346349

350+
// The current price is 3000.0000, and update operation via OData service is price: 3000. In this case, a changelog should not be generated.
351+
const priceChanges = await adminService.run(
352+
SELECT.from(ChangeView).where({
353+
entity: "sap.capire.bookshop.Books",
354+
attribute: "price",
355+
})
356+
);
357+
358+
expect(priceChanges.length).to.equal(0);
359+
360+
delete cds.services.AdminService.entities.Books.elements.price["@changelog"];
347361
});
348362

349363
it("2.3 Child entity delete - should log basic data type changes (ERP4SMEPREPWORKAPPPLAT-32 ERP4SMEPREPWORKAPPPLAT-613)", async () => {
@@ -604,7 +618,7 @@ describe("change log integration test", () => {
604618
expect(titleChanges.length).to.equal(1);
605619

606620
const titleChange = titleChanges[0];
607-
expect(titleChange.objectID).to.equal("11.11, new title, 12");
621+
expect(titleChange.objectID).to.equal("3000, new title, 12");
608622

609623
cds.services.AdminService.entities.Books["@changelog"] = [
610624
{ "=": "title" },

0 commit comments

Comments
 (0)