16
16
17
17
import static com .google .firebase .firestore .local .EncodedPath .decodeResourcePath ;
18
18
import static com .google .firebase .firestore .local .EncodedPath .encode ;
19
+ import static com .google .firebase .firestore .testutil .TestUtil .filter ;
19
20
import static com .google .firebase .firestore .testutil .TestUtil .key ;
20
21
import static com .google .firebase .firestore .testutil .TestUtil .map ;
21
22
import static com .google .firebase .firestore .testutil .TestUtil .path ;
33
34
import android .database .sqlite .SQLiteOpenHelper ;
34
35
import androidx .test .core .app .ApplicationProvider ;
35
36
import com .google .firebase .database .collection .ImmutableSortedMap ;
37
+ import com .google .firebase .firestore .core .Query ;
36
38
import com .google .firebase .firestore .model .DatabaseId ;
37
39
import com .google .firebase .firestore .model .DocumentKey ;
38
40
import com .google .firebase .firestore .model .ResourcePath ;
@@ -68,8 +70,13 @@ public class SQLiteSchemaTest {
68
70
private SQLiteSchema schema ;
69
71
private SQLiteOpenHelper opener ;
70
72
73
+ private DatabaseId databaseId ;
74
+ private LocalSerializer serializer ;
75
+
71
76
@ Before
72
77
public void setUp () {
78
+ databaseId = DatabaseId .forProject ("foo" );
79
+ serializer = new LocalSerializer (new RemoteSerializer (databaseId ));
73
80
opener =
74
81
new SQLiteOpenHelper (ApplicationProvider .getApplicationContext (), "foo" , null , 1 ) {
75
82
@ Override
@@ -79,7 +86,7 @@ public void onCreate(SQLiteDatabase db) {}
79
86
public void onUpgrade (SQLiteDatabase db , int oldVersion , int newVersion ) {}
80
87
};
81
88
db = opener .getWritableDatabase ();
82
- schema = new SQLiteSchema (db );
89
+ schema = new SQLiteSchema (db , serializer );
83
90
}
84
91
85
92
@ After
@@ -521,14 +528,46 @@ public void keepsLastLimboFreeSnapshotIfNotDowngraded() {
521
528
Target targetProto = Target .parseFrom (targetProtoBytes );
522
529
assertTrue (targetProto .hasLastLimboFreeSnapshotVersion ());
523
530
} catch (InvalidProtocolBufferException e ) {
524
- fail ("Failed to decode Query data" );
531
+ fail ("Failed to decode Target data" );
532
+ }
533
+ });
534
+ }
535
+
536
+ @ Test
537
+ public void rewritesCanonicalIds () {
538
+ schema .runMigrations (0 , 10 );
539
+
540
+ Query filteredQuery = query ("colletion" ).filter (filter ("foo" , "==" , "bar" ));
541
+ TargetData initialTargetData =
542
+ new TargetData (
543
+ filteredQuery .toTarget (),
544
+ /* targetId= */ 2 ,
545
+ /* sequenceNumber= */ 1 ,
546
+ QueryPurpose .LISTEN );
547
+
548
+ db .execSQL (
549
+ "INSERT INTO targets (target_id, canonical_id, target_proto) VALUES (?,?, ?)" ,
550
+ new Object [] {2 , "foo" , serializer .encodeTargetData (initialTargetData ).toByteArray ()});
551
+
552
+ schema .runMigrations (10 , 11 );
553
+ new SQLitePersistence .Query (db , "SELECT canonical_id, target_proto, canonical_id FROM targets" )
554
+ .forEach (
555
+ cursor -> {
556
+ String actualCanonicalId = cursor .getString (0 );
557
+ byte [] targetProtoBytes = cursor .getBlob (1 );
558
+
559
+ try {
560
+ Target targetProto = Target .parseFrom (targetProtoBytes );
561
+ TargetData targetData = serializer .decodeTargetData (targetProto );
562
+ String expectedCanonicalId = targetData .getTarget ().getCanonicalId ();
563
+ assertEquals (actualCanonicalId , expectedCanonicalId );
564
+ } catch (InvalidProtocolBufferException e ) {
565
+ fail ("Failed to decode Target data" );
525
566
}
526
567
});
527
568
}
528
569
529
570
private SQLiteRemoteDocumentCache createRemoteDocumentCache () {
530
- DatabaseId databaseId = DatabaseId .forProject ("foo" );
531
- LocalSerializer serializer = new LocalSerializer (new RemoteSerializer (databaseId ));
532
571
SQLitePersistence persistence =
533
572
new SQLitePersistence (serializer , LruGarbageCollector .Params .Default (), opener );
534
573
persistence .start ();
0 commit comments