Files
yunhaifinance/backend/fix-returning.js
T

40 lines
1.5 KiB
JavaScript
Raw Normal View History

const fs = require('fs');
const path = require('path');
const dir = '/opt/company-finance-system/backend/routes';
const files = fs.readdirSync(dir).filter(f => f.endsWith('.js'));
for (const file of files) {
const filePath = path.join(dir, file);
let content = fs.readFileSync(filePath, 'utf8');
const original = content;
// Add RETURNING id to INSERT statements that don't already have it
// Match: INSERT INTO ... VALUES (...); followed by parameter array
// We look for the pattern where VALUES ends with a closing paren and is followed by the array literal
const lines = content.split('\n');
let modified = false;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
// Check if this line contains VALUES (...) ending and the next line starts the array
if (line.match(/VALUES\s*\(.*\)/) && !line.includes('RETURNING')) {
// Check if this INSERT is into a main table (has a corresponding db.query call)
// We need to add RETURNING id before the closing backtick
if (line.includes('CURRENT_TIMESTAMP)') || line.match(/\)\s*`?\s*$/)) {
// Replace the trailing )` or ), with RETURNING id
lines[i] = line.replace(/CURRENT_TIMESTAMP\)\s*`?\s*$/, "CURRENT_TIMESTAMP)\n RETURNING id`");
if (lines[i] !== line) modified = true;
}
}
}
if (modified) {
fs.writeFileSync(filePath, lines.join('\n'));
console.log('Fixed: ' + file);
} else {
console.log('No change: ' + file);
}
}