Rename username column to email in Users table and update existing records for consistency
Despliegue Automático / desplegar (push) Successful in 43s
Despliegue Automático / desplegar (push) Successful in 43s
This commit is contained in:
@@ -2,6 +2,25 @@ const sequelize = require('./config/database');
|
|||||||
|
|
||||||
async function initDB() {
|
async function initDB() {
|
||||||
try {
|
try {
|
||||||
|
const queryInterface = sequelize.getQueryInterface();
|
||||||
|
const tableName = 'Users';
|
||||||
|
|
||||||
|
if (await queryInterface.tableExists(tableName)) {
|
||||||
|
const tableDescription = await queryInterface.describeTable(tableName);
|
||||||
|
|
||||||
|
if (tableDescription.username && !tableDescription.email) {
|
||||||
|
await queryInterface.renameColumn(tableName, 'username', 'email');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tableDescription.username && tableDescription.email) {
|
||||||
|
await sequelize.query(
|
||||||
|
`UPDATE "${tableName}"
|
||||||
|
SET "email" = COALESCE("email", "username")
|
||||||
|
WHERE "email" IS NULL AND "username" IS NOT NULL;`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await sequelize.sync({ alter: true });
|
await sequelize.sync({ alter: true });
|
||||||
console.log('✅ Base de datos conectada y tablas sincronizadas.');
|
console.log('✅ Base de datos conectada y tablas sincronizadas.');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user