START TRANSACTION;

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

    ALTER TABLE `Clusters` ADD `EncryptedPrivateKey` 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` = '20250101154952_upgradefrom_2.8.0_to_2.9.0') THEN

    ALTER TABLE `Clusters` ADD `EncryptedPublicKey` 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` = '20250101154952_upgradefrom_2.8.0_to_2.9.0') THEN

    INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
    VALUES ('20250101154952_upgradefrom_2.8.0_to_2.9.0', '8.0.11');

    -- 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 ('2.9.0', NOW());

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

COMMIT;


