DD.WellWorkover.Cloud/AsbCloudDb/Migrations/20211123130153_Add_UserRoles_Permissions.cs

185 lines
7.7 KiB
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace AsbCloudDb.Migrations
{
public partial class Add_UserRoles_Permissions : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_t_user_t_user_role_id_role",
table: "t_user");
migrationBuilder.DropIndex(
name: "IX_t_user_id_role",
table: "t_user");
migrationBuilder.DropColumn(
name: "id_role",
table: "t_user");
migrationBuilder.DropColumn(
name: "level",
table: "t_user");
migrationBuilder.AddColumn<int>(
name: "id_parent",
table: "t_user_role",
type: "integer",
nullable: false,
defaultValue: 0,
comment: "От какой роли унаследована данная роль");
migrationBuilder.AddColumn<int>(
name: "role_type",
table: "t_user_role",
type: "integer",
nullable: false,
defaultValue: 0,
comment: "0-роль из стандартной матрицы, \n1-специальная роль для какого-либо пользователя");
migrationBuilder.CreateTable(
name: "t_permission",
columns: table => new
{
id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
caption = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: true, comment: "Название"),
type = table.Column<int>(type: "integer", nullable: false, comment: "1-чтение, 2-запись, 3-чтение и запись")
},
constraints: table =>
{
table.PrimaryKey("PK_t_permission", x => x.id);
},
comment: "Разрешения на доступ к данным");
migrationBuilder.CreateTable(
name: "t_relation_user_user_role",
columns: table => new
{
id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
id_user = table.Column<int>(type: "integer", nullable: false),
id_user_role = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_t_relation_user_user_role", x => x.id);
table.ForeignKey(
name: "FK_t_relation_user_user_role_t_user_id_user",
column: x => x.id_user,
principalTable: "t_user",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_t_relation_user_user_role_t_user_role_id_user_role",
column: x => x.id_user_role,
principalTable: "t_user_role",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
},
comment: "Отношение пользователей и ролей");
migrationBuilder.CreateTable(
name: "t_relation_user_role_permission",
columns: table => new
{
id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
id_user_role = table.Column<int>(type: "integer", nullable: false),
id_permission = table.Column<int>(type: "integer", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_t_relation_user_role_permission", x => x.id);
table.ForeignKey(
name: "FK_t_relation_user_role_permission_t_permission_id_permission",
column: x => x.id_permission,
principalTable: "t_permission",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_t_relation_user_role_permission_t_user_role_id_user_role",
column: x => x.id_user_role,
principalTable: "t_user_role",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
},
comment: "Отношение ролей пользователей и разрешений доступа");
migrationBuilder.CreateIndex(
name: "IX_t_relation_user_role_permission_id_permission",
table: "t_relation_user_role_permission",
column: "id_permission");
migrationBuilder.CreateIndex(
name: "IX_t_relation_user_role_permission_id_user_role",
table: "t_relation_user_role_permission",
column: "id_user_role");
migrationBuilder.CreateIndex(
name: "IX_t_relation_user_user_role_id_user",
table: "t_relation_user_user_role",
column: "id_user");
migrationBuilder.CreateIndex(
name: "IX_t_relation_user_user_role_id_user_role",
table: "t_relation_user_user_role",
column: "id_user_role");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "t_relation_user_role_permission");
migrationBuilder.DropTable(
name: "t_relation_user_user_role");
migrationBuilder.DropTable(
name: "t_permission");
migrationBuilder.DropColumn(
name: "id_parent",
table: "t_user_role");
migrationBuilder.DropColumn(
name: "role_type",
table: "t_user_role");
migrationBuilder.AddColumn<int>(
name: "id_role",
table: "t_user",
type: "integer",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "level",
table: "t_user",
type: "integer",
nullable: true);
migrationBuilder.UpdateData(
table: "t_user",
keyColumn: "id",
keyValue: 1,
columns: new[] { "id_role", "level" },
values: new object[] { 1, 2147483647 });
migrationBuilder.CreateIndex(
name: "IX_t_user_id_role",
table: "t_user",
column: "id_role");
migrationBuilder.AddForeignKey(
name: "FK_t_user_t_user_role_id_role",
table: "t_user",
column: "id_role",
principalTable: "t_user_role",
principalColumn: "id",
onDelete: ReferentialAction.Restrict);
}
}
}