using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace Mws.Infrastructure.Migrations { /// public partial class SplitProjectMemberPermissions : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( name: "project_member_permissions", columns: table => new { ProjectId = table.Column(type: "uuid", nullable: false), UserId = table.Column(type: "uuid", nullable: false), Screen = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), CanView = table.Column(type: "boolean", nullable: false), CanCreate = table.Column(type: "boolean", nullable: false), CanEdit = table.Column(type: "boolean", nullable: false), CanDelete = table.Column(type: "boolean", nullable: false) }, constraints: table => { table.PrimaryKey("PK_project_member_permissions", x => new { x.ProjectId, x.UserId, x.Screen }); table.ForeignKey( name: "FK_project_member_permissions_project_members_ProjectId_UserId", columns: x => new { x.ProjectId, x.UserId }, principalTable: "project_members", principalColumns: new[] { "ProjectId", "UserId" }, onDelete: ReferentialAction.Cascade); }); migrationBuilder.Sql(""" INSERT INTO "project_member_permissions" ("ProjectId", "UserId", "Screen", "CanView", "CanCreate", "CanEdit", "CanDelete") SELECT "ProjectId", "UserId", 'documents', "CanViewDocuments", "CanCreateDocuments", "CanEditDocuments", "CanDeleteDocuments" FROM "project_members"; """); migrationBuilder.DropColumn( name: "CanCreateDocuments", table: "project_members"); migrationBuilder.DropColumn( name: "CanDeleteDocuments", table: "project_members"); migrationBuilder.DropColumn( name: "CanEditDocuments", table: "project_members"); migrationBuilder.DropColumn( name: "CanViewDocuments", table: "project_members"); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn( name: "CanCreateDocuments", table: "project_members", type: "boolean", nullable: false, defaultValue: false); migrationBuilder.AddColumn( name: "CanDeleteDocuments", table: "project_members", type: "boolean", nullable: false, defaultValue: false); migrationBuilder.AddColumn( name: "CanEditDocuments", table: "project_members", type: "boolean", nullable: false, defaultValue: false); migrationBuilder.AddColumn( name: "CanViewDocuments", table: "project_members", type: "boolean", nullable: false, defaultValue: false); migrationBuilder.Sql(""" UPDATE "project_members" m SET "CanViewDocuments" = p."CanView", "CanCreateDocuments" = p."CanCreate", "CanEditDocuments" = p."CanEdit", "CanDeleteDocuments" = p."CanDelete" FROM "project_member_permissions" p WHERE p."ProjectId" = m."ProjectId" AND p."UserId" = m."UserId" AND p."Screen" = 'documents'; """); migrationBuilder.DropTable( name: "project_member_permissions"); } } }