'use strict'; /** @type {import('sequelize-cli').Migration} */ module.exports = { async up(queryInterface, Sequelize) { await queryInterface.createTable('user_achievements', { id: { type: Sequelize.CHAR(36), primaryKey: true, allowNull: false, comment: 'UUID primary key' }, user_id: { type: Sequelize.CHAR(36), allowNull: false, references: { model: 'users', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'CASCADE', comment: 'Foreign key to users table' }, achievement_id: { type: Sequelize.CHAR(36), allowNull: false, references: { model: 'achievements', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'CASCADE', comment: 'Foreign key to achievements table' }, earned_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: 'When the achievement was earned' }, notified: { type: Sequelize.BOOLEAN, allowNull: false, defaultValue: false, comment: 'Whether user has been notified about this achievement' }, created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: 'Record creation timestamp' }, updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: 'Record last update timestamp' } }, { charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: 'Junction table tracking achievements earned by users' }); // Add indexes await queryInterface.addIndex('user_achievements', ['user_id'], { name: 'idx_user_achievements_user_id' }); await queryInterface.addIndex('user_achievements', ['achievement_id'], { name: 'idx_user_achievements_achievement_id' }); await queryInterface.addIndex('user_achievements', ['earned_at'], { name: 'idx_user_achievements_earned_at' }); await queryInterface.addIndex('user_achievements', ['notified'], { name: 'idx_user_achievements_notified' }); // Unique composite index to prevent duplicate achievements await queryInterface.addIndex('user_achievements', ['user_id', 'achievement_id'], { name: 'idx_user_achievements_user_achievement', unique: true }); console.log('✅ User achievements table created with 6 fields and 5 indexes'); }, async down(queryInterface, Sequelize) { await queryInterface.dropTable('user_achievements'); console.log('✅ User achievements table dropped'); } };