Files
starr-philproperties/database/migrations/20260715000005-create-quiz-sessions.js
T
2026-07-15 21:34:21 +08:00

39 lines
2.2 KiB
JavaScript

'use strict';
// Backfill migration: this table already exists live (created outside of
// sequelize-cli before migration coverage was complete). Written to match
// the live CockroachDB schema exactly so a fresh install/cutover recreates
// it. On the existing production DB, mark this filename as already applied
// in SequelizeMeta instead of running it — see project notes.
//
// Note: 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');
},
};