/** * Script to add alt_name column to trusts table */ const fs = require('fs'); const { Pool } = require('pg'); const path = require('path'); // Read database configuration const dbConfFile = fs.readFileSync('db.conf', 'utf8'); const dbConfig = {}; // Parse the db.conf file dbConfFile.split('\n').forEach(line => { if (line.trim() === '') return; const [key, value] = line.split('='); if (key && value) { // Remove quotes if present const cleanValue = value.replace(/^['"]|['"]$/g, ''); dbConfig[key] = cleanValue; } }); // Configure PostgreSQL connection const pool = new Pool({ user: dbConfig.PSQL_USER, host: dbConfig.PSQL_HOST, database: dbConfig.PSQL_DBNAME, password: dbConfig.PSQL_PASSWORD, port: dbConfig.PSQL_PORT, }); // Function to execute SQL from a file async function executeSqlFile(filePath) { const client = await pool.connect(); try { const sql = fs.readFileSync(filePath, 'utf8'); await client.query('BEGIN'); await client.query(sql); await client.query('COMMIT'); console.log(`SQL file ${filePath} executed successfully`); return true; } catch (e) { await client.query('ROLLBACK'); console.error(`Error executing SQL file ${filePath}:`, e); return false; } finally { client.release(); } } // Main function async function main() { try { // Add the alt_name column console.log('Adding alt_name column...'); const success = await executeSqlFile('add_alt_name_column.sql'); if (success) { console.log('alt_name column added successfully'); } else { console.log('Failed to add alt_name column. It might already exist.'); } // Close the pool await pool.end(); } catch (e) { console.error('Error in main process:', e); process.exit(1); } } // Run the main function main();