Skip to content

Commit

Permalink
fix bug where update without editing parent table results in err
Browse files Browse the repository at this point in the history
  • Loading branch information
chaidhat committed Dec 27, 2024
1 parent 9eb1c96 commit 3646824
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/orm-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,9 @@ class DatabaseTable {
}
}

if (queryStrEntries.length === 0) {
return; // there is no update to be executed (occurs if we update the child table but not the table itself)
}
const queryStr = `UPDATE ${this.tableName} SET ${queryStrEntries.join(", ")} WHERE ${queryStrWheres.join(" AND ")};`;
await this.query(queryStr);
}
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async function runTests() {
xdescribe('insert tests', function () {
require('./insert-tests');
});
xdescribe('update tests', function () {
describe('update tests', function () {
require('./update-tests');
});
xdescribe('del tests', function () {
Expand Down
128 changes: 128 additions & 0 deletions test/update-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,134 @@ it('can update table with one-to-many relation', async function () {
mem.length,
);

let i = 0;
mem.forEach((memVal) => {
const foundRes = res.find((resElement) =>
resElement.debugTestTableId == memVal.debugTestTableId
);
if (foundRes === undefined) {
throw `fail: did not find relation ${JSON.stringify(memVal)} ${memVal.debugTestTableId} (idx: ${i}) in DebugTestTable0`
}

assert.equal(memVal.a, foundRes.a);
assert.equal(memVal.b, foundRes.b);
assert.equal(
memVal.d.length,
foundRes.d.length,
);
memVal.d.forEach((dd) => {
const foundResD = foundRes.d.find((resElement) =>
resElement.debugTestTable0Id == dd.debugTestTable0Id
);
if (foundRes === undefined) {
throw `fail: did not find relation ${JSON.stringify(dd)} ${memVal.debugTestTableId} (idx: ${i}) in DebugTestTable0`
}

assert.equal(dd.da, foundResD.da);
assert.equal(dd.db, foundResD.db);
assert.equal(memVal.debugTestTableId, foundResD.debugTestTableId);
});
});
});
it('can update table with one-to-many relation with only child being set', async function () {
// setup
options.debugTestTable0 = new orm.DatabaseTable(`DebugTestTable0`,
"debugTestTable0Id",
[
{
name: "debugTestTableId",
type: "int"
},
{
name: "da",
type: "int"
},
{
name: "db",
type: "varchar(256)"
},
]);
await options.debugTestTable0.init();

options.debugTestTable = new orm.DatabaseTable(`DebugTestTable`,
"debugTestTableId",
[
{
name: "a",
type: "int"
},
{
name: "b",
type: "varchar(256)"
},
{
name: "d",
type: `DebugTestTable0[]`, // one-to-many relation
// this requires options.debugTestTable0 to have a property with the name `DebugTestTableid`
},
]);
await options.debugTestTable.init();

const mem = [];
let debugTestTable0IdKey = 1;
for (let i = 0; i < common.MAX_LEN; i++) {
const d = [];
for (let j = 0; j < randomInt(0, 3); j++) {
d.push({
debugTestTable0Id: debugTestTable0IdKey,
debugTestTableId: i + 1,
da: randomInt(),
db: randomStr(),
})
debugTestTable0IdKey++;
}
mem[i] = {
debugTestTableId: i + 1,
a: randomInt(),
b: randomStr(),
d: d,
};
await orm.adminQuery(`INSERT INTO DebugTestTable (debugTestTableId, a,b) VALUES (${mem[i].debugTestTableId}, ${mem[i].a}, "${mem[i].b}")`);
for (let j = 0; j < d.length; j++) {
await orm.adminQuery(`INSERT INTO DebugTestTable0 (debugTestTable0Id, debugTestTableId, da, db) VALUES (${d[j].debugTestTable0Id}, ${d[j].debugTestTableId}, ${d[j].da}, "${d[j].db}")`);
}
}

// test
for (let i = 0; i < 10; i++) {
const randomIndex = randomInt(1, common.MAX_LEN);
const d = [];
for (let j = 0; j < randomInt(0, 3); j++) {
d.push({
debugTestTable0Id: debugTestTable0IdKey,
debugTestTableId: randomIndex,
da: randomInt(),
db: randomStr(),
})
debugTestTable0IdKey++;
}
mem[randomIndex] = {
debugTestTableId: randomIndex + 1,
a: mem[randomIndex].a,
b: mem[randomIndex].b,
d: d,
};
await options.debugTestTable.update(
{
debugTestTableId: randomIndex + 1,
},
{
d: mem[randomIndex].d.map((x) => {return {da: x.da, db: x.db}}),
}
);
}

const res = await options.debugTestTable.select();
assert.equal(
res.length,
mem.length,
);

let i = 0;
mem.forEach((memVal) => {
const foundRes = res.find((resElement) =>
Expand Down

0 comments on commit 3646824

Please sign in to comment.