using System; using System.Collections.Generic; using Microsoft.EntityFrameworkCore.Migrations; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; #nullable disable namespace Fengling.AuthService.Data.Migrations { /// public partial class AddTenantAndLogs : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn( name: "DisplayName", table: "AspNetRoles", type: "text", nullable: true); migrationBuilder.AddColumn( name: "IsSystem", table: "AspNetRoles", type: "boolean", nullable: false, defaultValue: false); migrationBuilder.AddColumn>( name: "Permissions", table: "AspNetRoles", type: "text[]", nullable: true); migrationBuilder.AddColumn( name: "TenantId", table: "AspNetRoles", type: "bigint", nullable: true); migrationBuilder.CreateTable( name: "AccessLogs", columns: table => new { Id = table.Column(type: "bigint", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), UserName = table.Column(type: "character varying(50)", maxLength: 50, nullable: true), TenantId = table.Column(type: "character varying(50)", maxLength: 50, nullable: true), Action = table.Column(type: "character varying(20)", maxLength: 20, nullable: false), Resource = table.Column(type: "character varying(200)", maxLength: 200, nullable: true), Method = table.Column(type: "character varying(10)", maxLength: 10, nullable: true), IpAddress = table.Column(type: "character varying(50)", maxLength: 50, nullable: true), UserAgent = table.Column(type: "character varying(500)", maxLength: 500, nullable: true), Status = table.Column(type: "character varying(20)", maxLength: 20, nullable: false), Duration = table.Column(type: "integer", nullable: false), RequestData = table.Column(type: "text", nullable: true), ResponseData = table.Column(type: "text", nullable: true), ErrorMessage = table.Column(type: "text", nullable: true), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false) }, constraints: table => { table.PrimaryKey("PK_AccessLogs", x => x.Id); }); migrationBuilder.CreateTable( name: "AuditLogs", columns: table => new { Id = table.Column(type: "bigint", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Operator = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), TenantId = table.Column(type: "character varying(50)", maxLength: 50, nullable: true), Operation = table.Column(type: "character varying(20)", maxLength: 20, nullable: false), Action = table.Column(type: "character varying(20)", maxLength: 20, nullable: false), TargetType = table.Column(type: "character varying(50)", maxLength: 50, nullable: true), TargetId = table.Column(type: "bigint", nullable: true), TargetName = table.Column(type: "character varying(100)", maxLength: 100, nullable: true), IpAddress = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), Description = table.Column(type: "character varying(500)", maxLength: 500, nullable: true), OldValue = table.Column(type: "text", nullable: true), NewValue = table.Column(type: "text", nullable: true), ErrorMessage = table.Column(type: "text", nullable: true), Status = table.Column(type: "character varying(20)", maxLength: 20, nullable: false), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false) }, constraints: table => { table.PrimaryKey("PK_AuditLogs", x => x.Id); }); migrationBuilder.CreateTable( name: "Tenants", columns: table => new { Id = table.Column(type: "bigint", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), TenantId = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), Name = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), ContactName = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), ContactEmail = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), ContactPhone = table.Column(type: "character varying(20)", maxLength: 20, nullable: true), MaxUsers = table.Column(type: "integer", nullable: true), CreatedAt = table.Column(type: "timestamp with time zone", nullable: false), Description = table.Column(type: "character varying(500)", maxLength: 500, nullable: true), Status = table.Column(type: "character varying(20)", maxLength: 20, nullable: false), ExpiresAt = table.Column(type: "timestamp with time zone", nullable: true), UpdatedAt = table.Column(type: "timestamp with time zone", nullable: true), IsDeleted = table.Column(type: "boolean", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Tenants", x => x.Id); }); migrationBuilder.CreateIndex( name: "IX_AccessLogs_Action", table: "AccessLogs", column: "Action"); migrationBuilder.CreateIndex( name: "IX_AccessLogs_CreatedAt", table: "AccessLogs", column: "CreatedAt"); migrationBuilder.CreateIndex( name: "IX_AccessLogs_Status", table: "AccessLogs", column: "Status"); migrationBuilder.CreateIndex( name: "IX_AccessLogs_TenantId", table: "AccessLogs", column: "TenantId"); migrationBuilder.CreateIndex( name: "IX_AccessLogs_UserName", table: "AccessLogs", column: "UserName"); migrationBuilder.CreateIndex( name: "IX_AuditLogs_Action", table: "AuditLogs", column: "Action"); migrationBuilder.CreateIndex( name: "IX_AuditLogs_CreatedAt", table: "AuditLogs", column: "CreatedAt"); migrationBuilder.CreateIndex( name: "IX_AuditLogs_Operation", table: "AuditLogs", column: "Operation"); migrationBuilder.CreateIndex( name: "IX_AuditLogs_Operator", table: "AuditLogs", column: "Operator"); migrationBuilder.CreateIndex( name: "IX_AuditLogs_TenantId", table: "AuditLogs", column: "TenantId"); migrationBuilder.CreateIndex( name: "IX_Tenants_TenantId", table: "Tenants", column: "TenantId", unique: true); migrationBuilder.AddForeignKey( name: "FK_AspNetUsers_Tenants_TenantId", table: "AspNetUsers", column: "TenantId", principalTable: "Tenants", principalColumn: "Id", onDelete: ReferentialAction.Cascade); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropForeignKey( name: "FK_AspNetUsers_Tenants_TenantId", table: "AspNetUsers"); migrationBuilder.DropTable( name: "AccessLogs"); migrationBuilder.DropTable( name: "AuditLogs"); migrationBuilder.DropTable( name: "Tenants"); migrationBuilder.DropColumn( name: "DisplayName", table: "AspNetRoles"); migrationBuilder.DropColumn( name: "IsSystem", table: "AspNetRoles"); migrationBuilder.DropColumn( name: "Permissions", table: "AspNetRoles"); migrationBuilder.DropColumn( name: "TenantId", table: "AspNetRoles"); } } }