-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit-db.js
More file actions
79 lines (67 loc) · 2.48 KB
/
Copy pathinit-db.js
File metadata and controls
79 lines (67 loc) · 2.48 KB
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
const fs = require('fs');
const path = require('path');
const db = require('./db');
async function initializeDatabase() {
try {
console.log('🔧 Initializing ILOS Database...');
// Read the schema file
const schemaPath = path.join(__dirname, 'schema.sql');
const schema = fs.readFileSync(schemaPath, 'utf8');
// Split the schema into individual statements
const statements = schema
.split(';')
.map(statement => statement.trim())
.filter(statement => statement.length > 0 && !statement.startsWith('--'));
console.log(`📝 Executing ${statements.length} SQL statements...`);
// Execute each statement
for (let i = 0; i < statements.length; i++) {
const statement = statements[i];
if (statement) {
try {
await db.query(statement);
console.log(`✅ Statement ${i + 1}/${statements.length} executed successfully`);
} catch (error) {
// Some statements might fail if tables already exist, that's okay for some cases
if (error.message.includes('already exists')) {
console.log(`⚠️ Statement ${i + 1}/${statements.length} skipped (already exists): ${error.message}`);
} else {
console.error(`❌ Error executing statement ${i + 1}:`, error.message);
console.error(`Statement: ${statement}`);
throw error;
}
}
}
}
// Verify the tables were created
const tablesResult = await db.query(`
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name
`);
console.log('📊 Created tables:');
tablesResult.rows.forEach(row => {
console.log(` - ${row.table_name}`);
});
console.log('✅ Database initialization completed successfully!');
// Test the database with a simple query
const testResult = await db.query('SELECT COUNT(*) as count FROM cif_customers');
console.log(`📈 CIF Customers count: ${testResult.rows[0].count}`);
} catch (error) {
console.error('❌ Database initialization failed:', error);
throw error;
}
}
// Run the initialization if this script is called directly
if (require.main === module) {
initializeDatabase()
.then(() => {
console.log('🎉 Initialization complete!');
process.exit(0);
})
.catch((error) => {
console.error('💥 Initialization failed:', error);
process.exit(1);
});
}
module.exports = { initializeDatabase };