import { MigrationInterface, QueryRunner } from "typeorm";

export class AddTableProfile51707208924575 implements MigrationInterface {
    name = 'AddTableProfile51707208924575'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE \`posMaster\` DROP FOREIGN KEY \`FK_22499e79738c6873fc7ede3f6dd\``);
        await queryRunner.query(`ALTER TABLE \`posMaster\` DROP FOREIGN KEY \`FK_9f765a0fef405dfafbd1b5f6c0a\``);
        await queryRunner.query(`ALTER TABLE \`profile\` DROP FOREIGN KEY \`FK_ee79137a2909faa188c20a70413\``);
        await queryRunner.query(`ALTER TABLE \`profile\` DROP FOREIGN KEY \`FK_c2ff3f61c281ece7f3ab5a2c20b\``);
        await queryRunner.query(`DROP INDEX \`REL_22499e79738c6873fc7ede3f6d\` ON \`posMaster\``);
        await queryRunner.query(`DROP INDEX \`REL_9f765a0fef405dfafbd1b5f6c0\` ON \`posMaster\``);
        await queryRunner.query(`DROP INDEX \`IDX_c2ff3f61c281ece7f3ab5a2c20\` ON \`profile\``);
        await queryRunner.query(`DROP INDEX \`IDX_ee79137a2909faa188c20a7041\` ON \`profile\``);
        await queryRunner.query(`DROP INDEX \`REL_c2ff3f61c281ece7f3ab5a2c20\` ON \`profile\``);
        await queryRunner.query(`DROP INDEX \`REL_ee79137a2909faa188c20a7041\` ON \`profile\``);
        await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`current_holderId\``);
        await queryRunner.query(`ALTER TABLE \`profile\` DROP COLUMN \`next_holderId\``);
        await queryRunner.query(`ALTER TABLE \`posMaster\` ADD CONSTRAINT \`FK_9f765a0fef405dfafbd1b5f6c0a\` FOREIGN KEY (\`current_holderId\`) REFERENCES \`posMaster\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`posMaster\` ADD CONSTRAINT \`FK_22499e79738c6873fc7ede3f6dd\` FOREIGN KEY (\`next_holderId\`) REFERENCES \`posMaster\`(\`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_22499e79738c6873fc7ede3f6dd\``);
        await queryRunner.query(`ALTER TABLE \`posMaster\` DROP FOREIGN KEY \`FK_9f765a0fef405dfafbd1b5f6c0a\``);
        await queryRunner.query(`ALTER TABLE \`profile\` ADD \`next_holderId\` varchar(40) NULL COMMENT 'คนที่กำลังจะมาครอง ตอนปรับโครงสร้าง ถ้าเลือกให้ใครมาครอง ProfileId ของคนนั้นจะมาอยู่ในช่องนี้ รวมทั้งตอนเลือกตำแหน่งเพื่อบรรจุ แต่งตั้ง เลื่อน ย้าย ในระบบบรรจุแต่งตั้งด้วย'`);
        await queryRunner.query(`ALTER TABLE \`profile\` ADD \`current_holderId\` varchar(40) NULL COMMENT 'คนครองปัจจุบัน เมื่อทำสำเนาโครงสร้างและตำแหน่งพร้อมกับคนครองมา คนครองจะอยู่ในฟิลด์นี้'`);
        await queryRunner.query(`CREATE UNIQUE INDEX \`REL_ee79137a2909faa188c20a7041\` ON \`profile\` (\`current_holderId\`)`);
        await queryRunner.query(`CREATE UNIQUE INDEX \`REL_c2ff3f61c281ece7f3ab5a2c20\` ON \`profile\` (\`next_holderId\`)`);
        await queryRunner.query(`CREATE UNIQUE INDEX \`IDX_ee79137a2909faa188c20a7041\` ON \`profile\` (\`current_holderId\`)`);
        await queryRunner.query(`CREATE UNIQUE INDEX \`IDX_c2ff3f61c281ece7f3ab5a2c20\` ON \`profile\` (\`next_holderId\`)`);
        await queryRunner.query(`CREATE UNIQUE INDEX \`REL_9f765a0fef405dfafbd1b5f6c0\` ON \`posMaster\` (\`current_holderId\`)`);
        await queryRunner.query(`CREATE UNIQUE INDEX \`REL_22499e79738c6873fc7ede3f6d\` ON \`posMaster\` (\`next_holderId\`)`);
        await queryRunner.query(`ALTER TABLE \`profile\` ADD CONSTRAINT \`FK_c2ff3f61c281ece7f3ab5a2c20b\` FOREIGN KEY (\`next_holderId\`) REFERENCES \`posMaster\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`profile\` ADD CONSTRAINT \`FK_ee79137a2909faa188c20a70413\` FOREIGN KEY (\`current_holderId\`) REFERENCES \`posMaster\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`posMaster\` ADD CONSTRAINT \`FK_9f765a0fef405dfafbd1b5f6c0a\` FOREIGN KEY (\`current_holderId\`) REFERENCES \`profile\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE \`posMaster\` ADD CONSTRAINT \`FK_22499e79738c6873fc7ede3f6dd\` FOREIGN KEY (\`next_holderId\`) REFERENCES \`profile\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
    }

}
