const mysql = require('mysql2/promise'); const fs = require('fs'); async function importDb() { console.log("Connecting to database..."); const connection = await mysql.createConnection({ host: '127.0.0.1', user: 'root', password: 'root', multipleStatements: true }); console.log("Connected. Creating 'habbo_retro' database..."); await connection.query("CREATE DATABASE IF NOT EXISTS habbo_retro CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"); await connection.query("USE habbo_retro;"); console.log("Database created and selected."); const sqlPath = '/Users/matt/DEV/hab/retro/database_backup/habbo_retro_complete_20251206_155812.sql'; console.log(`Reading SQL file from ${sqlPath}...`); const sql = fs.readFileSync(sqlPath, 'utf8'); console.log(`SQL file loaded. Size: ${(sql.length / 1024 / 1024).toFixed(2)} MB.`); // Naive split by semicolon + newline to avoid massive packets // This is not perfect but works for most standard dumps const statements = sql.split(/;\r?\n/); console.log(`Parsed ${statements.length} statements.`); let successCount = 0; let errorCount = 0; for (let i = 0; i < statements.length; i++) { const stmt = statements[i].trim(); if (stmt.length > 0) { try { await connection.query(stmt); successCount++; if (i % 50 === 0) process.stdout.write(`\rProgress: ${i}/${statements.length}`); } catch (err) { console.error(`\nError executing statement #${i}: ${err.message.substring(0, 100)}...`); errorCount++; } } } console.log(`\nImport complete.`); console.log(`Success: ${successCount}`); console.log(`Errors: ${errorCount}`); await connection.end(); } importDb().catch(err => { console.error("Fatal error:", err); process.exit(1); });