mdn / content

The content behind MDN Web Docs
https://developer.mozilla.org
Other
9.14k stars 22.46k forks source link

indexedDB.deleteDatabase doc: Mention that deleting a database isn't completed until the upgradeNeeded result is closed #20368

Open jespertheend opened 2 years ago

jespertheend commented 2 years ago

MDN URL

https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory/deleteDatabase

What specific section or headline is this issue about?

No response

What information was incorrect, unhelpful, or incomplete?

When calling IDBFactory.deleteDatabase(), the database isn't deleted until the upgradeneeded event is garbage collected, which can by triggered by calling request.result.close() on the indexedDB.open() result.

What did you expect to see?

Mention that onsuccess won't be fired until the indexedDB.open() result is closed like so:

const request = indexedDB.open(dbName);
request.onupgradeneeded = () => {
  request.result.close();
}

though to be honest I'm not sure how much of this depends on my specific situation. I reckon there might be other calls that similarly cause a database to stay open like this.

Do you have any supporting links, references, or citations?

Initially I thought this was a bug in Chrome, so I reported https://bugs.chromium.org/p/chromium/issues/detail?id=1359843. But it seems like this behaviour is intentional. You can find an example of this behaviour at https://indexeddb-deletedatabase-hanging.glitch.me/ you'll see that unless you have the 'close indexedDB.open() result' checkbox checked, deleting databases will take significantly longer depending on how fast the objects are garbage collected.

Do you have anything more you want to share?

No response

MDN metadata

Page report details * Folder: `en-us/web/api/idbfactory/deletedatabase` * MDN URL: https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory/deleteDatabase * GitHub URL: https://github.com/mdn/content/blob/main/files/en-us/web/api/idbfactory/deletedatabase/index.md * Last commit: https://github.com/mdn/content/commit/916af5e72ce683c6f7795755830c11677ca0f529 * Document last modified: 2022-08-04T06:19:25.000Z
evanstade commented 2 years ago

I think that specifically, this part of the spec should be made more explicit in the docs:

An event with type versionchange will be fired at an open connection if an attempt is made to upgrade or delete the database. This gives the connection the opportunity to close to allow the upgrade or delete to proceed.

So in the MDN docs where it says:

When deleteDatabase() is called, any other open connections to this particular database will get a versionchange event.

It could perhaps be added:

This gives the connection the opportunity to close to allow the deletion to proceed.

jespertheend commented 2 years ago

It took me quite some time to realise this, but I was opening new connections for every transaction and never closing them. So this was why calling IDBDatabase.close() on only the initial transaction did nothing to make the IDBFactory.deleteDatabase() request succeed.

I also noticed that newVersion on the versionchange event is null when a database is being deleted, for which I have submitted https://github.com/mdn/content/pull/20738. But I think it could be worth mentioning this on this page as well.

In my case I am specifically checking if newVersion is null and only then am I closing the connection, though I am not sure if it is common practice to simply always close the connection on every version change. If so it might be appropriate to also add a section to https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/versionchange_event.