initial commit
This commit is contained in:
92
update_weapon_skills.js
Normal file
92
update_weapon_skills.js
Normal file
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
* Script to update weapon skills in the database by removing "SC Icon.png" strings
|
||||
*/
|
||||
|
||||
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 update weapon skills
|
||||
async function updateWeaponSkills() {
|
||||
const client = await pool.connect();
|
||||
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
|
||||
// Get all trusts with weapon skills
|
||||
const result = await client.query('SELECT id, weapon_skills FROM trusts WHERE weapon_skills IS NOT NULL AND weapon_skills != \'\'');
|
||||
|
||||
let updatedCount = 0;
|
||||
|
||||
// Update each trust's weapon skills
|
||||
for (const row of result.rows) {
|
||||
const originalWeaponSkills = row.weapon_skills;
|
||||
|
||||
// Remove "SC Icon.png" from weapon skills
|
||||
const updatedWeaponSkills = originalWeaponSkills.replace(/SC Icon\.png/g, '');
|
||||
|
||||
// Only update if there was a change
|
||||
if (updatedWeaponSkills !== originalWeaponSkills) {
|
||||
await client.query(
|
||||
'UPDATE trusts SET weapon_skills = $1 WHERE id = $2',
|
||||
[updatedWeaponSkills, row.id]
|
||||
);
|
||||
|
||||
updatedCount++;
|
||||
console.log(`Updated weapon skills for trust ID ${row.id}`);
|
||||
}
|
||||
}
|
||||
|
||||
await client.query('COMMIT');
|
||||
console.log(`Updated weapon skills for ${updatedCount} trusts`);
|
||||
} catch (e) {
|
||||
await client.query('ROLLBACK');
|
||||
console.error('Error updating weapon skills:', e);
|
||||
throw e;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
// Main function
|
||||
async function main() {
|
||||
try {
|
||||
// Update weapon skills
|
||||
await updateWeaponSkills();
|
||||
|
||||
// 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();
|
||||
Reference in New Issue
Block a user