@@ -88,83 +88,79 @@ export async function migrateOldDatabase(
88
88
89
89
let tokenDetails : TokenDetails | null = null ;
90
90
91
- const db = await openDB (
92
- OLD_DB_NAME ,
93
- OLD_DB_VERSION ,
94
- {
95
- upgrade : async ( db , oldVersion , newVersion , upgradeTransaction ) => {
96
- if ( oldVersion < 2 ) {
97
- // Database too old, skip migration.
98
- return ;
99
- }
91
+ const db = await openDB ( OLD_DB_NAME , OLD_DB_VERSION , {
92
+ upgrade : async ( db , oldVersion , newVersion , upgradeTransaction ) => {
93
+ if ( oldVersion < 2 ) {
94
+ // Database too old, skip migration.
95
+ return ;
96
+ }
100
97
101
- if ( ! db . objectStoreNames . contains ( OLD_OBJECT_STORE_NAME ) ) {
102
- // Database did not exist. Nothing to do.
103
- return ;
104
- }
98
+ if ( ! db . objectStoreNames . contains ( OLD_OBJECT_STORE_NAME ) ) {
99
+ // Database did not exist. Nothing to do.
100
+ return ;
101
+ }
105
102
106
- const objectStore = upgradeTransaction . objectStore ( OLD_OBJECT_STORE_NAME ) ;
107
- const value = await objectStore . index ( 'fcmSenderId' ) . get ( senderId ) ;
108
- await objectStore . clear ( ) ;
103
+ const objectStore = upgradeTransaction . objectStore ( OLD_OBJECT_STORE_NAME ) ;
104
+ const value = await objectStore . index ( 'fcmSenderId' ) . get ( senderId ) ;
105
+ await objectStore . clear ( ) ;
106
+
107
+ if ( ! value ) {
108
+ // No entry in the database, nothing to migrate.
109
+ return ;
110
+ }
109
111
110
- if ( ! value ) {
111
- // No entry in the database, nothing to migrate.
112
+ if ( oldVersion === 2 ) {
113
+ const oldDetails = value as V2TokenDetails ;
114
+
115
+ if ( ! oldDetails . auth || ! oldDetails . p256dh || ! oldDetails . endpoint ) {
112
116
return ;
113
117
}
114
118
115
- if ( oldVersion === 2 ) {
116
- const oldDetails = value as V2TokenDetails ;
117
-
118
- if ( ! oldDetails . auth || ! oldDetails . p256dh || ! oldDetails . endpoint ) {
119
- return ;
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 )
120
131
}
121
-
122
- tokenDetails = {
123
- token : oldDetails . fcmToken ,
124
- createTime : oldDetails . createTime ?? Date . now ( ) ,
125
- subscriptionOptions : {
126
- auth : oldDetails . auth ,
127
- p256dh : oldDetails . p256dh ,
128
- endpoint : oldDetails . endpoint ,
129
- swScope : oldDetails . swScope ,
130
- vapidKey :
131
- typeof oldDetails . vapidKey === 'string'
132
- ? oldDetails . vapidKey
133
- : arrayToBase64 ( oldDetails . vapidKey )
134
- }
135
- } ;
136
- } else if ( oldVersion === 3 ) {
137
- const oldDetails = value as V3TokenDetails ;
138
-
139
- tokenDetails = {
140
- token : oldDetails . fcmToken ,
141
- createTime : oldDetails . createTime ,
142
- subscriptionOptions : {
143
- auth : arrayToBase64 ( oldDetails . auth ) ,
144
- p256dh : arrayToBase64 ( oldDetails . p256dh ) ,
145
- endpoint : oldDetails . endpoint ,
146
- swScope : oldDetails . swScope ,
147
- vapidKey : arrayToBase64 ( oldDetails . vapidKey )
148
- }
149
- } ;
150
- } else if ( oldVersion === 4 ) {
151
- const oldDetails = value as V4TokenDetails ;
152
-
153
- tokenDetails = {
154
- token : oldDetails . fcmToken ,
155
- createTime : oldDetails . createTime ,
156
- subscriptionOptions : {
157
- auth : arrayToBase64 ( oldDetails . auth ) ,
158
- p256dh : arrayToBase64 ( oldDetails . p256dh ) ,
159
- endpoint : oldDetails . endpoint ,
160
- swScope : oldDetails . swScope ,
161
- vapidKey : arrayToBase64 ( oldDetails . vapidKey )
162
- }
163
- } ;
164
- }
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
+ } ;
165
161
}
166
162
}
167
- ) ;
163
+ } ) ;
168
164
db . close ( ) ;
169
165
170
166
// Delete all old databases.
0 commit comments