Files
notas-api/src/initDB.js
T
2026-07-28 18:36:13 +02:00

35 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const sequelize = require('./config/database');
async function initDB() {
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 });
console.log('✅ Base de datos conectada y tablas sincronizadas.');
// console.log('️ Creación de usuario por defecto desactivada.');
} catch (error) {
console.error('❌ Error fatal al iniciar la base de datos:', error);
process.exit(1);
}
}
module.exports = initDB;