import { MigrationInterface, QueryRunner } from "typeorm";

export class AddTableProfile11707194386583 implements MigrationInterface {
    name = 'AddTableProfile11707194386583'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE \`posMaster\` DROP FOREIGN KEY \`FK_1acc4ba5b4af308f0d8a1b7b249\``);
        await queryRunner.query(`ALTER TABLE \`posMaster\` DROP FOREIGN KEY \`FK_9d29f52dcf499db3a5e8209b6dc\``);
        await queryRunner.query(`ALTER TABLE \`profile\` ADD \`currentHolderId\` varchar(36) NULL`);
        await queryRunner.query(`ALTER TABLE \`profile\` ADD UNIQUE INDEX \`IDX_b5bfc34771c02a8078b9bf8944\` (\`currentHolderId\`)`);
        await queryRunner.query(`ALTER TABLE \`profile\` ADD \`nextHolderId\` varchar(36) NULL`);
        await queryRunner.query(`ALTER TABLE \`profile\` ADD UNIQUE INDEX \`IDX_49d2dcf6c8bd789ca99df7dc0d\` (\`nextHolderId\`)`);
        await queryRunner.query(`ALTER TABLE \`posMaster\` ADD \`currentHolderId\` varchar(36) NULL`);
        await queryRunner.query(`ALTER TABLE \`posMaster\` ADD UNIQUE INDEX \`IDX_dca00872043f09a74af1d5172f\` (\`currentHolderId\`)`);
        await queryRunner.query(`ALTER TABLE \`posMaster\` ADD \`nextHolderId\` varchar(36) NULL`);
        await queryRunner.query(`ALTER TABLE \`posMaster\` ADD UNIQUE INDEX \`IDX_d15696d0e541ead772db817b36\` (\`nextHolderId\`)`);
        await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`citizenId\``);
        await queryRunner.query(`ALTER TABLE \`profile\` ADD \`citizenId\` varchar(13) NULL COMMENT 'เลขประจำตัวประชาชน'`);
        await queryRunner.query(`CREATE UNIQUE INDEX \`REL_b5bfc34771c02a8078b9bf8944\` ON \`profile\` (\`currentHolderId\`)`);
        await queryRunner.query(`CREATE UNIQUE INDEX \`REL_49d2dcf6c8bd789ca99df7dc0d\` ON \`profile\` (\`nextHolderId\`)`);
        await queryRunner.query(`CREATE UNIQUE INDEX \`REL_dca00872043f09a74af1d5172f\` ON \`posMaster\` (\`currentHolderId\`)`);
        await queryRunner.query(`CREATE UNIQUE INDEX \`REL_d15696d0e541ead772db817b36\` ON \`posMaster\` (\`nextHolderId\`)`);
        await queryRunner.query(`ALTER TABLE \`profile\` ADD CONSTRAINT \`FK_b5bfc34771c02a8078b9bf89445\` FOREIGN KEY (\`currentHolderId\`) REFERENCES \`posMaster\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`profile\` ADD CONSTRAINT \`FK_49d2dcf6c8bd789ca99df7dc0d4\` FOREIGN KEY (\`nextHolderId\`) REFERENCES \`posMaster\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`posMaster\` ADD CONSTRAINT \`FK_dca00872043f09a74af1d5172f3\` FOREIGN KEY (\`currentHolderId\`) REFERENCES \`profile\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`posMaster\` ADD CONSTRAINT \`FK_d15696d0e541ead772db817b36a\` FOREIGN KEY (\`nextHolderId\`) REFERENCES \`profile\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE \`posMaster\` DROP FOREIGN KEY \`FK_d15696d0e541ead772db817b36a\``);
        await queryRunner.query(`ALTER TABLE \`posMaster\` DROP FOREIGN KEY \`FK_dca00872043f09a74af1d5172f3\``);
        await queryRunner.query(`ALTER TABLE \`profile\` DROP FOREIGN KEY \`FK_49d2dcf6c8bd789ca99df7dc0d4\``);
        await queryRunner.query(`ALTER TABLE \`profile\` DROP FOREIGN KEY \`FK_b5bfc34771c02a8078b9bf89445\``);
        await queryRunner.query(`DROP INDEX \`REL_d15696d0e541ead772db817b36\` ON \`posMaster\``);
        await queryRunner.query(`DROP INDEX \`REL_dca00872043f09a74af1d5172f\` ON \`posMaster\``);
        await queryRunner.query(`DROP INDEX \`REL_49d2dcf6c8bd789ca99df7dc0d\` ON \`profile\``);
        await queryRunner.query(`DROP INDEX \`REL_b5bfc34771c02a8078b9bf8944\` ON \`profile\``);
        await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`citizenId\``);
        await queryRunner.query(`ALTER TABLE \`profile\` ADD \`citizenId\` int NULL COMMENT 'เลขประจำตัวประชาชน'`);
        await queryRunner.query(`ALTER TABLE \`posMaster\` DROP INDEX \`IDX_d15696d0e541ead772db817b36\``);
        await queryRunner.query(`ALTER TABLE \`posMaster\` DROP COLUMN \`nextHolderId\``);
        await queryRunner.query(`ALTER TABLE \`posMaster\` DROP INDEX \`IDX_dca00872043f09a74af1d5172f\``);
        await queryRunner.query(`ALTER TABLE \`posMaster\` DROP COLUMN \`currentHolderId\``);
        await queryRunner.query(`ALTER TABLE \`profile\` DROP INDEX \`IDX_49d2dcf6c8bd789ca99df7dc0d\``);
        await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`nextHolderId\``);
        await queryRunner.query(`ALTER TABLE \`profile\` DROP INDEX \`IDX_b5bfc34771c02a8078b9bf8944\``);
        await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`currentHolderId\``);
        await queryRunner.query(`ALTER TABLE \`posMaster\` ADD CONSTRAINT \`FK_9d29f52dcf499db3a5e8209b6dc\` FOREIGN KEY (\`profileIdNextHolder\`) REFERENCES \`profile\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`posMaster\` ADD CONSTRAINT \`FK_1acc4ba5b4af308f0d8a1b7b249\` FOREIGN KEY (\`profileIdCurrentHolder\`) REFERENCES \`profile\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
    }

}
