@@ -8,6 +8,7 @@ import 'package:ht_api/src/services/auth_token_service.dart';
8
8
import 'package:ht_api/src/services/dashboard_summary_service.dart' ;
9
9
import 'package:ht_api/src/services/default_user_preference_limit_service.dart' ;
10
10
import 'package:ht_api/src/services/jwt_auth_token_service.dart' ;
11
+ import 'package:ht_api/src/services/database_seeding_service.dart' ;
11
12
import 'package:ht_api/src/services/token_blacklist_service.dart' ;
12
13
import 'package:ht_api/src/services/user_preference_limit_service.dart' ;
13
14
import 'package:ht_api/src/services/verification_code_storage_service.dart' ;
@@ -92,7 +93,17 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
92
93
);
93
94
_log.info ('PostgreSQL database connection established.' );
94
95
95
- // 3. Initialize Repositories
96
+ // 3. Initialize and run database seeding
97
+ // This runs on every startup. The operations are idempotent (`IF NOT EXISTS`,
98
+ // `ON CONFLICT DO NOTHING`), so it's safe to run every time. This ensures
99
+ // the database is always in a valid state, especially for first-time setup
100
+ // in any environment.
101
+ final seedingService = DatabaseSeedingService (connection: _connection, log: _log);
102
+ await seedingService.createTables ();
103
+ await seedingService.seedGlobalFixtureData ();
104
+ await seedingService.seedInitialAdminAndConfig ();
105
+
106
+ // 4. Initialize Repositories
96
107
final headlineRepository = _createRepository <Headline >(
97
108
tableName: 'headlines' ,
98
109
fromJson: Headline .fromJson,
@@ -135,7 +146,7 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
135
146
toJson: (c) => c.toJson (),
136
147
);
137
148
138
- // 4 . Initialize Services
149
+ // 5 . Initialize Services
139
150
const uuid = Uuid ();
140
151
const emailRepository = HtEmailRepository (
141
152
emailClient: HtEmailInMemoryClient (),
@@ -167,7 +178,7 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
167
178
appConfigRepository: appConfigRepository,
168
179
);
169
180
170
- // 5 . Create the main handler with all dependencies provided
181
+ // 6 . Create the main handler with all dependencies provided
171
182
final finalHandler = handler
172
183
// Foundational utilities
173
184
.use (provider <Uuid >((_) => uuid))
@@ -206,15 +217,15 @@ Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
206
217
),
207
218
);
208
219
209
- // 6 . Start the server
220
+ // 7 . Start the server
210
221
final server = await serve (
211
222
finalHandler,
212
223
ip,
213
224
port,
214
225
);
215
226
_log.info ('Server listening on port ${server .port }' );
216
227
217
- // 7 . Handle graceful shutdown
228
+ // 8 . Handle graceful shutdown
218
229
ProcessSignal .sigint.watch ().listen ((_) async {
219
230
_log.info ('Received SIGINT. Shutting down...' );
220
231
await _connection.close ();
0 commit comments