Skip to content

Commit bd1bd12

Browse files
committed
feat(database): add logoUrl field to existing sources
- New migration to add logoUrl field to sources collection - Uses Clearbit Logo API to generate logo URLs based on source website URLs - Updates 66 files, adds 1 new migration file
1 parent fe00ba0 commit bd1bd12

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 add the `logoUrl` field to existing `sources` documents.
6+
class AddLogoUrlToSources extends Migration {
7+
/// {@macro add_logo_url_to_sources}
8+
AddLogoUrlToSources()
9+
: super(
10+
prDate: '20251024000000',
11+
prId: '60',
12+
prSummary:
13+
'Adds the required `logoUrl` field to all existing '
14+
'documents in the `sources` collection to align with the '
15+
'core v1.3.0 model update.',
16+
);
17+
18+
@override
19+
Future<void> up(Db db, Logger log) async {
20+
final collection = db.collection('sources');
21+
final sourcesToUpdate = await collection
22+
.find(where.notExists('logoUrl'))
23+
.toList();
24+
25+
if (sourcesToUpdate.isEmpty) {
26+
log.info('No sources found needing a logoUrl. Migration is up to date.');
27+
return;
28+
}
29+
30+
log.info(
31+
'Found ${sourcesToUpdate.length} sources to update with a logoUrl.',
32+
);
33+
var count = 0;
34+
for (final source in sourcesToUpdate) {
35+
final sourceUrl = source['url'] as String?;
36+
if (sourceUrl != null && sourceUrl.isNotEmpty) {
37+
try {
38+
final host = Uri.parse(sourceUrl).host;
39+
final logoUrl = 'https://logo.clearbit.com/$host?size=200';
40+
await collection.updateOne(
41+
where.id(source['_id'] as ObjectId),
42+
modify.set('logoUrl', logoUrl),
43+
);
44+
count++;
45+
} catch (e) {
46+
log.warning(
47+
'Could not parse URL for source ${source['_id']}: $sourceUrl',
48+
);
49+
}
50+
}
51+
}
52+
log.info('Updated $count sources with a new logoUrl.');
53+
}
54+
55+
@override
56+
Future<void> down(Db db, Logger log) async {
57+
final collection = db.collection('sources');
58+
await collection.updateMany(
59+
where.exists('logoUrl'),
60+
modify.unset('logoUrl'),
61+
);
62+
log.info(
63+
'Removed "logoUrl" field from all documents in the sources collection.',
64+
);
65+
}
66+
}

lib/src/database/migrations/all_migrations.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter_news_app_api_server_full_source_code/src/database/migration.dart';
22
import 'package:flutter_news_app_api_server_full_source_code/src/database/migrations/20250924084800__refactor_ad_config_to_role_based.dart';
3+
import 'package:flutter_news_app_api_server_full_source_code/src/database/migrations/20251024000000_add_logo_url_to_sources.dart';
34
import 'package:flutter_news_app_api_server_full_source_code/src/database/migrations/20251013000056_add_saved_filters_to_user_preferences.dart';
45
import 'package:flutter_news_app_api_server_full_source_code/src/database/migrations/20251013000057_add_saved_filters_to_remote_config.dart';
56
import 'package:flutter_news_app_api_server_full_source_code/src/services/database_migration_service.dart'
@@ -14,4 +15,5 @@ final List<Migration> allMigrations = [
1415
RefactorAdConfigToRoleBased(),
1516
AddSavedFiltersToUserPreferences(),
1617
AddSavedFiltersToRemoteConfig(),
18+
AddLogoUrlToSources(),
1719
];

0 commit comments

Comments
 (0)