Closed Zoxive closed 1 month ago
Ok. Something fishy is going on. Just refactored my code to query the row myself.. and the rowId that is given is not right.
Test updated to check rowId:
it("update hook, INSERT", async () => {
const { promise, promiseResolve } = getPromiseResolve<{ operation: string; table: string; row: unknown; rowId: number; }>();
db.updateHook(({ operation, row, rowId, table }) => {
// console.warn(
// `Hook has been called, rowId: ${rowId}, ${table}, ${operation}`,
// JSON.stringify(row, null, 2)
// );
promiseResolve?.({ operation, table, rowId, row });
});
await db.execute(
'INSERT INTO "User" (id, name, age, networth) VALUES(?, ?, ?, ?)',
[1, 'bob', 76, 523.02],
);
const hookResult = await promise;
const query = await db.execute('SELECT rowId FROM "User" WHERE id = ?', [1]);
// biome-ignore lint/style/noNonNullAssertion: <explanation>
const rowId = query.rows![0];
expect(hookResult).toEqual({ operation: "INSERT", table: "User", rowId, row: {} });
});
Locally, im getting rowId: 8 vs (expected) rowId: 1.
Edit: Quite the Heisenbug.
Sometimes the rowId is right, but it really likes to return 8 as the first rowId in the hook. (If i spam refresh my test app.. sometimes the test passes.. but its rare)
Ive also seen it return a rowId like 108, when my max in that table is 23.
More context after playing around today.. I decided to revert to before the reactiveQuery changes, which was 6.0.1
The row {} object is now populated for "UPDATE" | "INSERT" operations. Yay.
But the same rowid weirdness exists. The given rowid from the updateHook does not match the rowid from the row that my test changed. Im guessing theres some conversion problem from c++ -> native -> JS land. (But at the time c++ receives it and queries the db to create the row obj its fine there.. in 6.0.1 at least..) I dont even need the rowid now that the row obj is there, but it I kinda expected them to match.
You are going to have to wait until I have time to take a look at this, I'm currently changing cities and next flying to a conference in Asia, so it might take some time. Or you can take a look at the C++ code yourself.
I just took a look and the issue might be the same as #128. Basically executing any operation inside of the update hook callback is a undefined behavior/buggy behavior. I've removed the row from the result and it should work properly now I hope. Give 9.2.0
a try. This means to get the row you will have to query for the row manually outside of the hook. I've also updated the docs to reflect the row is no longer returned.
Going to close this, if you are still facing issues feel free to comment and I will take a look
Describe the bug In previous versions of op-sqlite updateHook would contain the row of the insert or update.
The notion documentation talks about it here: https://ospfranco.notion.site/API-1a39b6bb3eb74eb893d640c8c3459362#6983c7e0a73e4f219b560e3434ca9faf
But it seems to always be an empty object now in 9.1.2+ (im not sure what version caused the regression.. i actually just updated from 2.0.7 -> 9.1.2)
I'm actually ok with the behavior.. i can query the row myself via the table + rowId. The only heartache is that because the apis are all async now, i have do a little refactoring on my end (again not that bad).
I'm opening this to either.
Versions:
Reproducible example