|
| 1 | +/** |
| 2 | + * Storage Migration Utility |
| 3 | + * Migrates data from AsyncStorage to MMKV without data loss |
| 4 | + */ |
| 5 | + |
| 6 | +import AsyncStorage from '@react-native-async-storage/async-storage'; |
| 7 | +import { MMKV } from 'react-native-mmkv'; |
| 8 | + |
| 9 | +const MIGRATION_KEY = '@inknest_storage_migrated_v1'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Check if migration has already been completed |
| 13 | + */ |
| 14 | +export async function isMigrationComplete() { |
| 15 | + const migrated = await AsyncStorage.getItem(MIGRATION_KEY); |
| 16 | + return migrated === 'true'; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Mark migration as complete |
| 21 | + */ |
| 22 | +export async function markMigrationComplete() { |
| 23 | + await AsyncStorage.setItem(MIGRATION_KEY, 'true'); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Get all keys from AsyncStorage that match redux-persist pattern |
| 28 | + */ |
| 29 | +async function getReduxPersistKeys() { |
| 30 | + const allKeys = await AsyncStorage.getAllKeys(); |
| 31 | + // Redux-persist stores data with 'persist:' prefix |
| 32 | + return allKeys.filter(key => key.startsWith('persist:')); |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Migrate a single key-value pair from AsyncStorage to MMKV |
| 37 | + */ |
| 38 | +async function migrateKeyToMMKV(key, mmkv) { |
| 39 | + try { |
| 40 | + const value = await AsyncStorage.getItem(key); |
| 41 | + if (value !== null) { |
| 42 | + mmkv.set(key, value); |
| 43 | + return true; |
| 44 | + } |
| 45 | + } catch (error) { |
| 46 | + console.error(`Failed to migrate key ${key}:`, error); |
| 47 | + } |
| 48 | + return false; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Main migration function |
| 53 | + * Call this BEFORE creating the MMKV storage instance for Redux |
| 54 | + * @returns {Promise<boolean>} - true if migration was successful or already done |
| 55 | + */ |
| 56 | +export async function migrateAsyncStorageToMMKV(mmkv) { |
| 57 | + try { |
| 58 | + // Check if already migrated |
| 59 | + const alreadyMigrated = await isMigrationComplete(); |
| 60 | + if (alreadyMigrated) { |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + console.log('Starting AsyncStorage to MMKV migration...'); |
| 65 | + |
| 66 | + // Get all redux-persist keys |
| 67 | + const keysToMigrate = await getReduxPersistKeys(); |
| 68 | + |
| 69 | + if (keysToMigrate.length === 0) { |
| 70 | + console.log('No redux-persist data found in AsyncStorage'); |
| 71 | + await markMigrationComplete(); |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + console.log(`Found ${keysToMigrate.length} keys to migrate`); |
| 76 | + |
| 77 | + // Migrate each key |
| 78 | + let migratedCount = 0; |
| 79 | + for (const key of keysToMigrate) { |
| 80 | + const success = await migrateKeyToMMKV(key, mmkv); |
| 81 | + if (success) { |
| 82 | + migratedCount++; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + console.log(`Successfully migrated ${migratedCount}/${keysToMigrate.length} keys`); |
| 87 | + |
| 88 | + // Mark migration as complete |
| 89 | + await markMigrationComplete(); |
| 90 | + |
| 91 | + return true; |
| 92 | + } catch (error) { |
| 93 | + console.error('Storage migration failed:', error); |
| 94 | + // Don't mark as complete if migration failed |
| 95 | + // This allows retry on next app launch |
| 96 | + return false; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Fallback: If MMKV fails, use AsyncStorage adapter |
| 102 | + */ |
| 103 | +export function createMMKVAdapter(mmkv) { |
| 104 | + return { |
| 105 | + setItem: (key, value) => { |
| 106 | + mmkv.set(key, value); |
| 107 | + return Promise.resolve(); |
| 108 | + }, |
| 109 | + getItem: (key) => { |
| 110 | + const value = mmkv.getString(key); |
| 111 | + return Promise.resolve(value ?? null); |
| 112 | + }, |
| 113 | + removeItem: (key) => { |
| 114 | + mmkv.delete(key); |
| 115 | + return Promise.resolve(); |
| 116 | + }, |
| 117 | + }; |
| 118 | +} |
0 commit comments