Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Created September 13, 2015 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolanlawson/28ff2db3f3cb386c8ecb to your computer and use it in GitHub Desktop.
Save nolanlawson/28ff2db3f3cb386c8ecb to your computer and use it in GitHub Desktop.
Repro WebKit IndexedDB unique index bug
<body>
<h1> Repro WebKit IndexedDB unique index bug </h1>
<p>If you see the text <strong>There is a bug</strong>, then there's a bug.</p>
<div id="logger"></div>
<script>
var dbname = 'rando_' + Math.random() + '_';
function log(msg) {
document.getElementById("logger").innerHTML += msg + "<br>";
}
var req = indexedDB.open(dbname + "1");
req.onupgradeneeded = function (e) {
var db = e.target.result;
if (e.oldVersion === 1) {
return;
}
db.createObjectStore('store', {autoIncrement: true}).createIndex('key', 'key', {unique: true});
};
req.onsuccess = function (e) {
var db = e.target.result;
var txn = db.transaction('store', 'readwrite');
txn.objectStore('store').put({key: 'a'})
txn.oncomplete = function () {
var txn = db.transaction('store', 'readwrite');
var putReq = txn.objectStore('store').put({key: 'a'});
putReq.onsuccess = function () {
log('We managed to put() the same key twice!')
log('<strong>There is a bug</strong>')
};
putReq.onerror = function (e) {
log('Got a ConstraintError when we tried to put() the same key twice');
e.preventDefault(); // avoid transaction abort
e.stopPropagation(); // avoid transaction onerror
};
txn.oncomplete = function () {
var txn = db.transaction('store', 'readonly');
var keys = [];
var primaryKeys = [];
txn.objectStore('store').index('key').openKeyCursor().onsuccess = function (e) {
var cursor = e.target.result;
if (!cursor) {
return;
}
keys.push(cursor.key);
primaryKeys.push(cursor.primaryKey);
cursor.continue();
};
txn.oncomplete = function () {
log('')
log('Found keys: ' + keys.join(', '));
log('Found primaryKeys: ' + primaryKeys.join(', '));
};
};
};
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment