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

75 lines
2.6 KiB
C#

using System;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace MiaoJiZhang.Infrastructure.Persistence.Migrations;
[DbContext(typeof(AppDbContext))]
[Migration("20260722030000_AiChatQuotas")]
public partial class AiChatQuotas : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "AiChatLimit",
table: "Users",
type: "int",
nullable: false,
defaultValue: 50);
migrationBuilder.AddColumn<int>(
name: "AiChatPeriod",
table: "Users",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "AiChatUsed",
table: "Users",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<DateTime>(
name: "AiChatWindowStartedAt",
table: "Users",
type: "datetime(6)",
nullable: false,
defaultValue: new DateTime(1970, 1, 1));
migrationBuilder.Sql("""
UPDATE Users
SET AiChatWindowStartedAt =
CONVERT_TZ(DATE(CONVERT_TZ(UTC_TIMESTAMP(6), '+00:00', '+08:00')), '+08:00', '+00:00');
""");
migrationBuilder.Sql("""
INSERT INTO AppConfigs (`Key`, Value, Version, UpdatedAt)
SELECT 'quota.default_ai_chat_limit', '50', 1, UTC_TIMESTAMP(6)
WHERE NOT EXISTS (
SELECT 1 FROM AppConfigs
WHERE `Key` = 'quota.default_ai_chat_limit'
);
""");
migrationBuilder.Sql("""
INSERT INTO AppConfigs (`Key`, Value, Version, UpdatedAt)
SELECT 'quota.default_ai_chat_period', 'day', 1, UTC_TIMESTAMP(6)
WHERE NOT EXISTS (
SELECT 1 FROM AppConfigs
WHERE `Key` = 'quota.default_ai_chat_period'
);
""");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(name: "AiChatLimit", table: "Users");
migrationBuilder.DropColumn(name: "AiChatPeriod", table: "Users");
migrationBuilder.DropColumn(name: "AiChatUsed", table: "Users");
migrationBuilder.DropColumn(name: "AiChatWindowStartedAt", table: "Users");
migrationBuilder.Sql(
"DELETE FROM AppConfigs WHERE `Key` IN ('quota.default_ai_chat_limit', 'quota.default_ai_chat_period');");
}
}