'use strict'; /** @type {import('sequelize-cli').Migration} */ module.exports = { async up(queryInterface, Sequelize) { await queryInterface.createTable('quiz_answers', { id: { type: Sequelize.CHAR(36), primaryKey: true, allowNull: false, comment: 'UUID primary key' }, quiz_session_id: { type: Sequelize.CHAR(36), allowNull: false, references: { model: 'quiz_sessions', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'CASCADE', comment: 'Foreign key to quiz_sessions table' }, question_id: { type: Sequelize.CHAR(36), allowNull: false, references: { model: 'questions', key: 'id' }, onUpdate: 'CASCADE', onDelete: 'CASCADE', comment: 'Foreign key to questions table' }, selected_option: { type: Sequelize.STRING(255), allowNull: false, comment: 'The option selected by the user' }, is_correct: { type: Sequelize.BOOLEAN, allowNull: false, comment: 'Whether the selected answer was correct' }, points_earned: { type: Sequelize.INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: 'Points earned for this answer' }, time_taken: { type: Sequelize.INTEGER.UNSIGNED, allowNull: false, defaultValue: 0, comment: 'Time taken to answer in seconds' }, answered_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: 'When the question was answered' }, 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: 'Stores individual answers given during quiz sessions' }); // Add indexes await queryInterface.addIndex('quiz_answers', ['quiz_session_id'], { name: 'idx_quiz_answers_session_id' }); await queryInterface.addIndex('quiz_answers', ['question_id'], { name: 'idx_quiz_answers_question_id' }); await queryInterface.addIndex('quiz_answers', ['is_correct'], { name: 'idx_quiz_answers_is_correct' }); await queryInterface.addIndex('quiz_answers', ['answered_at'], { name: 'idx_quiz_answers_answered_at' }); // Composite index for session + question (unique constraint) await queryInterface.addIndex('quiz_answers', ['quiz_session_id', 'question_id'], { name: 'idx_quiz_answers_session_question', unique: true }); console.log('✅ Quiz answers table created successfully with 9 fields and 5 indexes'); }, async down(queryInterface, Sequelize) { await queryInterface.dropTable('quiz_answers'); console.log('✅ Quiz answers table dropped'); } };