﻿BEGIN TRANSACTION;
ALTER TABLE [DeploymentExecutions] DROP CONSTRAINT [FK_DeploymentExecutions_VirtualMachines_VirtualMachineId];

ALTER TABLE [DeploymentTargetAssignments] DROP CONSTRAINT [FK_DeploymentTargetAssignments_VirtualMachines_VirtualMachineId];

ALTER TABLE [VirtualMachines] DROP CONSTRAINT [FK_VirtualMachines_Domains_DomainID];

ALTER TABLE [VirtualMachines] DROP CONSTRAINT [PK_VirtualMachines];

EXEC sp_rename N'[VirtualMachines]', N'Targets', 'OBJECT';

EXEC sp_rename N'[DeploymentTargetAssignments].[VirtualMachineId]', N'TargetId', 'COLUMN';

EXEC sp_rename N'[DeploymentTargetAssignments].[IX_DeploymentTargetAssignments_VirtualMachineId]', N'IX_DeploymentTargetAssignments_TargetId', 'INDEX';

EXEC sp_rename N'[DeploymentTargetAssignments].[IX_DeploymentTargetAssignments_DeploymentGroupId_VirtualMachineId_RoleKey]', N'IX_DeploymentTargetAssignments_DeploymentGroupId_TargetId_RoleKey', 'INDEX';

EXEC sp_rename N'[DeploymentJobTargets].[VirtualMachineId]', N'TargetId', 'COLUMN';

EXEC sp_rename N'[DeploymentExecutions].[VirtualMachineId]', N'TargetId', 'COLUMN';

EXEC sp_rename N'[Targets].[IX_VirtualMachines_DomainID]', N'IX_Targets_DomainID', 'INDEX';

ALTER TABLE [Targets] ADD [ProviderType] nvarchar(64) NOT NULL DEFAULT N'OnPrem';

ALTER TABLE [Targets] ADD [TargetType] nvarchar(64) NOT NULL DEFAULT N'VirtualMachine';

ALTER TABLE [Targets] ADD CONSTRAINT [PK_Targets] PRIMARY KEY ([Id]);

IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'Id', N'Created', N'CreatedBy', N'DomainID', N'ExternalId', N'MetadataJson', N'Modified', N'ModifiedBy', N'Name', N'ProviderType', N'TargetType') AND [object_id] = OBJECT_ID(N'[Targets]'))
    SET IDENTITY_INSERT [Targets] ON;
INSERT INTO [Targets] ([Id], [Created], [CreatedBy], [DomainID], [ExternalId], [MetadataJson], [Modified], [ModifiedBy], [Name], [ProviderType], [TargetType])
VALUES ('30000000-0000-0000-0000-000000000006', '2026-07-07T00:00:00.0000000Z', N'DemoSeed', NULL, N'11111111-1111-1111-1111-111111111111', N'{"environment":"Production","workload":"M365"}', '2026-07-07T00:00:00.0000000Z', N'DemoSeed', N'contoso.onmicrosoft.com', N'Microsoft365', N'Tenant'),
('30000000-0000-0000-0000-000000000007', '2026-07-07T00:00:00.0000000Z', N'DemoSeed', NULL, N'Teams.StandardUsers', N'{"workload":"Teams","scope":"StandardUsers"}', '2026-07-07T00:00:00.0000000Z', N'DemoSeed', N'Teams - Standard Users', N'Microsoft365', N'PolicyScope');
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'Id', N'Created', N'CreatedBy', N'DomainID', N'ExternalId', N'MetadataJson', N'Modified', N'ModifiedBy', N'Name', N'ProviderType', N'TargetType') AND [object_id] = OBJECT_ID(N'[Targets]'))
    SET IDENTITY_INSERT [Targets] OFF;

ALTER TABLE [Targets] ADD CONSTRAINT [FK_Targets_Domains_DomainID] FOREIGN KEY ([DomainID]) REFERENCES [Domains] ([Id]);

ALTER TABLE [DeploymentExecutions] ADD CONSTRAINT [FK_DeploymentExecutions_Targets_TargetId] FOREIGN KEY ([TargetId]) REFERENCES [Targets] ([Id]) ON DELETE CASCADE;

ALTER TABLE [DeploymentTargetAssignments] ADD CONSTRAINT [FK_DeploymentTargetAssignments_Targets_TargetId] FOREIGN KEY ([TargetId]) REFERENCES [Targets] ([Id]) ON DELETE NO ACTION;

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20260707211957_AddGenericTargetModel', N'10.0.8');

COMMIT;
GO

