File size: 2,622 Bytes
1338bd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const fs = require('fs');
const path = require('path');
const { Pool } = require('pg');
require('dotenv').config();

const pool = new Pool({
  host: process.env.DB_HOST || 'localhost',
  port: parseInt(process.env.DB_PORT || '5432'),
  database: process.env.DB_NAME || 'scoreai',
  user: process.env.DB_USER || 'postgres',
  password: process.env.DB_PASSWORD || 'password',
  ssl: process.env.DB_SSL === 'true' ? { rejectUnauthorized: false } : false,
});

async function initDatabase() {
  console.log('[INIT] Starting database initialization...');
  
  try {
    // Read SQL file
    const sqlFile = path.join(__dirname, '..', 'database_schema_postgresql.sql');
    const sql = fs.readFileSync(sqlFile, 'utf8');
    
    console.log('[INIT] SQL file loaded, executing...');
    
    // Execute SQL as a single transaction
    try {
      await pool.query(sql);
      console.log('[INIT] ✅ Database tables created successfully');
    } catch (error) {
      if (error.code === '42P07') {
        console.log('[INIT] ⚠️  Some tables already exist, checking data...');
      } else {
        throw error;
      }
    }
    
    // Verify tables
    const tablesResult = await pool.query(`
      SELECT table_name 
      FROM information_schema.tables 
      WHERE table_schema = 'public'
      ORDER BY table_name
    `);
    
    console.log('[INIT] Tables created:');
    tablesResult.rows.forEach(row => {
      console.log(`  - ${row.table_name}`);
    });
    
    // Verify subjects data
    const subjectsResult = await pool.query('SELECT COUNT(*) as count FROM subjects');
    console.log(`[INIT] Subjects inserted: ${subjectsResult.rows[0].count}`);
    
    // Verify classes data
    const classesResult = await pool.query('SELECT COUNT(*) as count FROM classes');
    console.log(`[INIT] Classes inserted: ${classesResult.rows[0].count}`);
    
    // Verify exams data
    const examsResult = await pool.query('SELECT COUNT(*) as count FROM exams');
    console.log(`[INIT] Exams inserted: ${examsResult.rows[0].count}`);
    
    // Verify users data
    const usersResult = await pool.query('SELECT COUNT(*) as count FROM users');
    console.log(`[INIT] Users inserted: ${usersResult.rows[0].count}`);
    
  } catch (error) {
    console.error('[INIT] ❌ Database initialization failed:', error.message);
    throw error;
  } finally {
    await pool.end();
  }
}

initDatabase()
  .then(() => {
    console.log('[INIT] Database initialization completed');
    process.exit(0);
  })
  .catch((error) => {
    console.error('[INIT] Database initialization error:', error);
    process.exit(1);
  });