'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface
      .addColumn('Transactions', 'UsingVirtualCardFromCollectiveId', {
        type: Sequelize.INTEGER,
        references: { model: 'Collectives', key: 'id' },
        onDelete: 'SET NULL',
        onUpdate: 'CASCADE',
        allowNull: true,
        description:
          'References the collective that created the virtual card used for this order',
      })
      .then(() => queryInterface.sequelize.sync())
      .then(() => {
        return queryInterface.sequelize.query(`
          UPDATE  "Transactions" t
          SET     "UsingVirtualCardFromCollectiveId" = spm."CollectiveId"
          FROM    "PaymentMethods" pm, "PaymentMethods" spm
          WHERE   pm.id 	= t."PaymentMethodId"
          AND     spm.id 	= pm."SourcePaymentMethodId"
          AND     pm.type = 'virtualcard'
        `);
      });
  },

  down: queryInterface => {
    return queryInterface.removeColumn(
      'Transactions',
      'UsingVirtualCardFromCollectiveId',
    );
  },
};

'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.addColumn(
      'Transactions',
      'UsingVirtualCardFromCollectiveId',
      {
        type: Sequelize.INTEGER,
        references: { model: 'Collectives', key: 'id' },
        onDelete: 'SET NULL',
        onUpdate: 'CASCADE',
        allowNull: true,
        description:
          'References the collective that created the virtual card used for this order',
      },
    );
  },

  down: queryInterface => {
    return queryInterface.removeColumn(
      'Transactions',
      'UsingVirtualCardFromCollectiveId',
    );
  },
};

--- 
+++ 
@@ -2,10 +2,8 @@
 
 module.exports = {
   up: (queryInterface, Sequelize) => {
-    return queryInterface.addColumn(
-      'Transactions',
-      'UsingVirtualCardFromCollectiveId',
-      {
+    return queryInterface
+      .addColumn('Transactions', 'UsingVirtualCardFromCollectiveId', {
         type: Sequelize.INTEGER,
         references: { model: 'Collectives', key: 'id' },
         onDelete: 'SET NULL',
@@ -13,8 +11,18 @@
         allowNull: true,
         description:
           'References the collective that created the virtual card used for this order',
-      },
-    );
+      })
+      .then(() => queryInterface.sequelize.sync())
+      .then(() => {
+        return queryInterface.sequelize.query(`
+          UPDATE  "Transactions" t
+          SET     "UsingVirtualCardFromCollectiveId" = spm."CollectiveId"
+          FROM    "PaymentMethods" pm, "PaymentMethods" spm
+          WHERE   pm.id 	= t."PaymentMethodId"
+          AND     spm.id 	= pm."SourcePaymentMethodId"
+          AND     pm.type = 'virtualcard'
+        `);
+      });
   },
 
   down: queryInterface => {
Set UsingVirtualCardFromCollectiveId for existing transactions in migrations

