Skip to content

Commit 816df73

Browse files
committed
Debuggaushommia
1 parent 83361bc commit 816df73

File tree

4 files changed

+81
-32
lines changed

4 files changed

+81
-32
lines changed

backend/src/app.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import keywordRouter from './controllers/keyword.js';
77
import { fileURLToPath } from 'url';
88
import { runSetup } from './dbSetup.js';
99
import { getLatestStatusEntry, getAllStatusEntries, clearAllStatusEntries } from './db/models/status.js';
10+
import { addStatusRow, createTables } from './db/db.js';
1011

1112
const app = express()
1213
const __filename = fileURLToPath(import.meta.url);
@@ -26,7 +27,8 @@ app.get('/api/check-db-status', async (req: express.Request, res: express.Respon
2627
}
2728
});
2829

29-
app.get('/api/ping', (req, res) => {
30+
app.get('/api/ping', async (req, res) => {
31+
await createTables();
3032
res.send({ data: 'pong' })
3133
})
3234

@@ -36,14 +38,29 @@ app.post('/api/setup', async (req: express.Request, res: express.Response): Prom
3638

3739
setImmediate(async () => {
3840
try {
41+
console.log('[SETUP] Starting database setup...');
3942
await runSetup();
40-
console.log('Setup completed successfully');
43+
console.log('[SETUP] Setup completed successfully');
4144
} catch (error) {
42-
console.error('Setup failed:', error);
45+
console.error('[SETUP] Setup failed with error:', error);
46+
const errorMessage = error instanceof Error ? error.message : String(error);
47+
try {
48+
await addStatusRow(
49+
{
50+
message: 'setup_failed',
51+
error: errorMessage,
52+
timestamp: new Date().toISOString()
53+
},
54+
false
55+
);
56+
console.log('[SETUP] Wrote error status to database');
57+
} catch (dbError) {
58+
console.error('[SETUP] Failed to write error status to database:', dbError);
59+
}
4360
}
4461
});
4562
} catch (error) {
46-
console.error('Setup endpoint error:', error);
63+
console.error('[SETUP] Setup endpoint error:', error);
4764
res.status(500).json({ error: 'Failed to start setup' });
4865
}
4966
});

backend/src/db/db.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ async function dbIsUpToDate(): Promise<{upToDate: boolean, statutes: StatuteKey[
243243

244244
async function createTables(): Promise<void> {
245245
try {
246+
console.log('ennen poolia')
246247
const client = await pool.connect();
248+
console.log("taulut");
247249
await client.query(`
248250
CREATE TABLE IF NOT EXISTS images (
249251
uuid UUID PRIMARY KEY,
@@ -323,6 +325,7 @@ async function createTables(): Promise<void> {
323325
console.error('Error creating tables:', error);
324326
throw error;
325327
}
328+
console.log('Tables created successfully');
326329
}
327330

328331
async function dropTables(): Promise<void> {

backend/src/dbSetup.ts

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,74 @@ import { yearFrom, yearTo } from "./util/config.js";
77
setPool(process.env.PG_URI ?? '');
88

99
async 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

3846
export 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
}

docker-compose.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ services:
99
POSTGRES_DB: testdb
1010
ports:
1111
- "5432:5432"
12+
volumes:
13+
- finlex-db-data:/data
1214
typesense:
1315
build:
1416
context: .
@@ -18,3 +20,5 @@ services:
1820
TYPESENSE_DATA_DIR: /tmp
1921
ports:
2022
- "8108:8108"
23+
volumes:
24+
finlex-db-data:

0 commit comments

Comments
 (0)