Files
jizhi/backend/MiaoJiZhang.Infrastructure/Persistence/Migrations/20260721200000_AiPermissionsAndRecognitionIdempotency.cs
T
2026-07-24 23:11:20 +08:00

91 lines
3.7 KiB
C#

using System;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace MiaoJiZhang.Infrastructure.Persistence.Migrations;
[DbContext(typeof(AppDbContext))]
[Migration("20260721200000_AiPermissionsAndRecognitionIdempotency")]
public partial class AiPermissionsAndRecognitionIdempotency : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "ClientRequestId",
table: "Transactions",
type: "varchar(64)",
maxLength: 64,
nullable: true)
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateTable(
name: "UserFeaturePermissions",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
UserId = table.Column<long>(type: "bigint", nullable: false),
PermissionKey = table.Column<string>(type: "varchar(32)", maxLength: 32, nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
IsEnabled = table.Column<bool>(type: "tinyint(1)", nullable: false),
CreatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
UpdatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
},
constraints: table =>
{
table.PrimaryKey("PK_UserFeaturePermissions", x => x.Id);
table.ForeignKey(
name: "FK_UserFeaturePermissions_Users_UserId",
column: x => x.UserId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
})
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateIndex(
name: "IX_Transactions_UserId_ClientRequestId",
table: "Transactions",
columns: new[] { "UserId", "ClientRequestId" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_UserFeaturePermissions_UserId_PermissionKey",
table: "UserFeaturePermissions",
columns: new[] { "UserId", "PermissionKey" },
unique: true);
migrationBuilder.Sql("""
INSERT INTO UserFeaturePermissions
(UserId, PermissionKey, IsEnabled, CreatedAt, UpdatedAt)
SELECT Id, 'ai', TRUE, UTC_TIMESTAMP(6), UTC_TIMESTAMP(6)
FROM Users
WHERE NOT EXISTS (
SELECT 1 FROM UserFeaturePermissions p
WHERE p.UserId = Users.Id AND p.PermissionKey = 'ai'
);
""");
migrationBuilder.Sql("""
INSERT INTO AppConfigs (`Key`, Value, Version, UpdatedAt)
SELECT 'permission.default.ai_enabled', 'true', 1, UTC_TIMESTAMP(6)
WHERE NOT EXISTS (
SELECT 1 FROM AppConfigs
WHERE `Key` = 'permission.default.ai_enabled'
);
""");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(name: "UserFeaturePermissions");
migrationBuilder.DropIndex(
name: "IX_Transactions_UserId_ClientRequestId",
table: "Transactions");
migrationBuilder.DropColumn(name: "ClientRequestId", table: "Transactions");
migrationBuilder.Sql(
"DELETE FROM AppConfigs WHERE `Key` = 'permission.default.ai_enabled';");
}
}