import { MigrationInterface, QueryRunner, TableUnique } from "typeorm";

export class RenameUserColumns1758958856261 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<void> {
        // drop constraint
        await queryRunner.dropUniqueConstraint("user", "UQ_user_userName");
        // rename columns
        await queryRunner.renameColumn(
            "user",
            "userName",
            "user_name"
        )
        await queryRunner.renameColumn(
            "user",
            "firstName",
            "first_name"
        )
        await queryRunner.renameColumn(
            "user",
            "lastName",
            "last_name"
        )
        await queryRunner.renameColumn(
            "user",
            "createdAt",
            "create_date"
        )
        await queryRunner.renameColumn(
            "user",
            "updatedAt",
            "update_date"
        )
        // Add a unique constraint on user_name
        await queryRunner.createUniqueConstraint(
            "user",
            new TableUnique({
                columnNames: ["user_name"],
                name: "UQ_user_user_name",
            })
        );
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        // drop constraint
        await queryRunner.dropUniqueConstraint("user", "UQ_user_user_name");
         // rename columns
        await queryRunner.renameColumn(
            "user",
            "user_name",
            "userName",
        )
        await queryRunner.renameColumn(
            "user",
            "first_name",
            "firstName"
        )
        await queryRunner.renameColumn(
            "user",
            "last_name",
            "lastName"
        )
        await queryRunner.renameColumn(
            "user",
            "create_date",
            "createdAt"
        )
        await queryRunner.renameColumn(
            "user",
            "update_date",
            "updatedAt"
        )
        // Add a unique constraint on userName
        await queryRunner.createUniqueConstraint(
            "user",
            new TableUnique({
                columnNames: ["userName"],
                name: "UQ_user_userName",
            })
        );
    }

}
