import {
  MigrationInterface,
  QueryRunner,
  Table,
  TableForeignKey,
} from 'typeorm';

export class CreateGroupsNewsTable1710747720146 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.createTable(
      new Table({
        name: 'groups_news',
        columns: [
          {
            name: 'news_id',
            type: 'uuid',
          },
          {
            name: 'group_id',
            type: 'uuid',
          },
        ],
      }),
    );

    await queryRunner.createForeignKey(
      'groups_news',
      new TableForeignKey({
        name: 'news_fk',
        columnNames: ['news_id'],
        referencedColumnNames: ['id'],
        referencedTableName: 'news',
        onDelete: 'CASCADE',
      }),
    );

    await queryRunner.createForeignKey(
      'groups_news',
      new TableForeignKey({
        name: 'group_fk',
        columnNames: ['group_id'],
        referencedColumnNames: ['id'],
        referencedTableName: 'groups',
        onDelete: 'CASCADE',
      }),
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropForeignKey('groups_news', 'news_fk');
    await queryRunner.dropForeignKey('groups_news', 'group_fk');
    await queryRunner.dropTable('groups_news');
  }
}
