|
| 1 | +import 'package:flutter_news_app_api_server_full_source_code/src/database/migration.dart'; |
| 2 | +import 'package:logging/logging.dart'; |
| 3 | +import 'package:mongo_dart/mongo_dart.dart'; |
| 4 | + |
| 5 | +/// Migration to remove the legacy `local` ad platform from the `remote_configs` |
| 6 | +/// collection. |
| 7 | +/// |
| 8 | +/// This migration performs two critical cleanup tasks: |
| 9 | +/// 1. It removes the `local` key from the `adConfig.platformAdIdentifiers` map |
| 10 | +/// in all `remote_configs` documents. |
| 11 | +/// 2. It updates any `remote_configs` document where the `primaryAdPlatform` |
| 12 | +/// is set to `local`, changing it to `admob`. |
| 13 | +/// |
| 14 | +/// This ensures data consistency after the removal of the `AdPlatformType.local` |
| 15 | +/// enum value and prevents deserialization errors in the application. |
| 16 | +class RemoveLocalAdPlatform extends Migration { |
| 17 | + /// {@macro remove_local_ad_platform} |
| 18 | + RemoveLocalAdPlatform() |
| 19 | + : super( |
| 20 | + prDate: '20251103073226', |
| 21 | + prId: '57', |
| 22 | + prSummary: |
| 23 | + 'Removes the legacy local ad platform from the remote config, migrating existing data to use admob as the default.', |
| 24 | + ); |
| 25 | + |
| 26 | + @override |
| 27 | + Future<void> up(Db db, Logger log) async { |
| 28 | + final collection = db.collection('remote_configs'); |
| 29 | + |
| 30 | + // Step 1: Unset the 'local' key from the platformAdIdentifiers map. |
| 31 | + // This removes the field entirely from any document where it exists. |
| 32 | + log.info( |
| 33 | + 'Attempting to remove "adConfig.platformAdIdentifiers.local" field...', |
| 34 | + ); |
| 35 | + final unsetResult = await collection.updateMany( |
| 36 | + where.exists('adConfig.platformAdIdentifiers.local'), |
| 37 | + modify.unset('adConfig.platformAdIdentifiers.local'), |
| 38 | + ); |
| 39 | + log.info( |
| 40 | + 'Removed "adConfig.platformAdIdentifiers.local" from ${unsetResult.nModified} documents.', |
| 41 | + ); |
| 42 | + |
| 43 | + // Step 2: Update the primaryAdPlatform from 'local' to 'admob'. |
| 44 | + // This ensures that no document is left with an invalid primary platform. |
| 45 | + log.info( |
| 46 | + 'Attempting to migrate primaryAdPlatform from "local" to "admob"...', |
| 47 | + ); |
| 48 | + final updateResult = await collection.updateMany( |
| 49 | + where.eq('adConfig.primaryAdPlatform', 'local'), |
| 50 | + modify.set('adConfig.primaryAdPlatform', 'admob'), |
| 51 | + ); |
| 52 | + log.info( |
| 53 | + 'Migrated primaryAdPlatform to "admob" for ${updateResult.nModified} documents.', |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + @override |
| 58 | + Future<void> down(Db db, Logger log) async { |
| 59 | + // Reverting this change is not safe as it would require re-introducing |
| 60 | + // an enum value that no longer exists in the code. |
| 61 | + log.warning( |
| 62 | + 'Reverting "RemoveLocalAdPlatform" is not supported. The "local" ad platform configuration would need to be manually restored if required.', |
| 63 | + ); |
| 64 | + } |
| 65 | +} |
0 commit comments