﻿BEGIN TRANSACTION;
DROP INDEX [IX_DeploymentJobTargets_DeploymentJobId] ON [DeploymentJobTargets];

DROP INDEX [IX_DeploymentJobSteps_DeploymentJobId] ON [DeploymentJobSteps];

DECLARE @var nvarchar(max);
SELECT @var = QUOTENAME([d].[name])
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[DeploymentJobTargets]') AND [c].[name] = N'Status');
IF @var IS NOT NULL EXEC(N'ALTER TABLE [DeploymentJobTargets] DROP CONSTRAINT ' + @var + ';');
ALTER TABLE [DeploymentJobTargets] ALTER COLUMN [Status] nvarchar(450) NOT NULL;

ALTER TABLE [DeploymentJobTargets] ADD [Finished] datetime2 NULL;

ALTER TABLE [DeploymentJobTargets] ADD [OutputMetadataJson] nvarchar(max) NULL;

ALTER TABLE [DeploymentJobTargets] ADD [Started] datetime2 NULL;

DECLARE @var1 nvarchar(max);
SELECT @var1 = QUOTENAME([d].[name])
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[DeploymentJobSteps]') AND [c].[name] = N'Status');
IF @var1 IS NOT NULL EXEC(N'ALTER TABLE [DeploymentJobSteps] DROP CONSTRAINT ' + @var1 + ';');
ALTER TABLE [DeploymentJobSteps] ALTER COLUMN [Status] nvarchar(450) NOT NULL;

ALTER TABLE [DeploymentJobSteps] ADD [ErrorMessage] nvarchar(max) NULL;

ALTER TABLE [DeploymentJobSteps] ADD [Finished] datetime2 NULL;

ALTER TABLE [DeploymentJobSteps] ADD [OutputMetadataJson] nvarchar(max) NULL;

ALTER TABLE [DeploymentJobSteps] ADD [Started] datetime2 NULL;

DECLARE @var2 nvarchar(max);
SELECT @var2 = QUOTENAME([d].[name])
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[DeploymentJobs]') AND [c].[name] = N'Status');
IF @var2 IS NOT NULL EXEC(N'ALTER TABLE [DeploymentJobs] DROP CONSTRAINT ' + @var2 + ';');
ALTER TABLE [DeploymentJobs] ALTER COLUMN [Status] nvarchar(450) NOT NULL;

ALTER TABLE [DeploymentJobs] ADD [CorrelationId] uniqueidentifier NOT NULL DEFAULT (NEWID());

ALTER TABLE [DeploymentJobs] ADD [HeartbeatAt] datetime2 NULL;

ALTER TABLE [DeploymentJobs] ADD [Priority] int NOT NULL DEFAULT 100;

ALTER TABLE [DeploymentJobs] ADD [RowVersion] rowversion NOT NULL;

ALTER TABLE [DeploymentJobs] ADD [ScheduledAt] datetime2 NULL;

ALTER TABLE [DeploymentJobs] ADD [WorkerName] nvarchar(450) NULL;

CREATE INDEX [IX_DeploymentJobTargets_DeploymentJobId_Status] ON [DeploymentJobTargets] ([DeploymentJobId], [Status]);

ALTER TABLE [DeploymentJobTargets] ADD CONSTRAINT [CK_DeploymentJobTargets_OutputMetadataJson_IsJson] CHECK ([OutputMetadataJson] IS NULL OR ISJSON([OutputMetadataJson]) = 1);

CREATE INDEX [IX_DeploymentJobSteps_DeploymentJobId_Status_SortOrder] ON [DeploymentJobSteps] ([DeploymentJobId], [Status], [SortOrder]);

ALTER TABLE [DeploymentJobSteps] ADD CONSTRAINT [CK_DeploymentJobSteps_OutputMetadataJson_IsJson] CHECK ([OutputMetadataJson] IS NULL OR ISJSON([OutputMetadataJson]) = 1);

CREATE INDEX [IX_DeploymentJobs_CorrelationId] ON [DeploymentJobs] ([CorrelationId]);

CREATE INDEX [IX_DeploymentJobs_Status_ScheduledAt_LockedUntil_Priority_Created] ON [DeploymentJobs] ([Status], [ScheduledAt], [LockedUntil], [Priority], [Created]);

CREATE INDEX [IX_DeploymentJobs_WorkerName] ON [DeploymentJobs] ([WorkerName]);

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20260708194227_AddQueueHardening', N'10.0.8');

COMMIT;
GO

