'use strict'; /** @type {import('sequelize-cli').Migration} */ module.exports = { async up(queryInterface, Sequelize) { // Normalize existing users' role text to canonical codes and set role_id // 1) General normalization: match users.role to roles.code or roles.role (case-insensitive) await queryInterface.sequelize.query(` UPDATE users u JOIN roles r ON LOWER(r.code) = LOWER(u.role) OR LOWER(r.role) = LOWER(u.role) SET u.role_id = r.id, u.role = r.code WHERE u.role_id IS NULL OR u.role <> r.code; `); // 2) Fallback: ensure the known admin email is set correctly, if present await queryInterface.sequelize.query(` UPDATE users u SET u.role = 'ADMIN', u.role_id = (SELECT id FROM roles WHERE code = 'ADMIN' LIMIT 1) WHERE u.email = 'admin@example.com'; `); }, async down(queryInterface, Sequelize) { // Best-effort revert for the demo admin only await queryInterface.sequelize.query(` UPDATE users SET role = 'admin', role_id = NULL WHERE email = 'admin@example.com'; `); } };