@@ -24,10 +24,11 @@ import {
24
24
DbTarget ,
25
25
DbTargetGlobal ,
26
26
DbTargetGlobalKey ,
27
- DbTargetKey
27
+ DbTargetKey ,
28
+ DbTimestamp
28
29
} from '../../../src/local/indexeddb_schema' ;
29
30
import { SimpleDb , SimpleDbTransaction } from '../../../src/local/simple_db' ;
30
- import { PersistencePromise } from '../../../src/local/persistence_promise ' ;
31
+ import { SnapshotVersion } from '../../../src/core/snapshot_version ' ;
31
32
32
33
const INDEXEDDB_TEST_DATABASE = 'schemaTest' ;
33
34
@@ -71,17 +72,6 @@ function getAllObjectStores(db: IDBDatabase): string[] {
71
72
return objectStores ;
72
73
}
73
74
74
- function getTargetCount ( db : IDBDatabase ) : Promise < number > {
75
- const sdb = new SimpleDb ( db ) ;
76
- return sdb
77
- . runTransaction ( 'readonly' , [ DbTargetGlobal . store ] , txn =>
78
- txn
79
- . store < DbTargetGlobalKey , DbTargetGlobal > ( DbTargetGlobal . store )
80
- . get ( DbTargetGlobal . key )
81
- )
82
- . then ( metadata => metadata . targetCount ) ;
83
- }
84
-
85
75
describe ( 'IndexedDbSchema: createOrUpgradeDb' , ( ) => {
86
76
if ( ! IndexedDbPersistence . isAvailable ( ) ) {
87
77
console . warn ( 'No IndexedDB. Skipping createOrUpgradeDb() tests.' ) ;
@@ -101,58 +91,35 @@ describe('IndexedDbSchema: createOrUpgradeDb', () => {
101
91
} ) ;
102
92
} ) ;
103
93
104
- it ( 'can install schema version 2' , ( ) => {
105
- return withDb ( 2 , db => {
106
- expect ( db . version ) . to . equal ( 2 ) ;
107
- // We should have all of the stores, we should have the target global row
108
- // and we should not have any targets counted, because there are none.
109
- expect ( getAllObjectStores ( db ) ) . to . have . members ( ALL_STORES ) ;
110
- // Check the target count. We haven't added any targets, so we expect 0.
111
- return getTargetCount ( db ) . then ( targetCount => {
112
- expect ( targetCount ) . to . equal ( 0 ) ;
113
- } ) ;
114
- } ) ;
115
- } ) ;
116
-
117
- it ( 'can upgrade from schema version 1 to 2' , ( ) => {
118
- const expectedTargetCount = 5 ;
119
- return withDb ( 1 , db => {
120
- const sdb = new SimpleDb ( db ) ;
121
- // Now that we have all of the stores, add some targets so the next
122
- // migration can count them.
123
- return sdb . runTransaction ( 'readwrite' , [ DbTarget . store ] , txn => {
124
- const store = txn . store ( DbTarget . store ) ;
125
- let p = PersistencePromise . resolve ( ) ;
126
- for ( let i = 0 ; i < expectedTargetCount ; i ++ ) {
127
- p = p . next ( ( ) => store . put ( { targetId : i } ) ) ;
128
- }
129
- return p ;
130
- } ) ;
131
- } ) . then ( ( ) =>
132
- withDb ( 2 , db => {
133
- expect ( db . version ) . to . equal ( 2 ) ;
134
- expect ( getAllObjectStores ( db ) ) . to . have . members ( ALL_STORES ) ;
135
- return getTargetCount ( db ) . then ( targetCount => {
136
- expect ( targetCount ) . to . equal ( expectedTargetCount ) ;
137
- } ) ;
138
- } )
139
- ) ;
140
- } ) ;
141
-
142
94
it ( 'drops the query cache from 2 to 3' , ( ) => {
143
95
const userId = 'user' ;
144
96
const batchId = 1 ;
145
97
const targetId = 2 ;
146
98
147
99
const expectedMutation = new DbMutationBatch ( userId , batchId , 1000 , [ ] ) ;
100
+ const dummyTargetGlobal = new DbTargetGlobal (
101
+ /*highestTargetId=*/ 1 ,
102
+ /*highestListenSequencNumber=*/ 1 ,
103
+ /*lastRemoteSnapshotVersion=*/ new DbTimestamp ( 1 , 1 ) ,
104
+ /*targetCount=*/ 1
105
+ ) ;
106
+ const resetTargetGlobal = new DbTargetGlobal (
107
+ /*highestTargetId=*/ 0 ,
108
+ /*highestListenSequencNumber=*/ 0 ,
109
+ /*lastRemoteSnapshotVersion=*/ SnapshotVersion . MIN . toTimestamp ( ) ,
110
+ /*targetCount=*/ 0
111
+ ) ;
148
112
149
113
return withDb ( 2 , db => {
150
114
const sdb = new SimpleDb ( db ) ;
151
115
return sdb . runTransaction (
152
116
'readwrite' ,
153
- [ DbTarget . store , DbMutationBatch . store ] ,
117
+ [ DbTarget . store , DbTargetGlobal . store , DbMutationBatch . store ] ,
154
118
txn => {
155
119
const targets = txn . store < DbTargetKey , DbTarget > ( DbTarget . store ) ;
120
+ const targetGlobal = txn . store < DbTargetGlobalKey , DbTargetGlobal > (
121
+ DbTargetGlobal . store
122
+ ) ;
156
123
const mutations = txn . store < DbMutationBatchKey , DbMutationBatch > (
157
124
DbMutationBatch . store
158
125
) ;
@@ -161,6 +128,9 @@ describe('IndexedDbSchema: createOrUpgradeDb', () => {
161
128
targets
162
129
// tslint:disable-next-line:no-any
163
130
. put ( { targetId, canonicalId : 'foo' } as any )
131
+ . next ( ( ) =>
132
+ targetGlobal . put ( DbTargetGlobal . key , dummyTargetGlobal )
133
+ )
164
134
. next ( ( ) => mutations . put ( expectedMutation ) )
165
135
) ;
166
136
}
@@ -173,9 +143,12 @@ describe('IndexedDbSchema: createOrUpgradeDb', () => {
173
143
const sdb = new SimpleDb ( db ) ;
174
144
return sdb . runTransaction (
175
145
'readwrite' ,
176
- [ DbTarget . store , DbMutationBatch . store ] ,
146
+ [ DbTarget . store , DbTargetGlobal . store , DbMutationBatch . store ] ,
177
147
txn => {
178
148
const targets = txn . store < DbTargetKey , DbTarget > ( DbTarget . store ) ;
149
+ const targetGlobal = txn . store < DbTargetGlobalKey , DbTargetGlobal > (
150
+ DbTargetGlobal . store
151
+ ) ;
179
152
const mutations = txn . store < DbMutationBatchKey , DbMutationBatch > (
180
153
DbMutationBatch . store
181
154
) ;
@@ -186,6 +159,14 @@ describe('IndexedDbSchema: createOrUpgradeDb', () => {
186
159
// The target should have been dropped
187
160
expect ( target ) . to . be . null ;
188
161
} )
162
+ . next ( ( ) => targetGlobal . get ( DbTargetGlobal . key ) )
163
+ . next ( targetGlobalEntry => {
164
+ // Target Global should exist but be cleared.
165
+ // HACK: round-trip through JSON to clear types, like IndexedDb
166
+ // does.
167
+ const expected = JSON . parse ( JSON . stringify ( resetTargetGlobal ) ) ;
168
+ expect ( targetGlobalEntry ) . to . deep . equal ( expected ) ;
169
+ } )
189
170
. next ( ( ) => mutations . get ( [ userId , batchId ] ) )
190
171
. next ( mutation => {
191
172
// Mutations should be unaffected.
0 commit comments