Skip to content

Commit 4e9f3ce

Browse files
committed
fix(api): make headline seeder resilient and align with schema
The database seeding was crashing with a "Missing variable for 'source_id'" error due to invalid fixture data. Additionally, the `INSERT` statement for headlines was missing the `created_at` and `updated_at` columns. This change makes the headline seeder more robust by validating that required fields (`source_id`, `category_id`) are present before attempting insertion, skipping invalid fixtures with a warning. It also updates the `INSERT` statement to be fully aligned with the table schema, resolving all remaining seeding issues.
1 parent 3b11a62 commit 4e9f3ce

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

lib/src/services/database_seeding_service.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,17 +255,28 @@ class DatabaseSeedingService {
255255
final headline = Headline.fromJson(data);
256256
final params = headline.toJson();
257257

258+
// The `source_id` and `category_id` columns are NOT NULL. If a
259+
// fixture is missing these, the `toJson()` map will lack the key
260+
// and cause a crash. We log a warning and skip the invalid entry.
261+
if (params['source_id'] == null || params['category_id'] == null) {
262+
_log.warning(
263+
'Skipping headline fixture with missing source or category ID: '
264+
'${headline.title}',
265+
);
266+
continue;
267+
}
268+
258269
// Ensure optional fields exist for the postgres driver.
259270
params.putIfAbsent('description', () => null);
260271
params.putIfAbsent('content', () => null);
261272
params.putIfAbsent('updated_at', () => null);
262273

263274
await _connection.execute(
264275
Sql.named(
265-
'INSERT INTO headlines (id, title, source_id, category_id, '
266-
'image_url, url, published_at, description, content) '
276+
'INSERT INTO headlines (id, title, source_id, category_id, image_url, '
277+
'url, published_at, description, content, created_at, updated_at) '
267278
'VALUES (@id, @title, @source_id, @category_id, @image_url, @url, '
268-
'@published_at, @description, @content) '
279+
'@published_at, @description, @content, @created_at, @updated_at) '
269280
'ON CONFLICT (id) DO NOTHING',
270281
),
271282
parameters: params,

0 commit comments

Comments
 (0)