import { MigrationInterface, QueryRunner } from 'typeorm'

export class AddTableSubscriberEntity1696871144845 implements MigrationInterface {
	name = 'AddTableSubscriberEntity1696871144845'

	public async up(queryRunner: QueryRunner): Promise<void> {
		await queryRunner.query(
			`CREATE TABLE "table_subscriber" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "createdBy" uuid, "lastUpdatedAt" TIMESTAMP NOT NULL DEFAULT now(), "lastUpdatedBy" uuid, "tableId" uuid NOT NULL, "userId" uuid NOT NULL, CONSTRAINT "PK_d6a8892ec8949fabf5f6ad0436d" PRIMARY KEY ("id"))`,
		)
		await queryRunner.query(
			`ALTER TABLE "table_subscriber" ADD CONSTRAINT "FK_46967fc0d6db491ab2a780f80d3" FOREIGN KEY ("tableId") REFERENCES "table"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
		)
		await queryRunner.query(
			`ALTER TABLE "table_subscriber" ADD CONSTRAINT "FK_d8f6beebcd264cfd6904747975b" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
		)
	}

	public async down(queryRunner: QueryRunner): Promise<void> {
		await queryRunner.query(`ALTER TABLE "table_subscriber" DROP CONSTRAINT "FK_d8f6beebcd264cfd6904747975b"`)
		await queryRunner.query(`ALTER TABLE "table_subscriber" DROP CONSTRAINT "FK_46967fc0d6db491ab2a780f80d3"`)
		await queryRunner.query(`DROP TABLE "table_subscriber"`)
	}
}
