/** * Seed default roles if they don't exist (idempotent). */ module.exports = { async up(queryInterface, Sequelize) { const queries = [ `INSERT INTO roles (code, role, created_at, updated_at) SELECT 'ADMIN', 'Admin', NOW(), NOW() FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM roles WHERE code = 'ADMIN')`, `INSERT INTO roles (code, role, created_at, updated_at) SELECT 'EDITOR', 'Editor', NOW(), NOW() FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM roles WHERE code = 'EDITOR')`, `INSERT INTO roles (code, role, created_at, updated_at) SELECT 'USER', 'User', NOW(), NOW() FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM roles WHERE code = 'USER')` ]; for (const sql of queries) { await queryInterface.sequelize.query(sql); } }, async down(queryInterface, Sequelize) { await queryInterface.sequelize.query( "DELETE FROM roles WHERE code IN ('ADMIN', 'EDITOR', 'USER')" ); } };