44 lines
1.5 KiB
JavaScript
44 lines
1.5 KiB
JavaScript
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);
|
|
});
|