TF-2310 Upgrade hive database to version 10

This commit is contained in:
dab246
2024-04-12 16:47:10 +07:00
committed by Dat H. Pham
parent 017c7135d3
commit 5eb75ee9ee
7 changed files with 61 additions and 13 deletions
@@ -0,0 +1,4 @@
abstract class UpgradeDatabaseSteps {
Future<void> onUpgrade(int oldVersion, int newVersion);
}
@@ -0,0 +1,17 @@
import 'package:tmail_ui_user/features/base/upgradeable/upgrade_database_steps.dart';
import 'package:tmail_ui_user/features/caching/caching_manager.dart';
class UpgradeHiveDatabaseStepsV10 extends UpgradeDatabaseSteps {
final CachingManager _cachingManager;
UpgradeHiveDatabaseStepsV10(this._cachingManager);
@override
Future<void> onUpgrade(int oldVersion, int newVersion) async {
if (oldVersion > 0 && oldVersion < newVersion && newVersion == 10) {
await _cachingManager.clearLoginRecentData();
}
}
}
@@ -0,0 +1,17 @@
import 'package:tmail_ui_user/features/base/upgradeable/upgrade_database_steps.dart';
import 'package:tmail_ui_user/features/caching/caching_manager.dart';
class UpgradeHiveDatabaseStepsV7 extends UpgradeDatabaseSteps {
final CachingManager _cachingManager;
UpgradeHiveDatabaseStepsV7(this._cachingManager);
@override
Future<void> onUpgrade(int oldVersion, int newVersion) async {
if (oldVersion > 0 && oldVersion < newVersion && newVersion == 7) {
await _cachingManager.clearAll();
}
}
}
+13 -9
View File
@@ -11,6 +11,8 @@ import 'package:tmail_ui_user/features/caching/clients/hive_cache_version_client
import 'package:tmail_ui_user/features/caching/clients/mailbox_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/new_email_hive_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/opened_email_hive_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/recent_login_url_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/recent_login_username_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/recent_search_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/session_hive_cache_client.dart';
import 'package:tmail_ui_user/features/caching/clients/state_cache_client.dart';
@@ -26,6 +28,8 @@ class CachingManager {
final StateCacheClient _stateCacheClient;
final EmailCacheClient _emailCacheClient;
final RecentSearchCacheClient _recentSearchCacheClient;
final RecentLoginUrlCacheClient _recentLoginUrlCacheClient;
final RecentLoginUsernameCacheClient _recentLoginUsernameCacheClient;
final AccountCacheClient _accountCacheClient;
final FcmCacheClient _fcmCacheClient;
final FirebaseRegistrationCacheClient _firebaseRegistrationCacheClient;
@@ -43,6 +47,8 @@ class CachingManager {
this._stateCacheClient,
this._emailCacheClient,
this._recentSearchCacheClient,
this._recentLoginUrlCacheClient,
this._recentLoginUsernameCacheClient,
this._accountCacheClient,
this._fcmCacheClient,
this._firebaseRegistrationCacheClient,
@@ -110,15 +116,6 @@ class CachingManager {
], eagerError: true);
}
Future<void> onUpgradeCache(int oldVersion, int newVersion) async {
log('CachingManager::onUpgradeCache():oldVersion $oldVersion | newVersion: $newVersion');
await clearData();
if (oldVersion > 0 && oldVersion < newVersion && newVersion == 7) {
await clearAll();
}
await storeCacheVersion(newVersion);
}
Future<bool> storeCacheVersion(int newVersion) async {
log('CachingManager::storeCacheVersion()');
return _hiveCacheVersionClient.storeVersion(newVersion);
@@ -138,4 +135,11 @@ class CachingManager {
_fileUtils.removeFolder(CachingConstants.openedEmailContentFolderName),
]);
}
Future<void> clearLoginRecentData() async {
await Future.wait([
_recentLoginUrlCacheClient.clearAllData(),
_recentLoginUsernameCacheClient.clearAllData(),
]);
}
}
@@ -1,4 +1,4 @@
class CacheVersion {
static const int hiveDBVersion = 9;
static const int hiveDBVersion = 10;
}
@@ -6,6 +6,8 @@ import 'package:core/utils/app_logger.dart';
import 'package:core/utils/platform_info.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart' as path_provider;
import 'package:tmail_ui_user/features/base/upgradeable/upgrade_hive_database_steps_v10.dart';
import 'package:tmail_ui_user/features/base/upgradeable/upgrade_hive_database_steps_v7.dart';
import 'package:tmail_ui_user/features/caching/caching_manager.dart';
import 'package:tmail_ui_user/features/caching/config/cache_version.dart';
import 'package:tmail_ui_user/features/caching/utils/caching_constants.dart';
@@ -60,9 +62,11 @@ class HiveCacheConfig {
final oldVersion = await cachingManager.getLatestVersion() ?? 0;
const newVersion = CacheVersion.hiveDBVersion;
log('HiveCacheConfig::onUpgradeDatabase():oldVersion: $oldVersion | newVersion: $newVersion');
if (oldVersion != newVersion) {
await cachingManager.onUpgradeCache(oldVersion, newVersion);
}
await UpgradeHiveDatabaseStepsV7(cachingManager).onUpgrade(oldVersion, newVersion);
await UpgradeHiveDatabaseStepsV10(cachingManager).onUpgrade(oldVersion, newVersion);
await cachingManager.storeCacheVersion(newVersion);
}
Future<void> initializeEncryptionKey() async {
@@ -93,6 +93,8 @@ class LocalBindings extends Bindings {
Get.find<StateCacheClient>(),
Get.find<EmailCacheClient>(),
Get.find<RecentSearchCacheClient>(),
Get.find<RecentLoginUrlCacheClient>(),
Get.find<RecentLoginUsernameCacheClient>(),
Get.find<AccountCacheClient>(),
Get.find<FcmCacheClient>(),
Get.find<FirebaseRegistrationCacheClient>(),