-
Notifications
You must be signed in to change notification settings - Fork 475
Created Android scoped storage migration method #559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
EvanBacon
merged 9 commits into
react-native-async-storage:master
from
EvanBacon:@evanbacon/android/expo-migration-script
Apr 2, 2021
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e4b1e17
Created Android migration script
EvanBacon ac4fade
Update AsyncStorageMigration.java
EvanBacon 6fbaf4c
Removed delete step
EvanBacon ededf89
Revert "Removed delete step"
EvanBacon 443e64f
[feedback] Remove private context
EvanBacon 647b402
[feedback] inline statement
EvanBacon e99829f
Update AsyncStorageMigration.java
EvanBacon 00ca567
rename migration module
EvanBacon f3be28d
[feedback] Use correct ReactDatabaseSupplier
EvanBacon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStorageExpoMigration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package com.reactnativecommunity.asyncstorage; | ||
|
||
import android.content.Context; | ||
import android.os.Build; | ||
import android.util.Log; | ||
|
||
import androidx.annotation.RequiresApi; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.file.Files; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.util.ArrayList; | ||
|
||
// A utility class that migrates a scoped AsyncStorage database to RKStorage. | ||
// This utility only runs if the RKStorage file has not been created yet. | ||
public class AsyncStorageExpoMigration { | ||
static final String LOG_TAG = "AsyncStorageExpoMigration"; | ||
|
||
public static void migrate(Context context) { | ||
// Only migrate if the default async storage file does not exist. | ||
if (isAsyncStorageDatabaseCreated(context)) { | ||
return; | ||
} | ||
|
||
ArrayList<File> expoDatabases = getExpoDatabases(context); | ||
|
||
File expoDatabase = getLastModifiedFile(expoDatabases); | ||
|
||
if (expoDatabase == null) { | ||
Log.v(LOG_TAG, "No scoped database found"); | ||
return; | ||
} | ||
|
||
try { | ||
// Create the storage file | ||
ReactDatabaseSupplier.getInstance(context).get(); | ||
copyFile(new FileInputStream(expoDatabase), new FileOutputStream(context.getDatabasePath(ReactDatabaseSupplier.DATABASE_NAME))); | ||
Log.v(LOG_TAG, "Migrated most recently modified database " + expoDatabase.getName() + " to RKStorage"); | ||
} catch (Exception e) { | ||
Log.v(LOG_TAG, "Failed to migrate scoped database " + expoDatabase.getName()); | ||
e.printStackTrace(); | ||
return; | ||
} | ||
|
||
try { | ||
for (File file : expoDatabases) { | ||
if (file.delete()) { | ||
Log.v(LOG_TAG, "Deleted scoped database " + file.getName()); | ||
} else { | ||
Log.v(LOG_TAG, "Failed to delete scoped database " + file.getName()); | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
Log.v(LOG_TAG, "Completed the scoped AsyncStorage migration"); | ||
} | ||
|
||
private static boolean isAsyncStorageDatabaseCreated(Context context) { | ||
return context.getDatabasePath(ReactDatabaseSupplier.DATABASE_NAME).exists(); | ||
} | ||
|
||
// Find all database files that the user may have created while using Expo. | ||
private static ArrayList<File> getExpoDatabases(Context context) { | ||
ArrayList<File> scopedDatabases = new ArrayList<>(); | ||
try { | ||
File databaseDirectory = context.getDatabasePath("noop").getParentFile(); | ||
File[] directoryListing = databaseDirectory.listFiles(); | ||
if (directoryListing != null) { | ||
for (File child : directoryListing) { | ||
// Find all databases matching the Expo scoped key, and skip any database journals. | ||
if (child.getName().startsWith("RKStorage-scoped-experience-") && !child.getName().endsWith("-journal")) { | ||
scopedDatabases.add(child); | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
// Just in case anything happens catch and print, file system rules can tend to be different across vendors. | ||
e.printStackTrace(); | ||
} | ||
return scopedDatabases; | ||
} | ||
|
||
// Returns the most recently modified file. | ||
// If a user publishes an app with Expo, then changes the slug | ||
// and publishes again, a new database will be created. | ||
// We want to select the most recent database and migrate it to RKStorage. | ||
private static File getLastModifiedFile(ArrayList<File> files) { | ||
if (files.size() == 0) { | ||
EvanBacon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return null; | ||
} | ||
long lastMod = -1; | ||
File lastModFile = null; | ||
for (File child : files) { | ||
long modTime = getLastModifiedTimeInMillis(child); | ||
if (modTime > lastMod) { | ||
lastMod = modTime; | ||
lastModFile = child; | ||
} | ||
} | ||
if (lastModFile != null) { | ||
return lastModFile; | ||
} | ||
|
||
return files.get(0); | ||
} | ||
|
||
private static long getLastModifiedTimeInMillis(File file) { | ||
try { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
return getLastModifiedTimeFromBasicFileAttrs(file); | ||
} else { | ||
return file.lastModified(); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return -1; | ||
} | ||
} | ||
|
||
@RequiresApi(Build.VERSION_CODES.O) | ||
private static long getLastModifiedTimeFromBasicFileAttrs(File file) { | ||
try { | ||
return Files.readAttributes(file.toPath(), BasicFileAttributes.class).creationTime().toMillis(); | ||
} catch (Exception e) { | ||
return -1; | ||
} | ||
} | ||
|
||
private static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException { | ||
FileChannel fromChannel = null; | ||
FileChannel toChannel = null; | ||
try { | ||
fromChannel = fromFile.getChannel(); | ||
toChannel = toFile.getChannel(); | ||
fromChannel.transferTo(0, fromChannel.size(), toChannel); | ||
} finally { | ||
try { | ||
if (fromChannel != null) { | ||
fromChannel.close(); | ||
} | ||
} finally { | ||
if (toChannel != null) { | ||
toChannel.close(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.