import { DataTypes } from 'sequelize';

import { Migration, safeApplyColumnChanges } from '../migrate';

export const up: Migration = async ({ context }) => {
  // Add type field to exchange_rate_providers table
  await safeApplyColumnChanges(context, {
    exchange_rate_providers: [
      {
        name: 'type',
        field: {
          type: DataTypes.STRING(32),
          allowNull: false,
          defaultValue: 'token-data',
        },
      },
    ],
  });

  // Update existing provider to have type field
  await context.sequelize.query(
    "UPDATE exchange_rate_providers SET type = 'token-data' WHERE type IS NULL OR type = ''"
  );
};

export const down: Migration = async ({ context }) => {
  await context.removeColumn('exchange_rate_providers', 'type');
};
