Skip to content

Commit 3e64a82

Browse files
Support downgrades
1 parent 062a6c1 commit 3e64a82

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLitePersistence.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -320,18 +320,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
320320
@Override
321321
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
322322
ensureConfigured(db);
323-
324-
// For now, we can safely do nothing.
325-
//
326-
// The only case that's possible at this point would be to downgrade from version 1 (present
327-
// in our first released version) to 0 (uninstalled). Nobody would want us to just wipe the
328-
// data so instead we just keep it around in the hope that they'll upgrade again :-).
329-
//
330-
// Note that if you uninstall a Firestore-based app, the database goes away completely. The
331-
// downgrade-then-upgrade case can only happen in very limited circumstances.
332-
//
333-
// We'll have to revisit this once we ship a migration past version 1, but this will
334-
// definitely be good enough for our initial launch.
323+
new SQLiteSchema(db, serializer).runMigrations(oldVersion);
335324
}
336325

337326
@Override

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteSchema.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,11 @@ void runMigrations(int fromVersion, int toVersion) {
153153
dropLastLimboFreeSnapshotVersion();
154154
}
155155

156+
// Schema version 11 changed the format of canonical IDs in the target cache.
156157
if (fromVersion < 11 && toVersion >= 11) {
157-
// Schema version 11 changed the format of canonical IDs in the target cache.
158+
rewriteCanonicalIds();
159+
} else if (fromVersion >= 11 && toVersion < 11) {
160+
// Rewrite the canonical IDs in the old format.
158161
rewriteCanonicalIds();
159162
}
160163

firebase-firestore/src/test/java/com/google/firebase/firestore/local/SQLiteSchemaTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,19 @@ public void rewritesCanonicalIds() {
552552
});
553553

554554
schema.runMigrations(10, 11);
555+
assertTargetIds();
556+
557+
// Simulate a downgrade that requires a canonical ID migration.
558+
db.execSQL("UPDATE targets SET canonical_id = ?", new Object[] {"invalid_canonical_id"});
559+
schema.runMigrations(11, 10);
560+
assertTargetIds();
561+
}
562+
563+
/**
564+
* Evaluates the data in the `targets` table to make sure that the canonical ID matches the
565+
* current format.
566+
*/
567+
private void assertTargetIds() {
555568
new SQLitePersistence.Query(db, "SELECT canonical_id, target_proto, canonical_id FROM targets")
556569
.forEach(
557570
cursor -> {

0 commit comments

Comments
 (0)