Files
starr-philproperties/database/migrations/20260709000005-add-order-index-and-is-required-to-tasks.js
T

40 lines
1.3 KiB
JavaScript

'use strict';
// Part 1B — task_lists' tasks currently sort only by createdAt (no ordering
// column exists). Add order_index (position within the list) and is_required
// (mirrors unit_quizzes.is_required — an optional task doesn't block anything
// after it in the sequencing lock).
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn('tasks', 'order_index', {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0,
after: 'deadline',
});
await queryInterface.addColumn('tasks', 'is_required', {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: true,
after: 'order_index',
});
// Backfill existing rows with a stable order matching current createdAt
// sort, so tasks don't all collapse to order_index 0 on first use.
await queryInterface.sequelize.query(`
UPDATE tasks t SET order_index = sub.rn - 1
FROM (
SELECT task_id, ROW_NUMBER() OVER (PARTITION BY task_list_id ORDER BY "createdAt" ASC) AS rn
FROM tasks
) sub
WHERE t.task_id = sub.task_id
`);
},
async down(queryInterface) {
await queryInterface.removeColumn('tasks', 'is_required');
await queryInterface.removeColumn('tasks', 'order_index');
},
};