import { MigrationInterface, QueryRunner } from "typeorm"

export class CreateContractors_table1762413291899 implements MigrationInterface {
  name = "CreateContractors_table1762413291899"

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `CREATE TABLE "contractor_types" ("created_by" integer, "updated_by" integer, "deleted_by" integer, "created_at" TIMESTAMP NOT NULL DEFAULT NOW(), "updated_at" TIMESTAMP NOT NULL DEFAULT NOW(), "deleted_at" TIMESTAMP, "id" SERIAL NOT NULL, "type_name" character varying(255) NOT NULL, "company_id" integer NOT NULL, CONSTRAINT "PK_1afd7a142c9f41d9bb1f79b089a" PRIMARY KEY ("id"))`,
    )
    await queryRunner.query(
      `CREATE TABLE "contractors" ("created_by" integer, "updated_by" integer, "deleted_by" integer, "created_at" TIMESTAMP NOT NULL DEFAULT NOW(), "updated_at" TIMESTAMP NOT NULL DEFAULT NOW(), "deleted_at" TIMESTAMP, "id" SERIAL NOT NULL, "contractor_name" character varying(255) NOT NULL, "type_id" integer NOT NULL, "company_id" integer NOT NULL, "phone_number" character varying(255), "email" character varying(255), "project_id" integer, "notes" text, CONSTRAINT "PK_6dbfde8813cdc4c4689f1e1e503" PRIMARY KEY ("id"))`,
    )
    await queryRunner.query(
      `ALTER TABLE "contractor_types" ADD CONSTRAINT "FK_4c462f2bf3b6fe6912c4cfe3a08" FOREIGN KEY ("company_id") REFERENCES "companies"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "contractors" ADD CONSTRAINT "FK_0aa4f46b92678f86b7e87876ee0" FOREIGN KEY ("company_id") REFERENCES "companies"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "contractors" ADD CONSTRAINT "FK_59580a47be8f41b8673142d7227" FOREIGN KEY ("type_id") REFERENCES "contractor_types"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "contractors" ADD CONSTRAINT "FK_c6edec426d9183c98768c0edcbd" FOREIGN KEY ("project_id") REFERENCES "projects"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "contractors" DROP CONSTRAINT "FK_c6edec426d9183c98768c0edcbd"`,
    )
    await queryRunner.query(
      `ALTER TABLE "contractors" DROP CONSTRAINT "FK_59580a47be8f41b8673142d7227"`,
    )
    await queryRunner.query(
      `ALTER TABLE "contractors" DROP CONSTRAINT "FK_0aa4f46b92678f86b7e87876ee0"`,
    )
    await queryRunner.query(
      `ALTER TABLE "contractor_types" DROP CONSTRAINT "FK_4c462f2bf3b6fe6912c4cfe3a08"`,
    )
    await queryRunner.query(`DROP TABLE "contractors"`)
    await queryRunner.query(`DROP TABLE "contractor_types"`)
  }
}
