'use strict'; const bcrypt = require('bcrypt'); /** @type {import('sequelize-cli').Migration} */ module.exports = { async up (queryInterface, Sequelize) { // Check if the user already exists const [existingUsers] = await queryInterface.sequelize.query( 'SELECT id FROM users WHERE email = ?;', { replacements: ['admin@example.com'] } ); // If the user doesn't exist, insert it if (existingUsers.length === 0) { const hashedPassword = await bcrypt.hash('admin123', 10); await queryInterface.bulkInsert('users', [{ username: 'admin', email: 'admin@example.com', password: hashedPassword, // Use standardized role code and set role_id if the column exists role: 'ADMIN', role_id: Sequelize.literal("(SELECT id FROM roles WHERE code = 'ADMIN' LIMIT 1)"), created_at: new Date(), updated_at: new Date() }], {}); } }, async down (queryInterface, Sequelize) { await queryInterface.bulkDelete('users', { email: 'admin@example.com' }, {}); } };