Skip to content

Commit 0a777a6

Browse files
committed
fix(api): align headlines schema and seeder with data model
The `headlines` table schema was missing the `status` and `type` columns inherited from its `FeedItem` base class. Additionally, the seeding logic was not correctly handling the nested `source` and `category` objects present in the `Headline` model, leading to a crash. This change updates the `headlines` table schema to be a complete representation of the model. The seeding logic is refactored to correctly extract foreign key IDs from the nested objects and to ensure the parameter map sent to the database driver is perfectly aligned with the schema. This resolves the final seeding error.
1 parent 4e9f3ce commit 0a777a6

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

lib/src/services/database_seeding_service.dart

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ class DatabaseSeedingService {
114114
content TEXT,
115115
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
116116
updated_at TIMESTAMPTZ
117+
status TEXT,
118+
type TEXT
117119
);
118120
''');
119121

@@ -254,29 +256,35 @@ class DatabaseSeedingService {
254256
for (final data in headlinesFixturesData) {
255257
final headline = Headline.fromJson(data);
256258
final params = headline.toJson();
257-
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) {
259+
260+
// The `source_id` and `category_id` columns are NOT NULL. If a fixture
261+
// is missing the nested source or category object, we cannot proceed.
262+
if (headline.source == null || headline.category == null) {
262263
_log.warning(
263264
'Skipping headline fixture with missing source or category ID: '
264265
'${headline.title}',
265266
);
266267
continue;
267268
}
268-
269+
270+
// Extract IDs from nested objects and remove the objects to match schema.
271+
params['source_id'] = headline.source!.id;
272+
params['category_id'] = headline.category!.id;
273+
params.remove('source');
274+
params.remove('category');
275+
269276
// Ensure optional fields exist for the postgres driver.
270277
params.putIfAbsent('description', () => null);
271278
params.putIfAbsent('content', () => null);
272279
params.putIfAbsent('updated_at', () => null);
273-
280+
274281
await _connection.execute(
275282
Sql.named(
276-
'INSERT INTO headlines (id, title, source_id, category_id, image_url, '
277-
'url, published_at, description, content, created_at, updated_at) '
278-
'VALUES (@id, @title, @source_id, @category_id, @image_url, @url, '
279-
'@published_at, @description, @content, @created_at, @updated_at) '
283+
'INSERT INTO headlines (id, title, source_id, category_id, '
284+
'image_url, url, published_at, description, content, status, '
285+
'type, created_at, updated_at) VALUES (@id, @title, @source_id, '
286+
'@category_id, @image_url, @url, @published_at, @description, '
287+
'@content, @status, @type, @created_at, @updated_at) '
280288
'ON CONFLICT (id) DO NOTHING',
281289
),
282290
parameters: params,

0 commit comments

Comments
 (0)