Files
yunhaifinance/fix-pp-final.js
T

53 lines
1.7 KiB
JavaScript

const db = require('./backend/db-sqlite');
async function fix() {
// Clean up old failed attempt
try { await db.query('DROP TABLE IF EXISTS payment_plans_new'); console.log('Dropped old _new'); } catch(e) {}
// Get current columns
const info = await db.query("PRAGMA table_info(payment_plans)");
const colNames = info.rows.map(r => r.name);
console.log('Current cols:', colNames.join(', '));
// Build new table definition - all columns with defaults
const colDefs = info.rows.map(r => {
let def = '';
if (r.pk) {
def = `${r.name} INTEGER PRIMARY KEY AUTOINCREMENT`;
} else {
// All columns get defaults so NOT NULL is satisfied
if (r.type.includes('INTEGER')) {
def = `${r.name} INTEGER DEFAULT 0`;
} else if (r.type.includes('REAL')) {
def = `${r.name} REAL DEFAULT 0`;
} else if (r.type.includes('DATETIME')) {
def = `${r.name} DATETIME DEFAULT CURRENT_TIMESTAMP`;
} else {
def = `${r.name} TEXT DEFAULT ''`;
}
}
return def;
});
try {
await db.query(`CREATE TABLE payment_plans_new (${colDefs.join(', ')})`);
console.log('Created new table');
// Copy data
const insertCols = colNames.join(', ');
await db.query(`INSERT INTO payment_plans_new (${insertCols}) SELECT ${insertCols} FROM payment_plans`);
console.log('Copied data');
await db.query(`DROP TABLE payment_plans`);
console.log('Dropped old table');
await db.query(`ALTER TABLE payment_plans_new RENAME TO payment_plans`);
console.log('Renamed table');
console.log('✅ payment_plans table fixed!');
} catch(e) {
console.log('ERR:', e.message);
}
process.exit();
}
fix();