From 1a03dd838086c0b6daf8638f5f59f50055877804 Mon Sep 17 00:00:00 2001 From: fulleni Date: Wed, 30 Jul 2025 13:28:47 +0100 Subject: [PATCH] refactor(database): remove collection seeding from `seedDatabase` - Remove `_seedCollection` method and its calls - Remove unused fixture data imports - Keep `_ensureIndexes` and `_seedOverrideAdminUser` methods --- .../services/database_seeding_service.dart | 91 ------------------- 1 file changed, 91 deletions(-) diff --git a/lib/src/services/database_seeding_service.dart b/lib/src/services/database_seeding_service.dart index d7a3e51..6ba1e75 100644 --- a/lib/src/services/database_seeding_service.dart +++ b/lib/src/services/database_seeding_service.dart @@ -29,50 +29,6 @@ class DatabaseSeedingService { await _ensureIndexes(); await _seedOverrideAdminUser(); - await _seedCollection( - collectionName: 'countries', - fixtureData: countriesFixturesData, - getId: (item) => item.id, - toJson: (item) => item.toJson(), - ); - await _seedCollection( - collectionName: 'sources', - fixtureData: sourcesFixturesData, - getId: (item) => item.id, - toJson: (item) => item.toJson(), - ); - await _seedCollection( - collectionName: 'topics', - fixtureData: topicsFixturesData, - getId: (item) => item.id, - toJson: (item) => item.toJson(), - ); - await _seedCollection( - collectionName: 'headlines', - fixtureData: headlinesFixturesData, - getId: (item) => item.id, - toJson: (item) => item.toJson(), - ); - await _seedCollection( - collectionName: 'users', - fixtureData: usersFixturesData, - getId: (item) => item.id, - toJson: (item) => item.toJson(), - ); - await _seedCollection( - collectionName: 'user_app_settings', - fixtureData: userAppSettingsFixturesData, - getId: (item) => item.id, - toJson: (item) => item.toJson(), - ); - - await _seedCollection( - collectionName: 'remote_configs', - fixtureData: remoteConfigsFixturesData, - getId: (item) => item.id, - toJson: (item) => item.toJson(), - ); - _log.info('Database seeding process completed.'); } @@ -190,53 +146,6 @@ class DatabaseSeedingService { ); } - /// Seeds a specific collection from a given list of fixture data. - Future _seedCollection({ - required String collectionName, - required List fixtureData, - required String Function(T) getId, - required Map Function(T) toJson, - }) async { - _log.info('Seeding collection: "$collectionName"...'); - try { - if (fixtureData.isEmpty) { - _log.info('No documents to seed for "$collectionName".'); - return; - } - - final collection = _db.collection(collectionName); - final operations = >[]; - - for (final item in fixtureData) { - // Use the predefined hex string ID from the fixture to create a - // deterministic ObjectId. This is crucial for maintaining relationships - // between documents (e.g., a headline and its source). - final objectId = ObjectId.fromHexString(getId(item)); - final document = toJson(item)..remove('id'); - - operations.add({ - // Use updateOne with $set to be less destructive than replaceOne. - 'updateOne': { - // Filter by the specific, deterministic _id. - 'filter': {'_id': objectId}, - // Set the fields of the document. - 'update': {r'$set': document}, - 'upsert': true, - }, - }); - } - - final result = await collection.bulkWrite(operations); - _log.info( - 'Seeding for "$collectionName" complete. ' - 'Upserted: ${result.nUpserted}, Modified: ${result.nModified}.', - ); - } on Exception catch (e, s) { - _log.severe('Failed to seed collection "$collectionName".', e, s); - rethrow; - } - } - /// Ensures that the necessary indexes exist on the collections. /// /// This method is idempotent; it will only create indexes if they do not