Skip to content

Commit 789053d

Browse files
committed
Upgrade idb in messaging
1 parent 2d3c699 commit 789053d

File tree

5 files changed

+114
-106
lines changed

5 files changed

+114
-106
lines changed

packages/installations/src/helpers/idb-manager.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,19 @@ const OBJECT_STORE_NAME = 'firebase-installations-store';
2828
let dbPromise: Promise<IDBPDatabase> | null = null;
2929
function getDbPromise(): Promise<IDBPDatabase> {
3030
if (!dbPromise) {
31-
dbPromise = openDB(DATABASE_NAME, DATABASE_VERSION, {upgrade: (db, oldVersion) => {
32-
// We don't use 'break' in this switch statement, the fall-through
33-
// behavior is what we want, because if there are multiple versions between
34-
// the old version and the current version, we want ALL the migrations
35-
// that correspond to those versions to run, not only the last one.
36-
// eslint-disable-next-line default-case
37-
switch (oldVersion) {
38-
case 0:
39-
db.createObjectStore(OBJECT_STORE_NAME);
31+
dbPromise = openDB(DATABASE_NAME, DATABASE_VERSION, {
32+
upgrade: (db, oldVersion) => {
33+
// We don't use 'break' in this switch statement, the fall-through
34+
// behavior is what we want, because if there are multiple versions between
35+
// the old version and the current version, we want ALL the migrations
36+
// that correspond to those versions to run, not only the last one.
37+
// eslint-disable-next-line default-case
38+
switch (oldVersion) {
39+
case 0:
40+
db.createObjectStore(OBJECT_STORE_NAME);
41+
}
4042
}
41-
}});
43+
});
4244
}
4345
return dbPromise;
4446
}

packages/messaging/src/helpers/migrate-old-database.test.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { FakePushSubscription } from '../testing/fakes/service-worker';
2828
import { base64ToArray } from './array-base64-translator';
2929
import { expect } from 'chai';
3030
import { getFakeTokenDetails } from '../testing/fakes/token-details';
31-
import { openDb } from 'idb';
31+
import { openDB } from 'idb';
3232

3333
describe('migrateOldDb', () => {
3434
it("does nothing if old DB didn't exist", async () => {
@@ -179,25 +179,27 @@ describe('migrateOldDb', () => {
179179
});
180180

181181
async function put(version: number, value: object): Promise<void> {
182-
const db = await openDb('fcm_token_details_db', version, upgradeDb => {
183-
if (upgradeDb.oldVersion === 0) {
184-
const objectStore = upgradeDb.createObjectStore(
185-
'fcm_token_object_Store',
186-
{
187-
keyPath: 'swScope'
188-
}
189-
);
190-
objectStore.createIndex('fcmSenderId', 'fcmSenderId', {
191-
unique: false
192-
});
193-
objectStore.createIndex('fcmToken', 'fcmToken', { unique: true });
182+
const db = await openDB('fcm_token_details_db', version, {
183+
upgrade: (upgradeDb, oldVersion) => {
184+
if (oldVersion === 0) {
185+
const objectStore = upgradeDb.createObjectStore(
186+
'fcm_token_object_Store',
187+
{
188+
keyPath: 'swScope'
189+
}
190+
);
191+
objectStore.createIndex('fcmSenderId', 'fcmSenderId', {
192+
unique: false
193+
});
194+
objectStore.createIndex('fcmToken', 'fcmToken', { unique: true });
195+
}
194196
}
195197
});
196198

197199
try {
198200
const tx = db.transaction('fcm_token_object_Store', 'readwrite');
199201
await tx.objectStore('fcm_token_object_Store').put(value);
200-
await tx.complete;
202+
await tx.done;
201203
} finally {
202204
db.close();
203205
}

packages/messaging/src/helpers/migrate-old-database.ts

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { deleteDb, openDb } from 'idb';
18+
import { deleteDB, openDB } from 'idb';
1919

2020
import { TokenDetails } from '../interfaces/token-details';
2121
import { arrayToBase64 } from './array-base64-translator';
@@ -88,83 +88,85 @@ export async function migrateOldDatabase(
8888

8989
let tokenDetails: TokenDetails | null = null;
9090

91-
const db = await openDb(OLD_DB_NAME, OLD_DB_VERSION, async db => {
92-
if (db.oldVersion < 2) {
93-
// Database too old, skip migration.
94-
return;
95-
}
96-
97-
if (!db.objectStoreNames.contains(OLD_OBJECT_STORE_NAME)) {
98-
// Database did not exist. Nothing to do.
99-
return;
100-
}
101-
102-
const objectStore = db.transaction.objectStore(OLD_OBJECT_STORE_NAME);
103-
const value = await objectStore.index('fcmSenderId').get(senderId);
104-
await objectStore.clear();
91+
const db = await openDB(OLD_DB_NAME, OLD_DB_VERSION, {
92+
upgrade: async (db, oldVersion, _newVersion, transaction) => {
93+
if (oldVersion < 2) {
94+
// Database too old, skip migration.
95+
return;
96+
}
10597

106-
if (!value) {
107-
// No entry in the database, nothing to migrate.
108-
return;
109-
}
98+
if (!db.objectStoreNames.contains(OLD_OBJECT_STORE_NAME)) {
99+
// Database did not exist. Nothing to do.
100+
return;
101+
}
110102

111-
if (db.oldVersion === 2) {
112-
const oldDetails = value as V2TokenDetails;
103+
const objectStore = transaction.objectStore(OLD_OBJECT_STORE_NAME);
104+
const value = await objectStore.index('fcmSenderId').get(senderId);
105+
await objectStore.clear();
113106

114-
if (!oldDetails.auth || !oldDetails.p256dh || !oldDetails.endpoint) {
107+
if (!value) {
108+
// No entry in the database, nothing to migrate.
115109
return;
116110
}
117111

118-
tokenDetails = {
119-
token: oldDetails.fcmToken,
120-
createTime: oldDetails.createTime ?? Date.now(),
121-
subscriptionOptions: {
122-
auth: oldDetails.auth,
123-
p256dh: oldDetails.p256dh,
124-
endpoint: oldDetails.endpoint,
125-
swScope: oldDetails.swScope,
126-
vapidKey:
127-
typeof oldDetails.vapidKey === 'string'
128-
? oldDetails.vapidKey
129-
: arrayToBase64(oldDetails.vapidKey)
130-
}
131-
};
132-
} else if (db.oldVersion === 3) {
133-
const oldDetails = value as V3TokenDetails;
134-
135-
tokenDetails = {
136-
token: oldDetails.fcmToken,
137-
createTime: oldDetails.createTime,
138-
subscriptionOptions: {
139-
auth: arrayToBase64(oldDetails.auth),
140-
p256dh: arrayToBase64(oldDetails.p256dh),
141-
endpoint: oldDetails.endpoint,
142-
swScope: oldDetails.swScope,
143-
vapidKey: arrayToBase64(oldDetails.vapidKey)
144-
}
145-
};
146-
} else if (db.oldVersion === 4) {
147-
const oldDetails = value as V4TokenDetails;
148-
149-
tokenDetails = {
150-
token: oldDetails.fcmToken,
151-
createTime: oldDetails.createTime,
152-
subscriptionOptions: {
153-
auth: arrayToBase64(oldDetails.auth),
154-
p256dh: arrayToBase64(oldDetails.p256dh),
155-
endpoint: oldDetails.endpoint,
156-
swScope: oldDetails.swScope,
157-
vapidKey: arrayToBase64(oldDetails.vapidKey)
112+
if (oldVersion === 2) {
113+
const oldDetails = value as V2TokenDetails;
114+
115+
if (!oldDetails.auth || !oldDetails.p256dh || !oldDetails.endpoint) {
116+
return;
158117
}
159-
};
118+
119+
tokenDetails = {
120+
token: oldDetails.fcmToken,
121+
createTime: oldDetails.createTime ?? Date.now(),
122+
subscriptionOptions: {
123+
auth: oldDetails.auth,
124+
p256dh: oldDetails.p256dh,
125+
endpoint: oldDetails.endpoint,
126+
swScope: oldDetails.swScope,
127+
vapidKey:
128+
typeof oldDetails.vapidKey === 'string'
129+
? oldDetails.vapidKey
130+
: arrayToBase64(oldDetails.vapidKey)
131+
}
132+
};
133+
} else if (oldVersion === 3) {
134+
const oldDetails = value as V3TokenDetails;
135+
136+
tokenDetails = {
137+
token: oldDetails.fcmToken,
138+
createTime: oldDetails.createTime,
139+
subscriptionOptions: {
140+
auth: arrayToBase64(oldDetails.auth),
141+
p256dh: arrayToBase64(oldDetails.p256dh),
142+
endpoint: oldDetails.endpoint,
143+
swScope: oldDetails.swScope,
144+
vapidKey: arrayToBase64(oldDetails.vapidKey)
145+
}
146+
};
147+
} else if (oldVersion === 4) {
148+
const oldDetails = value as V4TokenDetails;
149+
150+
tokenDetails = {
151+
token: oldDetails.fcmToken,
152+
createTime: oldDetails.createTime,
153+
subscriptionOptions: {
154+
auth: arrayToBase64(oldDetails.auth),
155+
p256dh: arrayToBase64(oldDetails.p256dh),
156+
endpoint: oldDetails.endpoint,
157+
swScope: oldDetails.swScope,
158+
vapidKey: arrayToBase64(oldDetails.vapidKey)
159+
}
160+
};
161+
}
160162
}
161163
});
162164
db.close();
163165

164166
// Delete all old databases.
165-
await deleteDb(OLD_DB_NAME);
166-
await deleteDb('fcm_vapid_details_db');
167-
await deleteDb('undefined');
167+
await deleteDB(OLD_DB_NAME);
168+
await deleteDB('fcm_vapid_details_db');
169+
await deleteDB('undefined');
168170

169171
return checkTokenDetails(tokenDetails) ? tokenDetails : null;
170172
}

packages/messaging/src/internals/idb-manager.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { DB, deleteDb, openDb } from 'idb';
18+
import { IDBPDatabase, deleteDB, openDB } from 'idb';
1919

2020
import { FirebaseInternalDependencies } from '../interfaces/internal-dependencies';
2121
import { TokenDetails } from '../interfaces/token-details';
@@ -26,17 +26,19 @@ export const DATABASE_NAME = 'firebase-messaging-database';
2626
const DATABASE_VERSION = 1;
2727
const OBJECT_STORE_NAME = 'firebase-messaging-store';
2828

29-
let dbPromise: Promise<DB> | null = null;
30-
function getDbPromise(): Promise<DB> {
29+
let dbPromise: Promise<IDBPDatabase> | null = null;
30+
function getDbPromise(): Promise<IDBPDatabase> {
3131
if (!dbPromise) {
32-
dbPromise = openDb(DATABASE_NAME, DATABASE_VERSION, upgradeDb => {
33-
// We don't use 'break' in this switch statement, the fall-through behavior is what we want,
34-
// because if there are multiple versions between the old version and the current version, we
35-
// want ALL the migrations that correspond to those versions to run, not only the last one.
36-
// eslint-disable-next-line default-case
37-
switch (upgradeDb.oldVersion) {
38-
case 0:
39-
upgradeDb.createObjectStore(OBJECT_STORE_NAME);
32+
dbPromise = openDB(DATABASE_NAME, DATABASE_VERSION, {
33+
upgrade: (upgradeDb, oldVersion) => {
34+
// We don't use 'break' in this switch statement, the fall-through behavior is what we want,
35+
// because if there are multiple versions between the old version and the current version, we
36+
// want ALL the migrations that correspond to those versions to run, not only the last one.
37+
// eslint-disable-next-line default-case
38+
switch (oldVersion) {
39+
case 0:
40+
upgradeDb.createObjectStore(OBJECT_STORE_NAME);
41+
}
4042
}
4143
});
4244
}
@@ -77,7 +79,7 @@ export async function dbSet(
7779
const db = await getDbPromise();
7880
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
7981
await tx.objectStore(OBJECT_STORE_NAME).put(tokenDetails, key);
80-
await tx.complete;
82+
await tx.done;
8183
return tokenDetails;
8284
}
8385

@@ -89,14 +91,14 @@ export async function dbRemove(
8991
const db = await getDbPromise();
9092
const tx = db.transaction(OBJECT_STORE_NAME, 'readwrite');
9193
await tx.objectStore(OBJECT_STORE_NAME).delete(key);
92-
await tx.complete;
94+
await tx.done;
9395
}
9496

9597
/** Deletes the DB. Useful for tests. */
9698
export async function dbDelete(): Promise<void> {
9799
if (dbPromise) {
98100
(await dbPromise).close();
99-
await deleteDb(DATABASE_NAME);
101+
await deleteDB(DATABASE_NAME);
100102
dbPromise = null;
101103
}
102104
}

packages/messaging/src/testing/setup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import chaiAsPromised from 'chai-as-promised';
1919
import sinonChai from 'sinon-chai';
2020

2121
import { dbDelete } from '../internals/idb-manager';
22-
import { deleteDb } from 'idb';
22+
import { deleteDB } from 'idb';
2323
import { restore } from 'sinon';
2424
import { use } from 'chai';
2525

@@ -29,5 +29,5 @@ use(sinonChai);
2929
afterEach(async () => {
3030
restore();
3131
await dbDelete();
32-
await deleteDb('fcm_token_details_db');
32+
await deleteDB('fcm_token_details_db');
3333
});

0 commit comments

Comments
 (0)