Skip to content

Commit 33cab08

Browse files
committed
refactor(database): update seeding process and data handling
- Replace AppConfig with RemoteConfig to accommodate multiple JSONB columns - Update admin user seeding to reflect new User model structure - Improve error handling and logging - Refactor JSONB data insertion to ensure proper encoding
1 parent b7954ed commit 33cab08

File tree

1 file changed

+71
-36
lines changed

1 file changed

+71
-36
lines changed

lib/src/services/database_seeding_service.dart

Lines changed: 71 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -295,90 +295,125 @@ class DatabaseSeedingService {
295295
}
296296
}
297297

298-
/// Seeds the database with the initial AppConfig and the default admin user.
298+
/// Seeds the database with the initial RemoteConfig and the default admin user.
299299
///
300300
/// This method is idempotent, using `ON CONFLICT DO NOTHING` to prevent
301301
/// errors if the data already exists. It runs within a single transaction.
302302
Future<void> seedInitialAdminAndConfig() async {
303-
_log.info('Seeding initial AppConfig and admin user...');
303+
_log.info('Seeding initial RemoteConfig and admin user...');
304304
try {
305305
await _connection.execute('BEGIN');
306306
try {
307-
// Seed AppConfig
308-
_log.fine('Seeding AppConfig...');
309-
final appConfig = AppConfig.fromJson(appConfigFixtureData);
310-
// The `app_config` table only has `id` and `user_preference_limits`.
311-
// We must provide an explicit map to avoid a "superfluous variables"
312-
// error from the postgres driver.
307+
// Seed RemoteConfig
308+
_log.fine('Seeding RemoteConfig...');
309+
final remoteConfig = RemoteConfig.fromJson(remoteConfigFixtureData);
310+
// The `remote_config` table has multiple JSONB columns. We must
311+
// provide an explicit map with JSON-encoded values to avoid a
312+
// "superfluous variables" error from the postgres driver.
313313
await _connection.execute(
314314
Sql.named(
315-
'INSERT INTO app_config (id, user_preference_limits) '
316-
'VALUES (@id, @user_preference_limits) '
315+
'INSERT INTO remote_config (id, user_preference_limits, ad_config, '
316+
'account_action_config, app_status) VALUES (@id, '
317+
'@user_preference_limits, @ad_config, @account_action_config, '
318+
'@app_status) '
317319
'ON CONFLICT (id) DO NOTHING',
318320
),
319321
parameters: {
320-
'id': appConfig.id,
321-
'user_preference_limits': appConfig.userPreferenceLimits.toJson(),
322+
'id': remoteConfig.id,
323+
'user_preference_limits':
324+
jsonEncode(remoteConfig.userPreferenceLimits.toJson()),
325+
'ad_config': jsonEncode(remoteConfig.adConfig.toJson()),
326+
'account_action_config':
327+
jsonEncode(remoteConfig.accountActionConfig.toJson()),
328+
'app_status': jsonEncode(remoteConfig.appStatus.toJson()),
322329
},
323330
);
324331

325332
// Seed Admin User
326333
_log.fine('Seeding admin user...');
327334
// Find the admin user in the fixture data.
328-
final adminUser = usersFixturesData.firstWhere(
329-
(user) => user.roles.contains(UserRoles.admin),
335+
final adminUserData = usersFixturesData.firstWhere(
336+
(data) => data['dashboard_role'] == DashboardUserRole.admin.name,
330337
orElse: () => throw StateError('Admin user not found in fixtures.'),
331338
);
332-
// The `users` table only has `id`, `email`, and `roles`. We must
333-
// provide an explicit map to avoid a "superfluous variables" error.
339+
final adminUser = User.fromJson(adminUserData);
340+
341+
// The `users` table has specific columns for roles and status.
334342
await _connection.execute(
335343
Sql.named(
336-
'INSERT INTO users (id, email, roles) '
337-
'VALUES (@id, @email, @roles) '
344+
'INSERT INTO users (id, email, app_role, dashboard_role, '
345+
'feed_action_status) VALUES (@id, @email, @app_role, '
346+
'@dashboard_role, @feed_action_status) '
338347
'ON CONFLICT (id) DO NOTHING',
339348
),
340349
parameters: {
341350
'id': adminUser.id,
342351
'email': adminUser.email,
343-
'roles': jsonEncode(adminUser.roles),
352+
'app_role': adminUser.appRole.name,
353+
'dashboard_role': adminUser.dashboardRole.name,
354+
'feed_action_status': jsonEncode(
355+
adminUser.feedActionStatus
356+
.map((key, value) => MapEntry(key.name, value.toJson())),
357+
),
344358
},
345359
);
346360

347361
// Seed default settings and preferences for the admin user.
348-
final adminSettings = UserAppSettings(id: adminUser.id);
349-
final adminPreferences = UserContentPreferences(id: adminUser.id);
362+
final adminSettings = UserAppSettings.fromJson(
363+
userAppSettingsFixturesData
364+
.firstWhere((data) => data['id'] == adminUser.id),
365+
);
366+
final adminPreferences = UserContentPreferences.fromJson(
367+
userContentPreferencesFixturesData
368+
.firstWhere((data) => data['id'] == adminUser.id),
369+
);
350370

351371
await _connection.execute(
352372
Sql.named(
353373
'INSERT INTO user_app_settings (id, user_id, display_settings, '
354-
'language, feed_preferences, engagement_shown_counts, '
355-
'engagement_last_shown_timestamps) VALUES (@id, @user_id, '
356-
'@display_settings, @language, @feed_preferences, '
357-
'@engagement_shown_counts, @engagement_last_shown_timestamps) '
374+
'language, feed_preferences) VALUES (@id, @user_id, '
375+
'@display_settings, @language, @feed_preferences) '
358376
'ON CONFLICT (id) DO NOTHING',
359377
),
360-
parameters: adminSettings.toJson()
361-
..['user_id'] = adminUser.id,
378+
parameters: {
379+
'id': adminSettings.id,
380+
'user_id': adminUser.id,
381+
'display_settings':
382+
jsonEncode(adminSettings.displaySettings.toJson()),
383+
'language': adminSettings.language,
384+
'feed_preferences':
385+
jsonEncode(adminSettings.feedPreferences.toJson()),
386+
},
362387
);
363388

364389
await _connection.execute(
365390
Sql.named(
366391
'INSERT INTO user_content_preferences (id, user_id, '
367-
'followed_categories, followed_sources, followed_countries, '
368-
'saved_headlines) VALUES (@id, @user_id, @followed_categories, '
392+
'followed_topics, followed_sources, followed_countries, '
393+
'saved_headlines) VALUES (@id, @user_id, @followed_topics, '
369394
'@followed_sources, @followed_countries, @saved_headlines) '
370395
'ON CONFLICT (id) DO NOTHING',
371396
),
372-
// Use toJson() to correctly serialize the lists of complex objects
373-
// into a format the database driver can handle for JSONB columns.
374-
parameters: adminPreferences.toJson()
375-
..['user_id'] = adminUser.id,
397+
parameters: {
398+
'id': adminPreferences.id,
399+
'user_id': adminUser.id,
400+
'followed_topics': jsonEncode(
401+
adminPreferences.followedTopics.map((e) => e.toJson()).toList(),
402+
),
403+
'followed_sources': jsonEncode(
404+
adminPreferences.followedSources.map((e) => e.toJson()).toList(),
405+
),
406+
'followed_countries': jsonEncode(
407+
adminPreferences.followedCountries.map((e) => e.toJson()).toList(),
408+
),
409+
'saved_headlines': jsonEncode(
410+
adminPreferences.savedHeadlines.map((e) => e.toJson()).toList(),
411+
),
412+
},
376413
);
377414

378415
await _connection.execute('COMMIT');
379-
_log.info(
380-
'Initial AppConfig and admin user seeding completed successfully.',
381-
);
416+
_log.info('Initial RemoteConfig and admin user seeding completed.');
382417
} catch (e) {
383418
await _connection.execute('ROLLBACK');
384419
rethrow;

0 commit comments

Comments
 (0)