import { createConnection } from 'mysql2/promise'; import { readFileSync } from 'fs'; import { config } from 'dotenv'; // Load env config(); const dbUrl = process.env.DATABASE_URL; if (!dbUrl) { console.error('DATABASE_URL not set'); process.exit(1); } // Parse MySQL URL: mysql://user:pass@host:port/dbname const url = new URL(dbUrl); const connection = await createConnection({ host: url.hostname, port: parseInt(url.port || '3306'), user: url.username, password: url.password, database: url.pathname.slice(1), ssl: { rejectUnauthorized: false }, multipleStatements: false, }); console.log('Connected to database'); // Check current count const [countRows] = await connection.execute('SELECT COUNT(*) as count FROM `all_games`'); console.log('Current games count:', countRows[0].count); // Read the SQL file const sql = readFileSync('/home/ubuntu/all_games_seed.sql', 'utf8'); // Extract just the INSERT statement (skip comments) const insertSql = sql.split('\n').filter(line => !line.startsWith('--')).join('\n').trim(); console.log('Executing seed SQL...'); try { await connection.execute(insertSql); console.log('Seed SQL executed successfully'); } catch (err) { console.error('Error executing seed SQL:', err.message); // Try splitting into smaller batches console.log('Trying batch insert...'); // Extract values const valuesMatch = insertSql.match(/VALUES\n([\s\S]+)\nON DUPLICATE/); if (!valuesMatch) { console.error('Could not parse SQL values'); process.exit(1); } const allValues = valuesMatch[1].split('),\n('); const firstValue = allValues[0].replace(/^\(/, ''); const lastValue = allValues[allValues.length - 1].replace(/\)$/, ''); allValues[0] = firstValue; allValues[allValues.length - 1] = lastValue; const header = `INSERT INTO \`all_games\` ( \`gameId\`, \`gameName\`, \`gameType\`, \`category\`, \`provider\`, \`rtp\`, \`volatility\`, \`paylines\`, \`reels\`, \`minBet\`, \`maxBet\`, \`jackpot\`, \`hasIcon\`, \`iconUrl\`, \`isActive\`, \`isFeatured\`, \`playCount\` ) VALUES `; const footer = `\nON DUPLICATE KEY UPDATE \`gameName\` = VALUES(\`gameName\`), \`iconUrl\` = VALUES(\`iconUrl\`), \`isFeatured\` = VALUES(\`isFeatured\`);`; const batchSize = 50; let inserted = 0; for (let i = 0; i < allValues.length; i += batchSize) { const batch = allValues.slice(i, i + batchSize); const batchSql = header + '(' + batch.join('),(') + ')' + footer; try { await connection.execute(batchSql); inserted += batch.length; process.stdout.write(`\rInserted ${inserted}/${allValues.length} games...`); } catch (batchErr) { console.error(`\nBatch ${i}-${i+batchSize} failed:`, batchErr.message); } } console.log(`\nBatch insert complete: ${inserted} games`); } // Verify final count const [finalRows] = await connection.execute('SELECT COUNT(*) as count FROM `all_games`'); console.log('Final games count:', finalRows[0].count); // Show sample const [sampleRows] = await connection.execute('SELECT gameId, gameName, provider, iconUrl FROM `all_games` WHERE iconUrl != "" LIMIT 5'); console.log('Sample games with thumbnails:'); sampleRows.forEach(r => console.log(` ${r.gameId}: ${r.gameName} (${r.provider}) - ${r.iconUrl.substring(0, 60)}...`)); await connection.end(); console.log('Done!');