35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
|
|
const db = require('../src/db');
|
||
|
|
|
||
|
|
const updateSchema = async () => {
|
||
|
|
try {
|
||
|
|
console.log('Updating database schema...');
|
||
|
|
await db.query(`
|
||
|
|
DO $$
|
||
|
|
BEGIN
|
||
|
|
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'upgrade_log' AND column_name = 'session_id') THEN
|
||
|
|
ALTER TABLE upgrade_log ADD COLUMN session_id UUID;
|
||
|
|
END IF;
|
||
|
|
END $$;
|
||
|
|
`);
|
||
|
|
console.log('Added session_id column.');
|
||
|
|
|
||
|
|
try {
|
||
|
|
await db.query(`ALTER TABLE upgrade_log DROP CONSTRAINT IF EXISTS upgrade_log_pkey`);
|
||
|
|
console.log('Dropped old primary key constraint.');
|
||
|
|
} catch (e) {
|
||
|
|
console.log('Primary key might not exist or different name:', e.message);
|
||
|
|
}
|
||
|
|
|
||
|
|
await db.query(`ALTER TABLE upgrade_log ADD PRIMARY KEY (uuid)`);
|
||
|
|
console.log('Set uuid as Primary Key.');
|
||
|
|
|
||
|
|
console.log('Schema update completed successfully.');
|
||
|
|
process.exit(0);
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Schema update failed:', error);
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
updateSchema();
|