@@ -7,49 +7,74 @@ import { yearFrom, yearTo } from "./util/config.js";
77setPool ( process . env . PG_URI ?? '' ) ;
88
99async function initDatabase ( ) {
10+ console . log ( '[DB] Creating tables if they do not exist...' ) ;
11+ await createTables ( ) ;
12+ console . log ( '[DB] Tables created successfully.' ) ;
1013 try {
14+ console . log ( '[DB] Checking if database is ready...' ) ;
1115 if ( ! await dbIsReady ( ) ) {
12- console . log ( 'Database is not ready, creating tables...' )
13- await createTables ( )
16+ console . log ( '[DB] Database is not ready, creating tables...' ) ;
17+
18+ console . log ( '[DB] Tables created successfully.' ) ;
19+ } else {
20+ console . log ( '[DB] Database is ready.' ) ;
1421 }
1522
16- await clearStatusRows ( )
17- await addStatusRow ( { message : 'updating' , from : yearFrom ( ) , to : yearTo ( ) } , true )
23+ await clearStatusRows ( ) ;
24+ await addStatusRow ( { message : 'updating' , from : yearFrom ( ) , to : yearTo ( ) } , true ) ;
25+ console . log ( '[DB] Status row inserted with updating=true' ) ;
1826
19- console . log ( 'Database is ready.' )
20- const { upToDate, statutes, judgments } = await dbIsUpToDate ( )
27+ console . log ( '[DB] Checking if database is up to date...' ) ;
28+ const { upToDate, statutes, judgments } = await dbIsUpToDate ( ) ;
2129
22- console . log ( upToDate , 'statutes' , statutes . length , 'judgements' , judgments . length )
30+ console . log ( '[DB] Up to date:' , upToDate , 'missing statutes: ' , statutes . length , 'missing judgements:' , judgments . length ) ;
2331
2432 if ( ! upToDate ) {
25- console . log ( 'Database is not up to date, filling database...' )
26- await fillDb ( statutes , judgments )
27- console . log ( 'Database is now up to date.' )
33+ console . log ( '[DB] Database is not up to date, filling database...' ) ;
34+ await fillDb ( statutes , judgments ) ;
35+ console . log ( '[DB] Database is now up to date.' ) ;
2836 } else {
29- console . log ( 'Database is up to date.' )
37+ console . log ( '[DB] Database is up to date.' ) ;
3038 }
3139
3240 } catch ( error ) {
33- console . error ( 'Error initializing database:' , error )
34- exit ( 1 )
41+ console . error ( '[DB] Error initializing database:' , error ) ;
42+ throw error ; // Re-throw so runSetup() catches it and writes error status
3543 }
3644}
3745
3846export const runSetup = async ( ) => {
39- if ( process . env . NODE_ENV === 'test' ) {
40- await setupTestDatabase ( )
41- } else {
42- await initDatabase ( ) ;
43- await deleteCollection ( 'statutes' , 'fin' ) ;
44- await deleteCollection ( 'statutes' , 'swe' ) ;
45- await deleteCollection ( 'judgments' , 'fin' ) ;
46- await deleteCollection ( 'judgments' , 'swe' ) ;
47- await syncStatutes ( 'fin' ) ;
48- await syncStatutes ( 'swe' ) ;
49- await syncJudgments ( 'fin' ) ;
50- await syncJudgments ( 'swe' ) ;
51-
52- await addStatusRow ( { message : 'updated' , from : yearFrom ( ) , to : yearTo ( ) } , false )
47+ await createTables ( ) ;
48+ try {
49+ if ( process . env . NODE_ENV === 'test' ) {
50+ console . log ( '[SETUP] Running in test mode...' ) ;
51+ await setupTestDatabase ( ) ;
52+ } else {
53+ console . log ( '[SETUP] Running in production mode...' ) ;
54+
55+ console . log ( '[SETUP] Step 1: Initialize database...' ) ;
56+ await initDatabase ( ) ;
57+
58+ console . log ( '[SETUP] Step 2: Clear and sync Typesense collections...' ) ;
59+ await deleteCollection ( 'statutes' , 'fin' ) ;
60+ await deleteCollection ( 'statutes' , 'swe' ) ;
61+ await deleteCollection ( 'judgments' , 'fin' ) ;
62+ await deleteCollection ( 'judgments' , 'swe' ) ;
63+
64+ console . log ( '[SETUP] Step 3: Sync statutes to Typesense...' ) ;
65+ await syncStatutes ( 'fin' ) ;
66+ await syncStatutes ( 'swe' ) ;
67+
68+ console . log ( '[SETUP] Step 4: Sync judgments to Typesense...' ) ;
69+ await syncJudgments ( 'fin' ) ;
70+ await syncJudgments ( 'swe' ) ;
71+
72+ console . log ( '[SETUP] Step 5: Write completion status...' ) ;
73+ await addStatusRow ( { message : 'updated' , from : yearFrom ( ) , to : yearTo ( ) , timestamp : new Date ( ) . toISOString ( ) } , false ) ;
74+ }
75+ console . log ( '[SETUP] Database setup done.' ) ;
76+ } catch ( error ) {
77+ console . error ( '[SETUP] Setup failed:' , error ) ;
78+ throw error ; // Re-throw so app.ts error handler can write error status
5379 }
54- console . log ( 'Database setup done.' ) ;
5580}
0 commit comments