It is likely that when we remove the "webkit" prefix we'll only support the onupgradeneeded approach and drop setVersion(). That's a ways off though.
We will support setVersion for several more versions of Chrome at least. We would also like to support a transition period where both setVersion and onupgradeneeded are supported, but we haven't fully confirmed that the spec would allow that behavior.
To be future-proof and support other browsers, the following (UNTESTED!) pattern should work:
indexedDB = indexedDB || webkitIndexedDB || mozIndexedDB || msIndexedDB;
var DESIRED_VERSION = 1;
var openRequest = indexedDB.open('db-name', DESIRED_VERSION);
// Always anticipate blocked events
openRequest.onblocked = function (e) {
...
};
// Support newer API:
openRequest.onupgradeneeded = function (e) {
var transaction = openRequest.transaction;
doUpgrade(e.oldVersion, transaction);
};
openRequest.onsuccess = function() {
var db = openRequest.result;
var oldVersion = Number(db.version);
if (oldVersion !== DESIRED_VERSION) {
// Support older API:
if (!db.setVersion) { throw new Error(); }
var versionRequest = db.setVersion(ver);
versionRequest.onsuccess = function (e) {
var transaction = versionRequest.result;
doUpgrade(oldVersion, transaction);
useDatabase(db);
};
} else {
useDatabase(db);
}
};
function doUpgrade(oldVersion, transaction) {
...
}
function useDatabase(db) {
...
}