NEW 178995
IndexedDB cursors fail to iterate on non-unique indexes with updates
https://bugs.webkit.org/show_bug.cgi?id=178995
Summary IndexedDB cursors fail to iterate on non-unique indexes with updates
Philip Walton
Reported 2017-10-29 22:04:41 PDT
Created attachment 325323 [details] The code in the description as a runnable HTML file When using an IndexedDB object store with an index set to `unique:false`, I noticed that cursors fail to iterate fully when there's an update call involved. STR: 1. Run the following code and notice that there are 4 items logged to the console. ```html <script> const createDb = () => { const openRequest = indexedDB.open('db', 1); openRequest.onupgradeneeded = (evt) => { const db = evt.target.result; const itemsStore = db.createObjectStore('items', {autoIncrement: true}); itemsStore.createIndex('name', 'name', {unique: false}); } openRequest.onsuccess = (evt) => { populateDb(evt.target.result); } } const populateDb = (db) => { const txn = db.transaction(['items'], 'readwrite'); const itemsStore = txn.objectStore('items'); itemsStore.add({name: 'foo', id: Math.random()}); itemsStore.add({name: 'bar', id: Math.random()}); itemsStore.add({name: 'foo', id: Math.random()}); itemsStore.add({name: 'bar', id: Math.random()}); txn.oncomplete = () => iterateOverItems(db); } const iterateOverItems = (db) => { const txn = db.transaction('items', 'readwrite'); const itemsStore = txn.objectStore('items'); const itemsIndex = itemsStore.index('name'); itemsIndex.openCursor().onsuccess = (evt) => { const cursor = evt.target.result; if (cursor) { const {value} = cursor; console.log(value); // Uncomment this line to see that calling `.update()` causes the cursor // to ignore non-unique values for this index. // cursor.update(value); cursor.continue(); } else { db.close(); indexedDB.deleteDatabase('db'); } } } createDb(); </script> ``` 2. Then uncomment the line `cursor.update(value)` and run it again. Notice how only 2 values are logged to the console. This works as expected in Chrome and Firefox, and it works when there's no update call. From my perspective it appears the presence of the update call makes the cursor behave as if the indexes are unique (which they're not).
Attachments
The code in the description as a runnable HTML file (1.37 KB, text/html)
2017-10-29 22:04 PDT, Philip Walton
no flags
Philip Walton
Comment 1 2017-10-29 23:56:30 PDT
This appears to also apply to `cursor.delete()` calls in addition to `cursor.update()`.
Radar WebKit Bug Importer
Comment 2 2017-10-30 09:09:35 PDT
Philip Walton
Comment 3 2017-11-08 22:01:48 PST
I added a Web Platform Test for this issue. As you can see from the results, it's passing in Chrome and Firefox but failing in Safari: https://github.com/w3c/web-platform-tests/blob/master/IndexedDB/idbcursor-iterating-update.htm
Note You need to log in before you can comment on or make changes to this bug.