Skip to content

refactor(database): remove collection seeding from seedDatabase #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 0 additions & 91 deletions lib/src/services/database_seeding_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,50 +29,6 @@ class DatabaseSeedingService {
await _ensureIndexes();
await _seedOverrideAdminUser();

await _seedCollection<Country>(
collectionName: 'countries',
fixtureData: countriesFixturesData,
getId: (item) => item.id,
toJson: (item) => item.toJson(),
);
await _seedCollection<Source>(
collectionName: 'sources',
fixtureData: sourcesFixturesData,
getId: (item) => item.id,
toJson: (item) => item.toJson(),
);
await _seedCollection<Topic>(
collectionName: 'topics',
fixtureData: topicsFixturesData,
getId: (item) => item.id,
toJson: (item) => item.toJson(),
);
await _seedCollection<Headline>(
collectionName: 'headlines',
fixtureData: headlinesFixturesData,
getId: (item) => item.id,
toJson: (item) => item.toJson(),
);
await _seedCollection<User>(
collectionName: 'users',
fixtureData: usersFixturesData,
getId: (item) => item.id,
toJson: (item) => item.toJson(),
);
await _seedCollection<UserAppSettings>(
collectionName: 'user_app_settings',
fixtureData: userAppSettingsFixturesData,
getId: (item) => item.id,
toJson: (item) => item.toJson(),
);

await _seedCollection<RemoteConfig>(
collectionName: 'remote_configs',
fixtureData: remoteConfigsFixturesData,
getId: (item) => item.id,
toJson: (item) => item.toJson(),
);

_log.info('Database seeding process completed.');
}

Expand Down Expand Up @@ -190,53 +146,6 @@ class DatabaseSeedingService {
);
}

/// Seeds a specific collection from a given list of fixture data.
Future<void> _seedCollection<T>({
required String collectionName,
required List<T> fixtureData,
required String Function(T) getId,
required Map<String, dynamic> 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 = <Map<String, Object>>[];

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
Expand Down
Loading