Skip to content

Commit 3a5c15d

Browse files
committed
refactor(database): remove legacy local ad platform from remote configs
- Add migration to delete 'local' ad platform from remote configs collection - Remove 'local' key from adConfig.platformAdIdentifiers map - Update primaryAdPlatform from 'local' to 'admob' where applicable - Prevent deserialization errors after removing AdPlatformType.local
1 parent 814d81e commit 3a5c15d

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
}

lib/src/database/migrations/all_migrations.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:flutter_news_app_api_server_full_source_code/src/database/migrat
33
import 'package:flutter_news_app_api_server_full_source_code/src/database/migrations/20251013000056_add_saved_filters_to_user_preferences.dart';
44
import 'package:flutter_news_app_api_server_full_source_code/src/database/migrations/20251013000057_add_saved_filters_to_remote_config.dart';
55
import 'package:flutter_news_app_api_server_full_source_code/src/database/migrations/20251024000000_add_logo_url_to_sources.dart';
6+
import 'package:flutter_news_app_api_server_full_source_code/src/database/migrations/20251103073226_remove_local_ad_platform.dart';
67
import 'package:flutter_news_app_api_server_full_source_code/src/services/database_migration_service.dart'
78
show DatabaseMigrationService;
89

@@ -16,4 +17,5 @@ final List<Migration> allMigrations = [
1617
AddSavedFiltersToUserPreferences(),
1718
AddSavedFiltersToRemoteConfig(),
1819
AddLogoUrlToSources(),
20+
RemoveLocalAdPlatform(),
1921
];

0 commit comments

Comments
 (0)