/** * Database Initialization Script * Run: npm run init-db * Creates the required MySQL database and tables. */ require('dotenv').config(); const mysql = require('mysql2/promise'); async function initDatabase() { const connection = await mysql.createConnection({ host: process.env.MYSQL_HOST || 'localhost', port: parseInt(process.env.MYSQL_PORT) || 3306, user: process.env.MYSQL_USER || 'root', password: process.env.MYSQL_PASSWORD || '' }); const dbName = process.env.MYSQL_DATABASE || 'display_wall'; console.log('šŸ”§ Initializing database...\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.query(`USE \`${dbName}\``); // Create prometheus_sources table await connection.query(` CREATE TABLE IF NOT EXISTS prometheus_sources ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, url VARCHAR(500) NOT NULL, description TEXT DEFAULT '', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci `); console.log(' āœ… Table "prometheus_sources" ready'); console.log('\nšŸŽ‰ Database initialization complete!\n'); await connection.end(); } initDatabase().catch(err => { console.error('āŒ Database initialization failed:', err.message); process.exit(1); });