import { MigrationInterface, QueryRunner } from 'typeorm'

export class AddReservationTablesEntity1721391121290 implements MigrationInterface {
	name = 'AddReservationTablesEntity1721391121290'

	public async up(queryRunner: QueryRunner): Promise<void> {
		await queryRunner.query(
			`CREATE TABLE "reservation_tables" ("reservationId" uuid NOT NULL, "tableId" uuid NOT NULL, CONSTRAINT "PK_81d62541c85a8e6ba40d3ae6d64" PRIMARY KEY ("reservationId", "tableId"))`,
		)
		await queryRunner.query(`CREATE INDEX "IDX_df1b0aff36e300c2e92e11d8e8" ON "reservation_tables" ("reservationId") `)
		await queryRunner.query(`CREATE INDEX "IDX_5fd5297e1999f283ebb06d340e" ON "reservation_tables" ("tableId") `)
		await queryRunner.query(
			`ALTER TABLE "reservation_tables" ADD CONSTRAINT "FK_df1b0aff36e300c2e92e11d8e86" FOREIGN KEY ("reservationId") REFERENCES "reservation"("id") ON DELETE CASCADE ON UPDATE CASCADE`,
		)
		await queryRunner.query(
			`ALTER TABLE "reservation_tables" ADD CONSTRAINT "FK_5fd5297e1999f283ebb06d340e1" FOREIGN KEY ("tableId") REFERENCES "table"("id") ON DELETE CASCADE ON UPDATE CASCADE`,
		)
	}

	public async down(queryRunner: QueryRunner): Promise<void> {
		await queryRunner.query(`ALTER TABLE "reservation_tables" DROP CONSTRAINT "FK_5fd5297e1999f283ebb06d340e1"`)
		await queryRunner.query(`ALTER TABLE "reservation_tables" DROP CONSTRAINT "FK_df1b0aff36e300c2e92e11d8e86"`)
		await queryRunner.query(`DROP INDEX "public"."IDX_5fd5297e1999f283ebb06d340e"`)
		await queryRunner.query(`DROP INDEX "public"."IDX_df1b0aff36e300c2e92e11d8e8"`)
		await queryRunner.query(`DROP TABLE "reservation_tables"`)
	}
}
