'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.addColumn('material_issue', 'department_id', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: {
        model: 'department',
        key: 'id',
      },
      onUpdate: 'CASCADE',
      onDelete: 'SET NULL',
      comment: 'Receiving department for department-based material issues (optional). For MR-based issues this is derived from Material Request.'
    });

    await queryInterface.addIndex('material_issue', ['department_id']);
  },

  async down(queryInterface) {
    try {
      await queryInterface.removeIndex('material_issue', ['department_id']);
    } catch (e) {
      // ignore
    }
    await queryInterface.removeColumn('material_issue', 'department_id');
  }
};


