﻿START TRANSACTION;
CREATE TABLE `UserOperations` (
    `Id` char(36) CHARACTER SET ascii COLLATE ascii_general_ci NOT NULL,
    `OwnerUserId` varchar(255) NOT NULL,
    `TaskType` varchar(64) NOT NULL,
    `IdempotencyKey` varchar(128) NOT NULL,
    `Status` varchar(32) NOT NULL,
    `Priority` int NOT NULL,
    `EntitlementDecision` varchar(32) NOT NULL,
    `PrivacyPolicy` varchar(32) NOT NULL,
    `SubjectType` varchar(64) NULL,
    `SubjectId` varchar(128) NULL,
    `Provider` varchar(128) NULL,
    `Model` varchar(128) NULL,
    `AttemptCount` int NOT NULL,
    `MaxAttempts` int NOT NULL,
    `CreatedAtUtc` datetime(6) NOT NULL,
    `AvailableAtUtc` datetime(6) NOT NULL,
    `StartedAtUtc` datetime(6) NULL,
    `CompletedAtUtc` datetime(6) NULL,
    `DeadlineAtUtc` datetime(6) NULL,
    `CancellationRequestedAtUtc` datetime(6) NULL,
    `LeaseToken` char(32) CHARACTER SET ascii COLLATE ascii_general_ci NULL,
    `LeaseExpiresAtUtc` datetime(6) NULL,
    `LastHeartbeatAtUtc` datetime(6) NULL,
    `ProgressStage` varchar(64) NULL,
    `ProgressPercent` int NULL,
    `FailureCategory` varchar(64) NULL,
    `FailureMessage` varchar(512) NULL,
    `ResultReference` varchar(256) NULL,
    CONSTRAINT `PK_UserOperations` PRIMARY KEY (`Id`)
) CHARACTER SET=utf8mb4;

CREATE UNIQUE INDEX `IX_UserOperations_OwnerUserId_TaskType_IdempotencyKey` ON `UserOperations` (`OwnerUserId`, `TaskType`, `IdempotencyKey`);

CREATE INDEX `IX_UserOperations_Status_AvailableAtUtc_Priority` ON `UserOperations` (`Status`, `AvailableAtUtc`, `Priority`);

INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
VALUES ('20260802224646_AddUserOperations', '9.0.14');

COMMIT;

