Files
starr-philproperties/apps/api/database/migrations/20270101000071-create-quiz-sessions.js

32 lines
1.8 KiB
JavaScript

'use strict';
// started_at/last_saved_at/createdAt/updatedAt are TIMESTAMP WITHOUT TIME
// ZONE live (unlike most other tables' TIMESTAMPTZ columns), so raw type
// strings are used instead of Sequelize.DATE to match exactly.
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable('quiz_sessions', {
session_id: { type: Sequelize.BIGINT, primaryKey: true, autoIncrement: true },
uuid: { type: Sequelize.UUID, allowNull: false, unique: true, defaultValue: Sequelize.literal('gen_random_uuid()') },
user_id: { type: Sequelize.BIGINT, allowNull: false, references: { model: 'users', key: 'user_id' }, onDelete: 'CASCADE' },
quiz_id: { type: Sequelize.BIGINT, allowNull: false, references: { model: 'unit_quizzes', key: 'quiz_id' }, onDelete: 'CASCADE' },
course_id: { type: Sequelize.BIGINT, allowNull: true },
unit_id: { type: Sequelize.BIGINT, allowNull: true },
status: { type: Sequelize.STRING(20), allowNull: false, defaultValue: 'in_progress' },
draft_answers: { type: Sequelize.JSONB, allowNull: true },
started_at: { type: 'TIMESTAMP', allowNull: false, defaultValue: Sequelize.NOW },
last_saved_at: { type: 'TIMESTAMP', allowNull: true },
createdAt: { type: 'TIMESTAMP', allowNull: false, defaultValue: Sequelize.NOW },
updatedAt: { type: 'TIMESTAMP', allowNull: false, defaultValue: Sequelize.NOW },
});
await queryInterface.addIndex('quiz_sessions', ['quiz_id'], { name: 'idx_qs_quiz_id' });
await queryInterface.addIndex('quiz_sessions', ['status'], { name: 'idx_qs_status' });
await queryInterface.addIndex('quiz_sessions', ['user_id'], { name: 'idx_qs_user_id' });
},
async down(queryInterface) {
await queryInterface.dropTable('quiz_sessions');
},
};