Skip to content

fix: keep the update hook registered after Database.export - #642

Closed
ybai08 wants to merge 1 commit into
sql-js:masterfrom
ybai08:fix/update-hook-after-export
Closed

ybai08 wants to merge 1 commit into
sql-js:masterfrom
ybai08:fix/update-hook-after-export

Conversation

@ybai08

@ybai08 ybai08 commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Database.export() closes the connection and opens a new one. The update hook was registered on the old connection only, so after an export no more changes were reported. updateHookFunctionPtr still pointed to the old callback, so nothing showed that the hook had been dropped:

db.updateHook((op, dbName, table, rowId) => console.log(op, table, rowId));
db.export();
db.exec("INSERT INTO consoles VALUES (4, 'Nintendo', 'Switch')"); // nothing is logged

export() now registers the existing callback on the reopened connection, the same way it already registers the extension functions again (#565). Calling updateHook(null) afterwards still removes it, because that call now acts on the new connection.

Test: one more step at the end of test/test_update_hook.js: export, insert a row, and expect the insert call.

Testing note: Emscripten is not installed on the machine this was prepared on, so the bundles were not rebuilt with npm run build. Instead, the src/api.js text inside the published 1.15.1 debug bundles (sql-wasm-debug.js, sql-wasm-inline-debug.js and their worker. versions) was replaced with the patched file. Those bundles contain src/api.js at fa1f0c1 verbatim. The test runner was then run against them:

  • before the fix (unmodified 1.15.1 bundles, new test only): node test/all.js wasm-debug and node test/all.js wasm-inline-debug -> FAIL test update_hook ("keep the update hook after an export"), Passed:27 Failed:1
  • after the fix: node test/all.js wasm-debug and node test/all.js wasm-inline-debug -> Passed:28 Failed:0
  • npx eslint . passes

The optimized wasm, wasm-browser and wasm-inline bundles were not rebuilt, so the full npm test (including test/builds.js) was not run with the change.

export() closes and reopens the database, and the new connection had no
update hook, so changes made after an export were no longer reported,
while updateHookFunctionPtr still pointed to the old callback.
@lovasoa

lovasoa commented Oct 26, 2026

Copy link
Copy Markdown
Member

Thank you for bringing this to my attention ! I think the clean fix is to use the dedicated https://sqlite.org/c3ref/serialize.html instead of closing the database entirely. I'll open a different PR.

@ybai08

ybai08 commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Makes sense: with sqlite3_serialize the connection stays open, so the update hook and the extension functions survive without any re-registering. Closing this in favour of your PR. The "keep the update hook after an export" case in test/test_update_hook.js here should still apply as a regression test, if it is useful.

@ybai08 ybai08 closed this Oct 26, 2026
lovasoa added a commit that referenced this pull request Oct 26, 2026
Use SQLite's backup API to fill a single preallocated JS-owned export buffer without closing the source connection or allocating a database-sized WASM snapshot. Preserve callbacks, statements, connection settings and transaction state; reject active write transactions without rolling them back. Add export regression coverage and prepare version 1.15.2. Supersedes #642 and #643. Validated by full CI on all WebAssembly variants.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants