|
| 1 | +// ignore_for_file: comment_references |
| 2 | + |
| 3 | +import 'package:core/core.dart'; |
| 4 | +import 'package:flutter_news_app_api_server_full_source_code/src/database/migration.dart'; |
| 5 | +import 'package:logging/logging.dart'; |
| 6 | +import 'package:mongo_dart/mongo_dart.dart'; |
| 7 | + |
| 8 | +/// {@template refactor_ad_config_to_role_based} |
| 9 | +/// A comprehensive migration to refactor the `adConfig` structure within |
| 10 | +/// `RemoteConfig` documents to a new role-based `visibleTo` map approach. |
| 11 | +/// |
| 12 | +/// This migration addresses significant changes introduced by a PR (see |
| 13 | +/// [gitHubPullRequest]) that aimed to enhance flexibility and maintainability |
| 14 | +/// of ad configurations. It transforms old, role-specific ad frequency and |
| 15 | +/// placement fields into new `visibleTo` maps for `FeedAdConfiguration`, |
| 16 | +/// `ArticleAdConfiguration`, and `InterstitialAdConfiguration`. |
| 17 | +/// |
| 18 | +/// The migration ensures that existing `RemoteConfig` documents are updated |
| 19 | +/// to conform to the latest model structure, preventing deserialization errors |
| 20 | +/// and enabling granular control over ad display for different user roles. |
| 21 | +/// {@endtemplate} |
| 22 | +class RefactorAdConfigToRoleBased extends Migration { |
| 23 | + /// {@macro refactor_ad_config_to_role_based} |
| 24 | + RefactorAdConfigToRoleBased() |
| 25 | + : super( |
| 26 | + prDate: '20250924084800', |
| 27 | + prSummary: 'Refactor adConfig to use role-based visibleTo maps', |
| 28 | + prId: '50', |
| 29 | + ); |
| 30 | + |
| 31 | + @override |
| 32 | + Future<void> up(Db db, Logger log) async { |
| 33 | + log.info( |
| 34 | + 'Applying migration PR#$prId (Date: $prDate): $prSummary.', |
| 35 | + ); |
| 36 | + |
| 37 | + final remoteConfigCollection = db.collection('remote_configs'); |
| 38 | + |
| 39 | + // Define default FeedAdFrequencyConfig for roles |
| 40 | + const defaultGuestFeedAdFrequency = FeedAdFrequencyConfig( |
| 41 | + adFrequency: 5, |
| 42 | + adPlacementInterval: 3, |
| 43 | + ); |
| 44 | + const defaultStandardUserFeedAdFrequency = FeedAdFrequencyConfig( |
| 45 | + adFrequency: 10, |
| 46 | + adPlacementInterval: 5, |
| 47 | + ); |
| 48 | + // Define default InterstitialAdFrequencyConfig for roles |
| 49 | + const defaultGuestInterstitialAdFrequency = InterstitialAdFrequencyConfig( |
| 50 | + transitionsBeforeShowingInterstitialAds: 5, |
| 51 | + ); |
| 52 | + const defaultStandardUserInterstitialAdFrequency = |
| 53 | + InterstitialAdFrequencyConfig( |
| 54 | + transitionsBeforeShowingInterstitialAds: 10, |
| 55 | + ); |
| 56 | + |
| 57 | + // Define default ArticleAdSlot visibility for roles |
| 58 | + final defaultArticleAdSlots = { |
| 59 | + InArticleAdSlotType.aboveArticleContinueReadingButton.name: true, |
| 60 | + InArticleAdSlotType.belowArticleContinueReadingButton.name: true, |
| 61 | + }; |
| 62 | + |
| 63 | + final result = await remoteConfigCollection.updateMany( |
| 64 | + // Find documents that still have the old structure (e.g., old frequency fields) |
| 65 | + where.exists( |
| 66 | + 'adConfig.feedAdConfiguration.frequencyConfig.guestAdFrequency', |
| 67 | + ), |
| 68 | + ModifierBuilder() |
| 69 | + // --- FeedAdConfiguration Transformation --- |
| 70 | + // Remove old frequencyConfig fields |
| 71 | + ..unset('adConfig.feedAdConfiguration.frequencyConfig.guestAdFrequency') |
| 72 | + ..unset( |
| 73 | + 'adConfig.feedAdConfiguration.frequencyConfig.guestAdPlacementInterval', |
| 74 | + ) |
| 75 | + ..unset( |
| 76 | + 'adConfig.feedAdConfiguration.frequencyConfig.authenticatedAdFrequency', |
| 77 | + ) |
| 78 | + ..unset( |
| 79 | + 'adConfig.feedAdConfiguration.frequencyConfig.authenticatedAdPlacementInterval', |
| 80 | + ) |
| 81 | + ..unset( |
| 82 | + 'adConfig.feedAdConfiguration.frequencyConfig.premiumAdFrequency', |
| 83 | + ) |
| 84 | + ..unset( |
| 85 | + 'adConfig.feedAdConfiguration.frequencyConfig.premiumAdPlacementInterval', |
| 86 | + ) |
| 87 | + // Set the new visibleTo map for FeedAdConfiguration |
| 88 | + ..set( |
| 89 | + 'adConfig.feedAdConfiguration.visibleTo', |
| 90 | + { |
| 91 | + AppUserRole.guestUser.name: defaultGuestFeedAdFrequency.toJson(), |
| 92 | + AppUserRole.standardUser.name: defaultStandardUserFeedAdFrequency |
| 93 | + .toJson(), |
| 94 | + }, |
| 95 | + ) |
| 96 | + // --- ArticleAdConfiguration Transformation --- |
| 97 | + // Remove old inArticleAdSlotConfigurations list |
| 98 | + ..unset('adConfig.articleAdConfiguration.inArticleAdSlotConfigurations') |
| 99 | + // Set the new visibleTo map for ArticleAdConfiguration |
| 100 | + ..set( |
| 101 | + 'adConfig.articleAdConfiguration.visibleTo', |
| 102 | + { |
| 103 | + AppUserRole.guestUser.name: defaultArticleAdSlots, |
| 104 | + AppUserRole.standardUser.name: defaultArticleAdSlots, |
| 105 | + }, |
| 106 | + ) |
| 107 | + // --- InterstitialAdConfiguration Transformation --- |
| 108 | + // Remove old feedInterstitialAdFrequencyConfig fields |
| 109 | + ..unset( |
| 110 | + 'adConfig.interstitialAdConfiguration.feedInterstitialAdFrequencyConfig.guestTransitionsBeforeShowingInterstitialAds', |
| 111 | + ) |
| 112 | + ..unset( |
| 113 | + 'adConfig.interstitialAdConfiguration.feedInterstitialAdFrequencyConfig.standardUserTransitionsBeforeShowingInterstitialAds', |
| 114 | + ) |
| 115 | + ..unset( |
| 116 | + 'adConfig.interstitialAdConfiguration.feedInterstitialAdFrequencyConfig.premiumUserTransitionsBeforeShowingInterstitialAds', |
| 117 | + ) |
| 118 | + // Set the new visibleTo map for InterstitialAdConfiguration |
| 119 | + ..set( |
| 120 | + 'adConfig.interstitialAdConfiguration.visibleTo', |
| 121 | + { |
| 122 | + AppUserRole.guestUser.name: defaultGuestInterstitialAdFrequency |
| 123 | + .toJson(), |
| 124 | + AppUserRole.standardUser.name: |
| 125 | + defaultStandardUserInterstitialAdFrequency.toJson(), |
| 126 | + }, |
| 127 | + ), |
| 128 | + ); |
| 129 | + |
| 130 | + log.info( |
| 131 | + 'Updated ${result.nModified} remote_config documents ' |
| 132 | + 'to new role-based adConfig structure.', |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + @override |
| 137 | + Future<void> down(Db db, Logger log) async { |
| 138 | + log.warning( |
| 139 | + 'Reverting migration: Revert adConfig to old structure ' |
| 140 | + '(not recommended for production).', |
| 141 | + ); |
| 142 | + // This down migration is complex and primarily for development/testing rollback. |
| 143 | + // Reverting to the old structure would require re-introducing the old fields |
| 144 | + // and potentially losing data if the new structure was used. |
| 145 | + // For simplicity in this example, we'll just unset the new fields. |
| 146 | + final result = await db |
| 147 | + .collection('remote_configs') |
| 148 | + .updateMany( |
| 149 | + where.exists('adConfig.feedAdConfiguration.visibleTo'), |
| 150 | + ModifierBuilder() |
| 151 | + ..unset('adConfig.feedAdConfiguration.visibleTo') |
| 152 | + ..unset('adConfig.articleAdConfiguration.visibleTo') |
| 153 | + ..unset('adConfig.interstitialAdConfiguration.visibleTo'), |
| 154 | + ); |
| 155 | + log.warning( |
| 156 | + 'Reverted ${result.nModified} remote_config documents ' |
| 157 | + 'by unsetting new adConfig fields.', |
| 158 | + ); |
| 159 | + } |
| 160 | +} |
0 commit comments