From f4dfbfbb3bdac18559443f5c2e6d2c0668815416 Mon Sep 17 00:00:00 2001 From: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com> Date: Mon, 26 Jan 2026 09:13:47 +0530 Subject: [PATCH] pr review --- .../lib/domain/utils/migrate_cloud_ids.dart | 73 ++++++++----------- .../repositories/local_asset.repository.dart | 1 - 2 files changed, 32 insertions(+), 42 deletions(-) diff --git a/mobile/lib/domain/utils/migrate_cloud_ids.dart b/mobile/lib/domain/utils/migrate_cloud_ids.dart index 904ea2b795..33a8eca94d 100644 --- a/mobile/lib/domain/utils/migrate_cloud_ids.dart +++ b/mobile/lib/domain/utils/migrate_cloud_ids.dart @@ -64,76 +64,63 @@ Future _processCloudIdMappingsInBatches( Logger logger, ) async { const pageSize = 20000; - int offset = 0; + String? lastLocalId; final seenRemoteAssetIds = {}; while (true) { - final mappings = await _fetchCloudIdMappings(drift, userId, pageSize, offset); + final mappings = await _fetchCloudIdMappings(drift, userId, pageSize, lastLocalId); if (mappings.isEmpty) { break; } - final uniqueMappings = <_CloudIdMapping>[]; + final items = []; for (final mapping in mappings) { if (seenRemoteAssetIds.add(mapping.remoteAssetId)) { - uniqueMappings.add(mapping); + items.add( + AssetMetadataBulkUpsertItemDto( + assetId: mapping.remoteAssetId, + key: kMobileMetadataKey, + value: RemoteAssetMobileAppMetadata( + cloudId: mapping.localAsset.cloudId, + createdAt: mapping.localAsset.createdAt.toIso8601String(), + adjustmentTime: mapping.localAsset.adjustmentTime?.toIso8601String(), + latitude: mapping.localAsset.latitude?.toString(), + longitude: mapping.localAsset.longitude?.toString(), + ), + ), + ); } else { logger.fine('Duplicate remote asset ID found: ${mapping.remoteAssetId}. Skipping duplicate entry.'); } } - if (uniqueMappings.isNotEmpty) { + if (items.isNotEmpty) { if (canBulkUpdate) { - await _bulkUpdateCloudIds(assetsApi, uniqueMappings); + await _bulkUpdateCloudIds(assetsApi, items); } else { - await _sequentialUpdateCloudIds(assetsApi, uniqueMappings); + await _sequentialUpdateCloudIds(assetsApi, items); } } - offset += pageSize; + lastLocalId = mappings.last.localAsset.id; if (mappings.length < pageSize) { break; } } } -Future _sequentialUpdateCloudIds(AssetsApi assetsApi, List<_CloudIdMapping> mappings) async { - for (final mapping in mappings) { - final item = AssetMetadataUpsertItemDto( - key: kMobileMetadataKey, - value: RemoteAssetMobileAppMetadata( - cloudId: mapping.localAsset.cloudId, - createdAt: mapping.localAsset.createdAt.toIso8601String(), - adjustmentTime: mapping.localAsset.adjustmentTime?.toIso8601String(), - latitude: mapping.localAsset.latitude?.toString(), - longitude: mapping.localAsset.longitude?.toString(), - ), - ); +Future _sequentialUpdateCloudIds(AssetsApi assetsApi, List items) async { + for (final item in items) { + final upsertItem = AssetMetadataUpsertItemDto(key: item.key, value: item.value); try { - await assetsApi.updateAssetMetadata(mapping.remoteAssetId, AssetMetadataUpsertDto(items: [item])); + await assetsApi.updateAssetMetadata(item.assetId, AssetMetadataUpsertDto(items: [upsertItem])); } catch (error, stack) { - Logger('migrateCloudIds').warning('Failed to update metadata for asset ${mapping.remoteAssetId}', error, stack); + Logger('migrateCloudIds').warning('Failed to update metadata for asset ${item.assetId}', error, stack); } } } -Future _bulkUpdateCloudIds(AssetsApi assetsApi, List<_CloudIdMapping> mappings) async { - final items = []; - for (final mapping in mappings) { - items.add( - AssetMetadataBulkUpsertItemDto( - assetId: mapping.remoteAssetId, - key: kMobileMetadataKey, - value: RemoteAssetMobileAppMetadata( - cloudId: mapping.localAsset.cloudId, - createdAt: mapping.localAsset.createdAt.toIso8601String(), - adjustmentTime: mapping.localAsset.adjustmentTime?.toIso8601String(), - latitude: mapping.localAsset.latitude?.toString(), - longitude: mapping.localAsset.longitude?.toString(), - ), - ), - ); - } +Future _bulkUpdateCloudIds(AssetsApi assetsApi, List items) async { try { await assetsApi.updateBulkAssetMetadata(AssetMetadataBulkUpsertDto(items: items)); } catch (error, stack) { @@ -163,7 +150,7 @@ Future _populateCloudIds(Drift drift) async { typedef _CloudIdMapping = ({String remoteAssetId, LocalAsset localAsset}); -Future> _fetchCloudIdMappings(Drift drift, String userId, int limit, int offset) async { +Future> _fetchCloudIdMappings(Drift drift, String userId, int limit, String? lastLocalId) async { final query = drift.localAssetEntity.select().join([ innerJoin( @@ -189,7 +176,11 @@ Future> _fetchCloudIdMappings(Drift drift, String userId, drift.remoteAssetCloudIdEntity.createdAt.isNotExp(drift.localAssetEntity.createdAt)), ) ..orderBy([OrderingTerm.asc(drift.localAssetEntity.id)]) - ..limit(limit, offset: offset); + ..limit(limit); + + if (lastLocalId != null) { + query.where(drift.localAssetEntity.id.isBiggerThanValue(lastLocalId)); + } return query.map((row) { return ( diff --git a/mobile/lib/infrastructure/repositories/local_asset.repository.dart b/mobile/lib/infrastructure/repositories/local_asset.repository.dart index 57edbbe556..9d7cbd831b 100644 --- a/mobile/lib/infrastructure/repositories/local_asset.repository.dart +++ b/mobile/lib/infrastructure/repositories/local_asset.repository.dart @@ -213,7 +213,6 @@ class DriftLocalAssetRepository extends DriftDatabaseRepository { INNER JOIN remote_asset_entity ON remote_asset_cloud_id_entity.asset_id = remote_asset_entity.id WHERE local_asset_entity.i_cloud_id = remote_asset_cloud_id_entity.cloud_id - AND local_asset_entity.i_cloud_id IS NOT NULL AND local_asset_entity.checksum IS NULL AND remote_asset_cloud_id_entity.adjustment_time IS local_asset_entity.adjustment_time AND remote_asset_cloud_id_entity.latitude IS local_asset_entity.latitude