64 lines
2.0 KiB
TypeScript
64 lines
2.0 KiB
TypeScript
import { Sequelize } from 'sequelize';
|
|
import config from '../src/common/config/database.js';
|
|
|
|
const env = process.env.NODE_ENV || 'development';
|
|
const dbConfig = config[env];
|
|
|
|
const sequelize = new Sequelize(
|
|
dbConfig.database,
|
|
dbConfig.username,
|
|
dbConfig.password,
|
|
{
|
|
host: dbConfig.host,
|
|
port: dbConfig.port,
|
|
dialect: dbConfig.dialect,
|
|
logging: console.log
|
|
}
|
|
);
|
|
|
|
async function migrate() {
|
|
try {
|
|
await sequelize.authenticate();
|
|
console.log('Connected to database.');
|
|
|
|
const queryInterface = sequelize.getQueryInterface();
|
|
|
|
// Check if users table exists
|
|
const tables = await queryInterface.showAllTables();
|
|
if (!tables.includes('users')) {
|
|
console.log('Users table does not exist. Skipping rename.');
|
|
return;
|
|
}
|
|
|
|
// Rename fullName to name
|
|
const columns = await queryInterface.describeTable('users');
|
|
|
|
if (columns.fullName && !columns.name) {
|
|
console.log('Renaming fullName to name...');
|
|
await queryInterface.renameColumn('users', 'fullName', 'name');
|
|
} else if (columns.fullName && columns.name) {
|
|
console.log('Both fullName and name exist. Manual intervention needed.');
|
|
} else {
|
|
console.log('fullName not found or name already exists.');
|
|
}
|
|
|
|
// Rename mobileNumber to phone
|
|
if (columns.mobileNumber && !columns.phone) {
|
|
console.log('Renaming mobileNumber to phone...');
|
|
await queryInterface.renameColumn('users', 'mobileNumber', 'phone');
|
|
} else if (columns.mobileNumber && columns.phone) {
|
|
console.log('Both mobileNumber and phone exist. Manual intervention needed.');
|
|
} else {
|
|
console.log('mobileNumber not found or phone already exists.');
|
|
}
|
|
|
|
console.log('Migration successful.');
|
|
} catch (error) {
|
|
console.error('Migration failed:', error);
|
|
} finally {
|
|
await sequelize.close();
|
|
}
|
|
}
|
|
|
|
migrate();
|