import { MigrationInterface, QueryRunner } from 'typeorm';

export class assetForeignBlocksAndUserRights1703630092293
  implements MigrationInterface
{
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
            CREATE TABLE asset_foreign_blocks(
                project_id  bigint                                 not null
                    constraint asset_blocks_project_id_fkey
                        references projects
                        on update cascade on delete cascade,
                block_key   bigint                                 not null,
                asset_id    uuid                                   not null,
                title       varchar,
                foreign_asset_id  uuid NOT NULL,
                foreign_block_key  bigint NOT NULL,
                created_at  timestamp with time zone default now() not null,
                updated_at  timestamp with time zone default now() not null,
                index       real,
                constraint asset_foreign_blocks_pkey
                    primary key (project_id, asset_id, block_key),
                constraint asset_foreign_blocks_project_id_block_key_fkey
                    foreign key (project_id, block_key) references asset_block_keys
                        on update cascade on delete cascade,
                constraint asset_foreign_blocks_project_id_asset_id_fkey
                    foreign key (project_id, asset_id) references assets
                        on update cascade on delete cascade,
                constraint asset_blocks_project_id_foreign_asset_id_fkey
                    foreign key (project_id, foreign_asset_id) references assets
                        on update cascade on delete cascade
            )        
        `);
    await queryRunner.query(`
            create table asset_user_rights
            (
                project_id    bigint             not null,
                asset_id      uuid               not null,
                user_id       integer            not null,
                rights        smallint default 0 not null,
                constraint asset_user_rights_pkey
                    primary key (project_id, asset_id, user_id),
                constraint asset_user_rights_project_id_asset_id_fkey
                    foreign key (project_id, asset_id) references assets
                        on update cascade on delete cascade
            );
        `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`
            DROP TABLE asset_foreign_blocks;     
        `);
    await queryRunner.query(`
            drop table asset_user_rights;
        `);
  }
}
