From 674d8e9393078ae8d6992444d2194d0875f74c16 Mon Sep 17 00:00:00 2001 From: Marcos Date: Tue, 28 Jul 2026 18:36:13 +0200 Subject: [PATCH] Rename username column to email in Users table and update existing records for consistency --- src/initDB.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/initDB.js b/src/initDB.js index cbaeec6..1285d7c 100644 --- a/src/initDB.js +++ b/src/initDB.js @@ -2,6 +2,25 @@ 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.');