const path = require('path'); require('dotenv').config({ path: path.join(__dirname, '..', '.env') }); const mysql = require('mysql2/promise'); const checkAndFixDatabase = require('./db-schema-check'); const db = require('./db'); async function initDatabase() { const host = process.env.MYSQL_HOST || 'localhost'; const port = parseInt(process.env.MYSQL_PORT) || 3306; const user = process.env.MYSQL_USER || 'root'; const password = process.env.MYSQL_PASSWORD || ''; const dbName = process.env.MYSQL_DATABASE || 'display_wall'; // 1. Create connection without database selected to create the DB itself const connection = await mysql.createConnection({ host, port, user, password }); console.log('šŸ”§ Initializing database environment...\n'); // Create database await connection.query(`CREATE DATABASE IF NOT EXISTS \`${dbName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`); console.log(` āœ… Database "${dbName}" ready`); await connection.end(); // 2. Re-initialize the standard pool so it can see the new DB db.initPool(); // 3. Use the centralized schema tool to create/fix all tables console.log(' šŸ“¦ Initializing tables using schema-check tool...'); await checkAndFixDatabase(); console.log(' āœ… Tables and columns ready'); console.log('\nšŸŽ‰ Database initialization complete!\n'); } initDatabase().catch(err => { console.error('āŒ Database initialization failed:', err.message); process.exit(1); });