_exec_sql.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const { Client } = require('pg');
  2. const fs = require('fs');
  3. const SQL_FILE = process.argv[2];
  4. if (!SQL_FILE) {
  5. console.error('Usage: node _exec_sql.js <sql_file>');
  6. process.exit(1);
  7. }
  8. const sql = fs.readFileSync(SQL_FILE, 'utf8');
  9. async function main() {
  10. const client = new Client({
  11. host: '39.98.38.2',
  12. port: 5866,
  13. database: 'highgo',
  14. user: 'highgo',
  15. password: 'Gw@141516',
  16. connectionTimeoutMillis: 30000,
  17. });
  18. try {
  19. await client.connect();
  20. console.log('Connected to database.');
  21. // Start transaction
  22. await client.query('BEGIN');
  23. console.log('Transaction started.');
  24. // Split SQL into individual statements
  25. // Skip comment lines and empty lines
  26. const statements = [];
  27. let current = '';
  28. const lines = sql.split('\n');
  29. for (const line of lines) {
  30. const trimmed = line.trim();
  31. // Skip pure comment lines and empty lines
  32. if (trimmed === '' || trimmed.startsWith('--')) {
  33. if (current.trim()) current += '\n';
  34. continue;
  35. }
  36. current += line + '\n';
  37. // Check if statement is complete (ends with ;)
  38. if (trimmed.endsWith(';')) {
  39. const stmt = current.trim();
  40. if (stmt) statements.push(stmt);
  41. current = '';
  42. }
  43. }
  44. // Don't forget last statement
  45. if (current.trim()) statements.push(current.trim());
  46. console.log(`Total statements to execute: ${statements.length}`);
  47. let ok = 0;
  48. let fail = 0;
  49. for (let i = 0; i < statements.length; i++) {
  50. const stmt = statements[i];
  51. // Get first line for preview
  52. const preview = stmt.split('\n')[0].substring(0, 80);
  53. try {
  54. await client.query(stmt);
  55. ok++;
  56. if ((i + 1) % 50 === 0) {
  57. console.log(` [${i+1}/${statements.length}] ${ok} ok, ${fail} fail`);
  58. }
  59. } catch (err) {
  60. fail++;
  61. console.error(`\n FAIL [#${i+1}]: ${preview}`);
  62. console.error(` Error: ${err.message}`);
  63. // Ask whether to continue or rollback
  64. console.error(` Rolling back entire transaction due to error.`);
  65. await client.query('ROLLBACK');
  66. await client.end();
  67. console.log(`\n=== ROLLED BACK ===`);
  68. console.log(`Executed: ${ok} ok, failed at #${i+1}`);
  69. process.exit(1);
  70. }
  71. }
  72. // Commit transaction
  73. await client.query('COMMIT');
  74. console.log(`\n=== COMMITTED ===`);
  75. console.log(`Total: ${ok} statements executed successfully, ${fail} failed`);
  76. await client.end();
  77. } catch (err) {
  78. console.error('Connection error:', err.message);
  79. try { await client.query('ROLLBACK'); } catch(e) {}
  80. await client.end();
  81. process.exit(1);
  82. }
  83. }
  84. main();