START TRANSACTION;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `IDSRules` ADD `DeploymentMode` int NOT NULL DEFAULT 0;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `IDSRules` ADD `DeploymentTemplateId` bigint NULL;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `Hosts` ADD `NetworkInterfaces` longtext CHARACTER SET utf8mb4 NULL;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `Feeds` ADD `ConsecutiveFailureCount` int NOT NULL DEFAULT 0;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `Feeds` ADD `CreatedAt` datetime(6) NOT NULL DEFAULT '0001-01-01 00:00:00';

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `Feeds` ADD `CurrentCollectionPath` longtext CHARACTER SET utf8mb4 NULL;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `Feeds` ADD `CurrentPage` int NULL;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `Feeds` ADD `DeploymentMode` int NOT NULL DEFAULT 0;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `Feeds` ADD `DeploymentTemplateId` bigint NULL;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `Feeds` ADD `DownloadErrorMessages` longtext CHARACTER SET utf8mb4 NULL;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `Feeds` ADD `InitialDownloadRangeDays` int NOT NULL DEFAULT 0;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `Feeds` ADD `InitialDownloadStatus` int NOT NULL DEFAULT 0;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `Feeds` ADD `ItemsPerPage` int NOT NULL DEFAULT 0;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `Feeds` ADD `LastDownloadedItemTimestamp` bigint NULL;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `Feeds` ADD `PausedAt` datetime(6) NULL;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `Feeds` ADD `UpdatedAt` datetime(6) NOT NULL DEFAULT '0001-01-01 00:00:00';

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `ConfSets` ADD `CreatedAt` datetime(6) NOT NULL DEFAULT '0001-01-01 00:00:00';

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `ConfSets` ADD `CreatedBy` longtext CHARACTER SET utf8mb4 NULL;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `Clusters` MODIFY COLUMN `SSHPrivateKeyPath` longtext CHARACTER SET utf8mb4 NULL;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    CREATE TABLE `DataProtectionKeys` (
        `Id` int NOT NULL AUTO_INCREMENT,
        `FriendlyName` longtext CHARACTER SET utf8mb4 NULL,
        `Xml` longtext CHARACTER SET utf8mb4 NULL,
        CONSTRAINT `PK_DataProtectionKeys` PRIMARY KEY (`Id`)
    ) CHARACTER SET=utf8mb4;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    CREATE TABLE `HostServiceInstances` (
        `Id` bigint NOT NULL AUTO_INCREMENT,
        `Name` longtext CHARACTER SET utf8mb4 NULL,
        `Description` longtext CHARACTER SET utf8mb4 NULL,
        `TargetSoftwareName` longtext CHARACTER SET utf8mb4 NULL,
        `TargetSoftwareType` int NOT NULL,
        `Interfaces` longtext CHARACTER SET utf8mb4 NULL,
        `LoadedRules` int NOT NULL,
        `FailedRules` int NOT NULL,
        `LastRulesUpdate` datetime(6) NOT NULL,
        `LastIndicatorsUpdate` datetime(6) NOT NULL,
        `ActualServiceStatus` longtext CHARACTER SET utf8mb4 NULL,
        `ConfiguredServiceStatus` longtext CHARACTER SET utf8mb4 NULL,
        `Uptime` datetime(6) NOT NULL,
        `Metrics` longtext CHARACTER SET utf8mb4 NULL,
        `CreatedAt` datetime(6) NOT NULL,
        `UpdatedAt` datetime(6) NOT NULL,
        `HostId` bigint NOT NULL,
        CONSTRAINT `PK_HostServiceInstances` PRIMARY KEY (`Id`),
        CONSTRAINT `FK_HostServiceInstances_Hosts_HostId` FOREIGN KEY (`HostId`) REFERENCES `Hosts` (`Id`) ON DELETE CASCADE
    ) CHARACTER SET=utf8mb4;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    CREATE TABLE `IdsRuleDeploymentTemplates` (
        `Id` bigint NOT NULL AUTO_INCREMENT,
        `Name` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `Description` varchar(1000) CHARACTER SET utf8mb4 NULL,
        `NewClusterPolicy` int NOT NULL,
        `NewResourcePolicy` int NOT NULL,
        `CreatedBy` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
        `CreatedAt` datetime(6) NOT NULL,
        `UpdatedAt` datetime(6) NOT NULL,
        CONSTRAINT `PK_IdsRuleDeploymentTemplates` PRIMARY KEY (`Id`)
    ) CHARACTER SET=utf8mb4;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    CREATE TABLE `DeploymentTemplateClusterRelationship` (
        `TemplateId` bigint NOT NULL,
        `ClusterId` bigint NOT NULL,
        `RelationshipType` int NOT NULL,
        CONSTRAINT `PK_DeploymentTemplateClusterRelationship` PRIMARY KEY (`TemplateId`, `ClusterId`),
        CONSTRAINT `FK_DeploymentTemplateClusterRelationship_Clusters_ClusterId` FOREIGN KEY (`ClusterId`) REFERENCES `Clusters` (`Id`) ON DELETE CASCADE,
        CONSTRAINT `FK_DeploymentTemplateClusterRelationship_IdsRuleDeploymentTempl~` FOREIGN KEY (`TemplateId`) REFERENCES `IdsRuleDeploymentTemplates` (`Id`) ON DELETE CASCADE
    ) CHARACTER SET=utf8mb4;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    CREATE TABLE `DeploymentTemplateConfGroupRelationship` (
        `TemplateId` bigint NOT NULL,
        `ConfGroupId` bigint NOT NULL,
        `RelationshipType` int NOT NULL,
        CONSTRAINT `PK_DeploymentTemplateConfGroupRelationship` PRIMARY KEY (`TemplateId`, `ConfGroupId`),
        CONSTRAINT `FK_DeploymentTemplateConfGroupRelationship_ConfGroups_ConfGroup~` FOREIGN KEY (`ConfGroupId`) REFERENCES `ConfGroups` (`Id`) ON DELETE CASCADE,
        CONSTRAINT `FK_DeploymentTemplateConfGroupRelationship_IdsRuleDeploymentTem~` FOREIGN KEY (`TemplateId`) REFERENCES `IdsRuleDeploymentTemplates` (`Id`) ON DELETE CASCADE
    ) CHARACTER SET=utf8mb4;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    CREATE TABLE `DeploymentTemplateHostRelationship` (
        `TemplateId` bigint NOT NULL,
        `HostId` bigint NOT NULL,
        `RelationshipType` int NOT NULL,
        CONSTRAINT `PK_DeploymentTemplateHostRelationship` PRIMARY KEY (`TemplateId`, `HostId`),
        CONSTRAINT `FK_DeploymentTemplateHostRelationship_Hosts_HostId` FOREIGN KEY (`HostId`) REFERENCES `Hosts` (`Id`) ON DELETE CASCADE,
        CONSTRAINT `FK_DeploymentTemplateHostRelationship_IdsRuleDeploymentTemplate~` FOREIGN KEY (`TemplateId`) REFERENCES `IdsRuleDeploymentTemplates` (`Id`) ON DELETE CASCADE
    ) CHARACTER SET=utf8mb4;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    CREATE TABLE `DeploymentTemplateInstanceRelationship` (
        `TemplateId` bigint NOT NULL,
        `InstanceId` bigint NOT NULL,
        `RelationshipType` int NOT NULL,
        CONSTRAINT `PK_DeploymentTemplateInstanceRelationship` PRIMARY KEY (`TemplateId`, `InstanceId`),
        CONSTRAINT `FK_DeploymentTemplateInstanceRelationship_HostServiceInstances_~` FOREIGN KEY (`InstanceId`) REFERENCES `HostServiceInstances` (`Id`) ON DELETE CASCADE,
        CONSTRAINT `FK_DeploymentTemplateInstanceRelationship_IdsRuleDeploymentTemp~` FOREIGN KEY (`TemplateId`) REFERENCES `IdsRuleDeploymentTemplates` (`Id`) ON DELETE CASCADE
    ) CHARACTER SET=utf8mb4;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    CREATE INDEX `IX_IDSRules_DeploymentTemplateId` ON `IDSRules` (`DeploymentTemplateId`);

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    CREATE INDEX `IX_Feeds_DeploymentTemplateId` ON `Feeds` (`DeploymentTemplateId`);

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    CREATE INDEX `IX_DeploymentTemplateClusterRelationship_ClusterId` ON `DeploymentTemplateClusterRelationship` (`ClusterId`);

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    CREATE INDEX `IX_DeploymentTemplateConfGroupRelationship_ConfGroupId` ON `DeploymentTemplateConfGroupRelationship` (`ConfGroupId`);

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    CREATE INDEX `IX_DeploymentTemplateHostRelationship_HostId` ON `DeploymentTemplateHostRelationship` (`HostId`);

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    CREATE INDEX `IX_DeploymentTemplateInstanceRelationship_InstanceId` ON `DeploymentTemplateInstanceRelationship` (`InstanceId`);

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    CREATE INDEX `IX_HostServiceInstances_HostId` ON `HostServiceInstances` (`HostId`);

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    CREATE UNIQUE INDEX `IX_IdsRuleDeploymentTemplates_Name` ON `IdsRuleDeploymentTemplates` (`Name`);

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `Feeds` ADD CONSTRAINT `FK_Feeds_IdsRuleDeploymentTemplates_DeploymentTemplateId` FOREIGN KEY (`DeploymentTemplateId`) REFERENCES `IdsRuleDeploymentTemplates` (`Id`) ON DELETE RESTRICT;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    ALTER TABLE `IDSRules` ADD CONSTRAINT `FK_IDSRules_IdsRuleDeploymentTemplates_DeploymentTemplateId` FOREIGN KEY (`DeploymentTemplateId`) REFERENCES `IdsRuleDeploymentTemplates` (`Id`) ON DELETE RESTRICT;

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

DROP PROCEDURE IF EXISTS MigrationsScript;
DELIMITER //
CREATE PROCEDURE MigrationsScript()
BEGIN
    IF NOT EXISTS(SELECT 1 FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20251215182940_upgradefrom_2.9.0_to_3.0.0') THEN

    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20251215182940_upgradefrom_2.9.0_to_3.0.0', '8.0.12');

    -- mark all hosts as having stale config
    UPDATE `Hosts` SET `NeedsConfigUpdate`='1';

    -- set the database version to 2.9.0
    INSERT INTO `database_version_history` (`Version`, `UpdatedAt`) 
    VALUES ('3.0.0', NOW());

    END IF;
END //
DELIMITER ;
CALL MigrationsScript();
DROP PROCEDURE MigrationsScript;

COMMIT;


