'use strict';

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.addColumn('general_dc', 'vendor_id', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: { model: 'business_partner', key: 'id' },
      onUpdate: 'CASCADE',
      onDelete: 'RESTRICT'
    });
    await queryInterface.addColumn('general_receipt', 'vendor_id', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: { model: 'business_partner', key: 'id' },
      onUpdate: 'CASCADE',
      onDelete: 'RESTRICT'
    });

    await queryInterface.addColumn('general_dc_item', 'machine_id', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: { model: 'machine', key: 'id' },
      onUpdate: 'CASCADE',
      onDelete: 'RESTRICT'
    });
    await queryInterface.addColumn('general_receipt_item', 'machine_id', {
      type: Sequelize.INTEGER,
      allowNull: true,
      references: { model: 'machine', key: 'id' },
      onUpdate: 'CASCADE',
      onDelete: 'RESTRICT'
    });

    await queryInterface.addIndex('general_dc', ['vendor_id'], { name: 'general_dc_vendor_idx' });
    await queryInterface.addIndex('general_receipt', ['vendor_id'], { name: 'general_receipt_vendor_idx' });
    await queryInterface.addIndex('general_dc_item', ['machine_id'], { name: 'general_dc_item_machine_idx' });
    await queryInterface.addIndex('general_receipt_item', ['machine_id'], { name: 'general_receipt_item_machine_idx' });
  },

  async down(queryInterface) {
    await queryInterface.removeIndex('general_receipt_item', 'general_receipt_item_machine_idx');
    await queryInterface.removeIndex('general_dc_item', 'general_dc_item_machine_idx');
    await queryInterface.removeIndex('general_receipt', 'general_receipt_vendor_idx');
    await queryInterface.removeIndex('general_dc', 'general_dc_vendor_idx');

    await queryInterface.removeColumn('general_receipt_item', 'machine_id');
    await queryInterface.removeColumn('general_dc_item', 'machine_id');
    await queryInterface.removeColumn('general_receipt', 'vendor_id');
    await queryInterface.removeColumn('general_dc', 'vendor_id');
  }
};
