|
9 | 9 |
|
10 | 10 | import javax.annotation.Nullable;
|
11 | 11 |
|
| 12 | +import java.io.File; |
12 | 13 | import java.util.Arrays;
|
13 | 14 | import java.util.Iterator;
|
14 | 15 |
|
15 | 16 | import android.content.ContentValues;
|
| 17 | +import android.content.Context; |
16 | 18 | import android.database.Cursor;
|
17 | 19 | import android.database.sqlite.SQLiteDatabase;
|
| 20 | +import android.os.Build; |
18 | 21 | import android.text.TextUtils;
|
| 22 | +import android.util.Log; |
19 | 23 |
|
20 | 24 | import com.facebook.react.bridge.ReadableArray;
|
21 | 25 |
|
@@ -142,4 +146,36 @@ private static void deepMergeInto(JSONObject oldJSON, JSONObject newJSON)
|
142 | 146 | }
|
143 | 147 | }
|
144 | 148 | }
|
| 149 | + /** |
| 150 | + * From Pie and up, Android started to use Write-ahead logging (WAL), instead of journal rollback |
| 151 | + * for atomic commits and rollbacks. |
| 152 | + * Basically, WAL does not write directly to the database file, rather to the supporting WAL file. |
| 153 | + * Because of that, migration to the next storage might not be successful, because the content of |
| 154 | + * RKStorage might be still in WAL file instead. Committing all data from WAL to db file is called |
| 155 | + * a "checkpoint" and is done automatically (by default) when the WAL file reaches a threshold |
| 156 | + * size of 1000 pages. |
| 157 | + * More here: https://sqlite.org/wal.html |
| 158 | + * <p> |
| 159 | + * This helper will force checkpoint on RKStorage, if Next storage file does not exists yet. |
| 160 | + */ |
| 161 | + public static void verifyAndForceSqliteCheckpoint(Context ctx) { |
| 162 | + File nextStorageFile = ctx.getDatabasePath("AsyncStorage"); |
| 163 | + File currentStorageFile = ctx.getDatabasePath(ReactDatabaseSupplier.DATABASE_NAME); |
| 164 | + boolean isCheckpointRequired = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P |
| 165 | + && !nextStorageFile.exists() |
| 166 | + && currentStorageFile.exists(); |
| 167 | + if (!isCheckpointRequired) { |
| 168 | + Log.i("AsyncStorage_Next", "SQLite checkpoint not required."); |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + try { |
| 173 | + ReactDatabaseSupplier supplier = ReactDatabaseSupplier.getInstance(ctx); |
| 174 | + supplier.get().execSQL("PRAGMA wal_checkpoint"); |
| 175 | + supplier.closeDatabase(); |
| 176 | + Log.i("AsyncStorage_Next", "Forcing SQLite checkpoint successful."); |
| 177 | + } catch (Exception e) { |
| 178 | + Log.w("AsyncStorage_Next", "Could not force checkpoint on RKStorage: " + e.getMessage()); |
| 179 | + } |
| 180 | + } |
145 | 181 | }
|
0 commit comments