IndexedDB version change and custom rollback procedure integration
0 reputation · 06 Oct 2021, 09:49 UTC
0 reputation · 06 Oct 2021, 09:49 UTC
Goal: guarantee that after an IndexedDB schema upgrade the application can revert to the previous object‑store layout without losing user‑generated data.
Constraint: IndexedDB only allows version numbers to increase; attempting to open a lower version throws a VersionError and does not automatically downgrade stores, so any rollback must rely on developer‑provided backup and restore logic, and the precise moment to capture a backup relative to the version‑change request is not defined by the specification.
What triggers the backup process relative to the version‑change request? When should the application interpret a VersionError as a cue to initiate restoration? Which storage mechanisms provide reliable snapshots for IndexedDB object stores across major browsers?
32775 reputation · 06 Oct 2021, 16:39 UTC
The safest moment is inside the versionchange transaction’s oncomplete (or success) handler, which fires after the transaction is granted but before any schema modifications are applied. At that point you have a consistent snapshot of all existing object stores.
onupgradeneeded handler (or the versionchange transaction’s oncomplete), open a separate backup database (different name or origin).put each record into the corresponding store in the backup database.A VersionError should be treated as a rollback cue only when the error originates from an attempt to open the database with a version number lower than the current one. Check event.target.error.name === 'VersionError' and compare event.target.result?.version (or the requested version passed to indexedDB.open) against the known current version. If the requested version is lower, invoke your restore routine; otherwise treat the error as a concurrent upgrade conflict and retry or abort.
localStorage or sessionStorage as a fallback; be aware of the 5 MB quota per origin.localStorage or to a backup IndexedDB database.These mechanisms give you a durable snapshot that can be read back and re‑inserted after clearing the original stores.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.