26
26
import androidx .annotation .VisibleForTesting ;
27
27
import com .google .common .base .Preconditions ;
28
28
import com .google .firebase .firestore .model .ResourcePath ;
29
+ import com .google .firebase .firestore .util .BackgroundQueue ;
29
30
import com .google .firebase .firestore .util .Consumer ;
31
+ import com .google .firebase .firestore .util .Executors ;
32
+
30
33
import java .util .ArrayList ;
31
34
import java .util .List ;
32
35
@@ -45,15 +48,8 @@ class SQLiteSchema {
45
48
/**
46
49
* The version of the schema. Increase this by one for each migration added to runMigrations
47
50
* below.
48
- *
49
- * <p>TODO(index-free): The migration to schema version 9 doesn't backfill `read_time` as this
50
- * requires rewriting the RemoteDocumentCache. For index-free queries to efficiently handle
51
- * existing documents, we still need to populate read_time for all existing entries, drop the
52
- * RemoteDocumentCache or ask users to invoke `clearPersistence()` manually. If we decide to
53
- * backfill or drop the contents of the RemoteDocumentCache, we need to perform an additional
54
- * schema migration.
55
51
*/
56
- static final int VERSION = 9 ;
52
+ static final int VERSION = 10 ;
57
53
58
54
// Remove this constant and increment VERSION to enable indexing support
59
55
static final int INDEXING_SUPPORT_VERSION = VERSION + 1 ;
@@ -74,11 +70,11 @@ class SQLiteSchema {
74
70
this .db = db ;
75
71
}
76
72
77
- void runMigrations () {
73
+ int runMigrations () {
78
74
runMigrations (0 , VERSION );
79
75
}
80
76
81
- void runMigrations (int fromVersion ) {
77
+ int runMigrations (int fromVersion ) {
82
78
runMigrations (fromVersion , VERSION );
83
79
}
84
80
@@ -89,7 +85,9 @@ void runMigrations(int fromVersion) {
89
85
* @param toVersion The version the database is migrating to. Usually VERSION, but can be
90
86
* otherwise for testing.
91
87
*/
92
- void runMigrations (int fromVersion , int toVersion ) {
88
+ int runMigrations (int fromVersion , int toVersion ) {
89
+ int finalVersion = fromVersion ;
90
+
93
91
/*
94
92
* New migrations should be added at the end of the series of `if` statements and should follow
95
93
* the pattern. Make sure to increment `VERSION` and to read the comment below about
@@ -100,6 +98,7 @@ void runMigrations(int fromVersion, int toVersion) {
100
98
createV1MutationQueue ();
101
99
createV1QueryCache ();
102
100
createV1RemoteDocumentCache ();
101
+ finalVersion = 1 ;
103
102
}
104
103
105
104
// Migration 2 to populate the target_globals table no longer needed since migration 3
@@ -112,31 +111,49 @@ void runMigrations(int fromVersion, int toVersion) {
112
111
dropV1QueryCache ();
113
112
createV1QueryCache ();
114
113
}
114
+ finalVersion = 3 ;
115
115
}
116
116
117
117
if (fromVersion < 4 && toVersion >= 4 ) {
118
118
ensureTargetGlobal ();
119
119
addTargetCount ();
120
+ finalVersion = 4 ;
120
121
}
121
122
122
123
if (fromVersion < 5 && toVersion >= 5 ) {
123
124
addSequenceNumber ();
125
+ finalVersion = 5 ;
124
126
}
125
127
126
128
if (fromVersion < 6 && toVersion >= 6 ) {
127
129
removeAcknowledgedMutations ();
130
+ finalVersion = 6 ;
128
131
}
129
132
130
133
if (fromVersion < 7 && toVersion >= 7 ) {
131
134
ensureSequenceNumbers ();
135
+ finalVersion = 7 ;
132
136
}
133
137
134
138
if (fromVersion < 8 && toVersion >= 8 ) {
135
139
createV8CollectionParentsIndex ();
140
+ finalVersion = 8 ;
136
141
}
137
142
138
143
if (fromVersion < 9 && toVersion >= 9 ) {
139
144
addReadTime ();
145
+ finalVersion = 9 ;
146
+ }
147
+
148
+ if (fromVersion < 10 && toVersion >= 10 ) {
149
+ // Migration 10 slowly populates the read time. If no documents lack read time, we mark the migration as complete.
150
+ if (!hasDocumentsWithoutReadTime ()) {
151
+ return 10 ;
152
+ }
153
+
154
+ Executors .BACKGROUND_EXECUTOR .execute (this ::populateReadTime );
155
+
156
+ return 9 ;
140
157
}
141
158
142
159
/*
@@ -158,6 +175,10 @@ void runMigrations(int fromVersion, int toVersion) {
158
175
}
159
176
}
160
177
178
+ private void populateReadTime () {
179
+
180
+ }
181
+
161
182
/**
162
183
* Used to assert that a set of tables either all exist or not. The supplied function is run if
163
184
* none of the tables exist. Use this method to create a set of tables at once.
0 commit comments