import { MigrationInterface, QueryRunner } from 'typeorm';

export class userRoleRights1702513694200 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    queryRunner.query(`
        alter table project_roles
        add default_asset_rights smallint default 5 not null;
    `);
    queryRunner.query(`
        alter table project_roles
        add default_workspace_rights smallint default 5 not null;
    `);
    queryRunner.query(`
        create table asset_rights
        (
            project_id bigint not null,
            asset_id uuid not null,
            user_role_num integer not null,
            rights smallint not null default(0),
            primary key (project_id, asset_id, user_role_num),
            foreign key (project_id, asset_id) references assets(project_id, id) ON UPDATE CASCADE ON DELETE CASCADE
        );
    `);
    queryRunner.query(`
        create table workspace_rights
        (
            project_id bigint not null,
            workspace_id uuid not null,
            user_role_num integer not null,
            rights smallint not null default(0),
            primary key (project_id, workspace_id, user_role_num),
            foreign key (project_id, workspace_id) references workspaces(project_id, id) ON UPDATE CASCADE ON DELETE CASCADE
        );
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    queryRunner.query(`
        alter table project_roles
        drop column default_asset_rights;
    `);
    queryRunner.query(`
        alter table project_roles
        drop column default_workspace_rights;
    `);
    queryRunner.query(`
        drop table asset_rights;
    `);
    queryRunner.query(`
        drop table workspace_rights;
    `);
  }
}
