using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace JobTrackerApi.Migrations
{
///
public partial class AddCrossFeatureAiUsage : Migration
{
///
protected override void Up(MigrationBuilder migrationBuilder)
{
if (ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase))
{
migrationBuilder.Sql("""
CREATE TABLE `AiUsageRecords` (
`Id` bigint NOT NULL AUTO_INCREMENT,
`OwnerUserId` varchar(255) NOT NULL,
`SourceType` varchar(32) NOT NULL,
`SourceId` varchar(64) NOT NULL,
`TaskType` varchar(64) NOT NULL,
`CallCount` int NOT NULL,
`InputCharacterCount` int NOT NULL,
`OutputCharacterCount` int NOT NULL,
`EstimatedTokenCount` int NOT NULL,
`CreatedAtUtc` datetime(6) NOT NULL,
CONSTRAINT `PK_AiUsageRecords` PRIMARY KEY (`Id`)
) CHARACTER SET=utf8mb4;
""");
}
else
{
migrationBuilder.CreateTable(
name: "AiUsageRecords",
columns: table => new
{
Id = table.Column(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
OwnerUserId = table.Column(type: "TEXT", maxLength: 255, nullable: false),
SourceType = table.Column(type: "TEXT", maxLength: 32, nullable: false),
SourceId = table.Column(type: "TEXT", maxLength: 64, nullable: false),
TaskType = table.Column(type: "TEXT", maxLength: 64, nullable: false),
CallCount = table.Column(type: "INTEGER", nullable: false),
InputCharacterCount = table.Column(type: "INTEGER", nullable: false),
OutputCharacterCount = table.Column(type: "INTEGER", nullable: false),
EstimatedTokenCount = table.Column(type: "INTEGER", nullable: false),
CreatedAtUtc = table.Column(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_AiUsageRecords", x => x.Id);
});
}
migrationBuilder.CreateIndex(
name: "IX_AiUsageRecords_OwnerUserId_CreatedAtUtc",
table: "AiUsageRecords",
columns: new[] { "OwnerUserId", "CreatedAtUtc" });
migrationBuilder.CreateIndex(
name: "IX_AiUsageRecords_OwnerUserId_SourceType_SourceId",
table: "AiUsageRecords",
columns: new[] { "OwnerUserId", "SourceType", "SourceId" },
unique: true);
if (ActiveProvider.Contains("MySql", StringComparison.OrdinalIgnoreCase))
{
migrationBuilder.Sql("""
INSERT IGNORE INTO `AiUsageRecords`
(`OwnerUserId`, `SourceType`, `SourceId`, `TaskType`, `CallCount`, `InputCharacterCount`, `OutputCharacterCount`, `EstimatedTokenCount`, `CreatedAtUtc`)
SELECT `OwnerUserId`, 'workspace-legacy', CAST(`Id` AS CHAR), `Module`, 1,
`InputCharacterCount`, `OutputCharacterCount`, `EstimatedTokenCount`, `CreatedAtUtc`
FROM `AiInteractions`;
""");
}
else
{
migrationBuilder.Sql("""
INSERT OR IGNORE INTO "AiUsageRecords"
("OwnerUserId", "SourceType", "SourceId", "TaskType", "CallCount", "InputCharacterCount", "OutputCharacterCount", "EstimatedTokenCount", "CreatedAtUtc")
SELECT "OwnerUserId", 'workspace-legacy', CAST("Id" AS TEXT), "Module", 1,
"InputCharacterCount", "OutputCharacterCount", "EstimatedTokenCount", "CreatedAtUtc"
FROM "AiInteractions";
""");
}
}
///
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "AiUsageRecords");
}
}
}