Dealer_Onboarding_Backend/scripts/migrate-relocation-schema.ts

74 lines
2.7 KiB
TypeScript

/**
* Migration Script: Add newDistrictId and newStateId to RelocationRequest
* Run: npx ts-node scripts/migrate-relocation-schema.ts
*/
import db from '../src/database/models/index.js';
async function migrate() {
const queryInterface = db.sequelize.getQueryInterface();
try {
console.log('Starting relocation schema migration...');
// Get table description to check existing columns
const tableInfo = await queryInterface.describeTable('relocation_requests');
// Add newDistrictId column if not exists
if (!tableInfo.newDistrictId) {
console.log('Adding newDistrictId column...');
await queryInterface.addColumn('relocation_requests', 'newDistrictId', {
type: db.Sequelize.DataTypes.UUID,
allowNull: true,
references: {
model: 'districts',
key: 'id'
}
});
console.log('✓ newDistrictId column added');
} else {
console.log('- newDistrictId column already exists');
}
// Add newStateId column if not exists
if (!tableInfo.newStateId) {
console.log('Adding newStateId column...');
await queryInterface.addColumn('relocation_requests', 'newStateId', {
type: db.Sequelize.DataTypes.UUID,
allowNull: true,
references: {
model: 'states',
key: 'id'
}
});
console.log('✓ newStateId column added');
} else {
console.log('- newStateId column already exists');
}
// Update enum to include 'Intercity' if not already present
console.log('Checking relocationType enum...');
try {
await db.sequelize.query(`
ALTER TYPE "enum_relocation_requests_relocationType"
ADD VALUE IF NOT EXISTS 'Intercity';
`);
console.log('✓ Intercity added to enum (if not already present)');
} catch (enumError: any) {
// PostgreSQL doesn't support IF NOT EXISTS for enum values in some versions
if (enumError.code === '42710') {
console.log('- Intercity already exists in enum');
} else {
console.log('Warning: Could not update enum:', enumError.message);
}
}
console.log('\n✅ Migration completed successfully!');
} catch (error) {
console.error('❌ Migration failed:', error);
process.exit(1);
} finally {
await db.sequelize.close();
}
}
migrate();