Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Created June 5, 2015 15:20
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/0ee229ca3bf4b650286e to your computer and use it in GitHub Desktop.
Save nolanlawson/0ee229ca3bf4b650286e to your computer and use it in GitHub Desktop.
Reproduce Webkit bug #137154
<html>
<body>
<h1>Demo primary key bug in Safari</h1>
<div>
<h3>
If you see text below saying "It's the Safari bug", then you have reproduced <a href='https://bugs.webkit.org/show_bug.cgi?id=137154'>this bug</a>.
</h3>
</div>
<div id="logger"></div>
<script>
function log(msg) {
document.getElementById("logger").innerHTML += msg + "<br>";
}
var openRequest = indexedDB.open("mydb", 1);
openRequest.onupgradeneeded = function(e) {
var thisDB = e.target.result;
if(!thisDB.objectStoreNames.contains("people")) {
thisDB.createObjectStore("people", {keyPath:"id"});
}
if(!thisDB.objectStoreNames.contains("notes")) {
thisDB.createObjectStore("notes", {keyPath:"uid"});
}
}
openRequest.onsuccess = function(e) {
db = e.target.result;
addPerson();
}
openRequest.onerror = function(e) {
log("openRequest error - " + e);
}
var sharedID = 0;
function addPerson() {
log("About to add person and note");
//Get a transaction
//default for OS list is all, default for type is read
var transaction = db.transaction("people","readwrite");
//Ask for the objectStore
var store = transaction.objectStore("people");
//Define a person
var person = {
name:"Person",
id:sharedID
}
//Perform the add
var request = store.add(person);
request.onerror = function(e) {
log("Error adding person - ", e);
}
request.onsuccess = function(e) {
log("Successfully added person");
setTimeout("addComplete();", 0);
}
//Define a note
var note = {
note:"Note",
uid:sharedID
}
var transaction2 = db.transaction("notes","readwrite");
//Ask for the objectStore
var store2 = transaction2.objectStore("notes");
//Perform the add
var request2 = store2.add(note);
request2.onerror = function(e) {
log("Error adding note - ", e);
}
request2.onsuccess = function(e) {
log("Successfully added note");
setTimeout("addComplete();", 0);
}
}
var completedAdds = 0;
function addComplete()
{
if (++completedAdds < 2)
return;
//Get a transaction
var transaction = db.transaction("people","readwrite");
//Ask for the objectStore
var store = transaction.objectStore("people");
//Perform the add
var request = store.get(sharedID);
request.onerror = function(e) {
log("Error getting person - ", e);
}
request.onsuccess = function(e) {
log("Successfully got person");
if (!e.target.result) {
log('<strong>Oh no! Something went wrong! It\'s the Safari bug!</strong>');
} else {
log('<strong>Everything looks normal. :) No Safari bug!</strong>');
for (n in e.target.result) {
log(n);
}
}
getComplete();
}
var transaction2 = db.transaction("notes","readwrite");
//Ask for the objectStore
var store2 = transaction2.objectStore("notes");
//Perform the add
var request2 = store2.get(sharedID);
request2.onerror = function(e) {
log("Error getting note - ", e);
}
request2.onsuccess = function(e) {
log("Successfully got note");
for (n in e.target.result) {
log(n);
}
getComplete();
}
}
var completedGets = 0;
function getComplete()
{
indexedDB.deleteDatabase('mydb');
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment