37 lines
1.5 KiB
JavaScript
37 lines
1.5 KiB
JavaScript
const db = require('./backend/db-sqlite');
|
|
async function fix() {
|
|
// Drop the failed new table first
|
|
try { await db.query('DROP TABLE IF EXISTS payment_plans_new'); } catch(e) {}
|
|
|
|
// Get current columns
|
|
const info = await db.query("PRAGMA table_info(payment_plans)");
|
|
const cols = info.rows.map(r => r.name);
|
|
console.log('Current payment_plans columns:', cols.join(', '));
|
|
|
|
// Create new table with all columns + nullable purchase_order_id
|
|
const newCols = [...cols];
|
|
if (!newCols.includes('purchase_order_id')) newCols.push('purchase_order_id');
|
|
|
|
const colDefs = newCols.map(c => {
|
|
if (c === 'id') return 'id INTEGER PRIMARY KEY AUTOINCREMENT';
|
|
if (c === 'purchase_order_id') return 'purchase_order_id INTEGER DEFAULT 0';
|
|
if (c === 'created_at' || c === 'updated_at') return `${c} DATETIME DEFAULT CURRENT_TIMESTAMP`;
|
|
if (c.includes('amount')) return `${c} REAL DEFAULT 0`;
|
|
return `${c} TEXT DEFAULT ''`;
|
|
});
|
|
|
|
try {
|
|
await db.query(`CREATE TABLE payment_plans_new (${colDefs.join(', ')})`);
|
|
// Copy data with column names
|
|
const colList = cols.join(', ');
|
|
await db.query(`INSERT INTO payment_plans_new (${colList}) SELECT ${colList} FROM payment_plans`);
|
|
await db.query(`DROP TABLE payment_plans`);
|
|
await db.query(`ALTER TABLE payment_plans_new RENAME TO payment_plans`);
|
|
console.log('OK: payment_plans table fixed');
|
|
} catch(e) {
|
|
console.log('ERR:', e.message.substring(0, 150));
|
|
}
|
|
|
|
process.exit();
|
|
}
|
|
fix(); |