Skip to content

Commit b7954ed

Browse files
committed
refactor(db): update seeding process and SQL queries
- Rename 'categories' to 'topics' in seeding process - Remove 'type' field from countries, sources, and headlines tables - Remove 'category' and 'event_country' objects from headlines seeding - Update SQL queries to reflect schema changes
1 parent 587cb6e commit b7954ed

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

lib/src/services/database_seeding_service.dart

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -171,25 +171,22 @@ class DatabaseSeedingService {
171171
try {
172172
await _connection.execute('BEGIN');
173173
try {
174-
// Seed Categories
175-
_log.fine('Seeding categories...');
176-
for (final data in categoriesFixturesData) {
177-
final category = Category.fromJson(data);
178-
final params = category.toJson();
174+
// Seed Topics
175+
_log.fine('Seeding topics...');
176+
for (final data in topicsFixturesData) {
177+
final topic = Topic.fromJson(data);
178+
final params = topic.toJson();
179179

180180
// Ensure optional fields exist for the postgres driver.
181-
// The driver requires all named parameters to be present in the map,
182-
// even if the value is null. The model's `toJson` with
183-
// `includeIfNull: false` will omit keys for null fields.
184181
params.putIfAbsent('description', () => null);
185182
params.putIfAbsent('icon_url', () => null);
186183
params.putIfAbsent('updated_at', () => null);
187184

188185
await _connection.execute(
189186
Sql.named(
190-
'INSERT INTO categories (id, name, description, icon_url, '
191-
'status, type, created_at, updated_at) VALUES (@id, @name, @description, '
192-
'@icon_url, @status, @type, @created_at, @updated_at) '
187+
'INSERT INTO topics (id, name, description, icon_url, '
188+
'status, created_at, updated_at) VALUES (@id, @name, '
189+
'@description, @icon_url, @status, @created_at, @updated_at) '
193190
'ON CONFLICT (id) DO NOTHING',
194191
),
195192
parameters: params,
@@ -207,9 +204,9 @@ class DatabaseSeedingService {
207204

208205
await _connection.execute(
209206
Sql.named(
210-
'INSERT INTO countries (id, name, iso_code, flag_url, status, '
211-
'type, created_at, updated_at) VALUES (@id, @name, @iso_code, '
212-
'@flag_url, @status, @type, @created_at, @updated_at) '
207+
'INSERT INTO countries (id, name, iso_code, flag_url, '
208+
'status, created_at, updated_at) VALUES (@id, @name, '
209+
'@iso_code, @flag_url, @status, @created_at, @updated_at) '
213210
'ON CONFLICT (id) DO NOTHING',
214211
),
215212
parameters: params,
@@ -235,16 +232,14 @@ class DatabaseSeedingService {
235232
params.putIfAbsent('url', () => null);
236233
params.putIfAbsent('language', () => null);
237234
params.putIfAbsent('source_type', () => null);
238-
params.putIfAbsent('status', () => null);
239-
params.putIfAbsent('type', () => null);
240235
params.putIfAbsent('updated_at', () => null);
241236

242237
await _connection.execute(
243238
Sql.named(
244239
'INSERT INTO sources (id, name, description, url, language, '
245-
'status, type, source_type, headquarters_country_id, '
246-
'created_at, updated_at) VALUES (@id, @name, @description, '
247-
'@url, @language, @status, @type, @source_type, '
240+
'status, source_type, headquarters_country_id, '
241+
'created_at, updated_at) VALUES (@id, @name, @description, @url, '
242+
'@language, @status, @source_type, '
248243
'@headquarters_country_id, @created_at, @updated_at) '
249244
'ON CONFLICT (id) DO NOTHING',
250245
),
@@ -257,28 +252,27 @@ class DatabaseSeedingService {
257252
for (final data in headlinesFixturesData) {
258253
final headline = Headline.fromJson(data);
259254
final params = headline.toJson();
260-
255+
261256
// Extract IDs from nested objects and remove the objects to match schema.
262-
// These are now nullable to match the schema.
263-
params['source_id'] = headline.source?.id;
264-
params['category_id'] = headline.category?.id;
257+
params['source_id'] = headline.source.id;
258+
params['topic_id'] = headline.topic.id;
259+
params['event_country_id'] = headline.eventCountry.id;
265260
params.remove('source');
266-
params.remove('category');
267-
261+
params.remove('topic');
262+
params.remove('eventCountry');
263+
268264
// Ensure optional fields exist for the postgres driver.
269-
params.putIfAbsent('description', () => null);
265+
params.putIfAbsent('excerpt', () => null);
270266
params.putIfAbsent('updated_at', () => null);
271267
params.putIfAbsent('image_url', () => null);
272268
params.putIfAbsent('url', () => null);
273-
params.putIfAbsent('published_at', () => null);
274-
269+
275270
await _connection.execute(
276271
Sql.named(
277-
'INSERT INTO headlines (id, title, source_id, category_id, '
278-
'image_url, url, published_at, description, status, '
279-
'type, created_at, updated_at) VALUES (@id, @title, @source_id, '
280-
'@category_id, @image_url, @url, @published_at, @description, '
281-
'@status, @type, @created_at, @updated_at) '
272+
'INSERT INTO headlines (id, title, excerpt, url, image_url, '
273+
'source_id, topic_id, event_country_id, status, created_at, '
274+
'updated_at) VALUES (@id, @title, @excerpt, @url, @image_url, '
275+
'@source_id, @topic_id, @event_country_id, @status, @created_at, @updated_at) '
282276
'ON CONFLICT (id) DO NOTHING',
283277
),
284278
parameters: params,

0 commit comments

Comments
 (0)