Files
Tasks/backend/migrations/20251110191906-create-quiz-session-questions.js
2025-11-11 00:25:50 +02:00

85 lines
2.5 KiB
JavaScript

'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('quiz_session_questions', {
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'
},
question_order: {
type: Sequelize.INTEGER.UNSIGNED,
allowNull: false,
comment: 'Order of question in the quiz (1-based)'
},
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 linking quiz sessions with questions'
});
// Add indexes
await queryInterface.addIndex('quiz_session_questions', ['quiz_session_id'], {
name: 'idx_qsq_session_id'
});
await queryInterface.addIndex('quiz_session_questions', ['question_id'], {
name: 'idx_qsq_question_id'
});
await queryInterface.addIndex('quiz_session_questions', ['question_order'], {
name: 'idx_qsq_question_order'
});
// Unique composite index to prevent duplicate questions in same session
await queryInterface.addIndex('quiz_session_questions', ['quiz_session_id', 'question_id'], {
name: 'idx_qsq_session_question',
unique: true
});
console.log('✅ Quiz session questions junction table created with 5 fields and 4 indexes');
},
async down(queryInterface, Sequelize) {
await queryInterface.dropTable('quiz_session_questions');
console.log('✅ Quiz session questions table dropped');
}
};