87 lines
2.0 KiB
JavaScript
87 lines
2.0 KiB
JavaScript
/**
|
|
* Script to add an "acquired" column to the trusts table
|
|
*/
|
|
|
|
const { Pool } = require('pg');
|
|
const fs = require('fs');
|
|
|
|
// 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 add the acquired column
|
|
async function addAcquiredColumn() {
|
|
const client = await pool.connect();
|
|
|
|
try {
|
|
await client.query('BEGIN');
|
|
|
|
// Check if the column already exists
|
|
const checkResult = await client.query(`
|
|
SELECT column_name
|
|
FROM information_schema.columns
|
|
WHERE table_name = 'trusts' AND column_name = 'acquired'
|
|
`);
|
|
|
|
if (checkResult.rows.length === 0) {
|
|
// Add the acquired column with a default value of false
|
|
await client.query(`
|
|
ALTER TABLE trusts
|
|
ADD COLUMN acquired BOOLEAN NOT NULL DEFAULT false
|
|
`);
|
|
|
|
console.log('Added "acquired" column to trusts table');
|
|
} else {
|
|
console.log('The "acquired" column already exists in the trusts table');
|
|
}
|
|
|
|
await client.query('COMMIT');
|
|
} catch (e) {
|
|
await client.query('ROLLBACK');
|
|
console.error('Error adding acquired column:', e);
|
|
throw e;
|
|
} finally {
|
|
client.release();
|
|
}
|
|
}
|
|
|
|
// Main function
|
|
async function main() {
|
|
try {
|
|
// Add the acquired column
|
|
await addAcquiredColumn();
|
|
|
|
// Close the pool
|
|
await pool.end();
|
|
|
|
console.log('Database update completed successfully');
|
|
} catch (e) {
|
|
console.error('Error updating database:', e);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
// Run the main function
|
|
main();
|