'use strict';

module.exports = {
  async up(queryInterface) {
    // Backfill PurchaseRequisition.department_id for PRs created from Material Requests
    // MaterialRequest has purchase_requisition_id and department_id.
    await queryInterface.sequelize.query(
      `
      UPDATE purchase_requisition pr
      JOIN material_request mr ON mr.purchase_requisition_id = pr.id
      SET pr.department_id = mr.department_id
      WHERE pr.department_id IS NULL
        AND mr.department_id IS NOT NULL
      `
    );
  },

  async down() {
    // No safe rollback for data backfill
  },
};


