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