'use strict';

module.exports = {
    up: async (queryInterface, Sequelize) => {
        await queryInterface.sequelize.transaction(async (transaction) => {
            await queryInterface.addColumn(
                'user',
                'email_temp',
                {
                    type: Sequelize.BLOB,
                },
                { transaction }
            );

            await queryInterface.sequelize.query(
                'UPDATE "community"."user" SET "email_temp" = "email"::bytea',
                { transaction }
            );

            await queryInterface.removeColumn('user', 'email', { transaction });

            await queryInterface.renameColumn('user', 'email_temp', 'email', { transaction });

            await queryInterface.changeColumn(
                'user',
                'email',
                {
                    type: Sequelize.BLOB,
                    allowNull: false,
                    unique: true,
                },
                { transaction }
            );
        });
    },

    down: async (queryInterface, Sequelize) => {
        // Rollback code if needed
    },
};
