-- Migration: Ajout des champs de suivi aux indicateurs de performance
-- Date: 2025-01-XX
-- Description: Ajoute les champs taux_realisation et statut pour le suivi des indicateurs de performance

SET @dbname = DATABASE();
SET @tablename = 'convention_indicateurs_performance';

-- Ajouter le champ taux_realisation (taux de réalisation en pourcentage)
SET @column1 = 'taux_realisation';
SET @preparedStatement1 = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
      AND (COLUMN_NAME = @column1)
  ) > 0,
  'SELECT "La colonne taux_realisation existe déjà." AS message;',
  CONCAT('ALTER TABLE `', @tablename, '` ADD COLUMN `', @column1, '` DECIMAL(5, 2) NULL DEFAULT NULL COMMENT "Taux de réalisation en pourcentage" AFTER `objectif`;')
));
PREPARE alterIfNotExists1 FROM @preparedStatement1;
EXECUTE alterIfNotExists1;
DEALLOCATE PREPARE alterIfNotExists1;

-- Ajouter le champ statut (Réalisé/Non/En cours)
SET @column2 = 'statut';
SET @preparedStatement2 = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename)
      AND (COLUMN_NAME = @column2)
  ) > 0,
  'SELECT "La colonne statut existe déjà." AS message;',
  CONCAT('ALTER TABLE `', @tablename, '` ADD COLUMN `', @column2, '` ENUM(\'Réalisé\',\'Non\',\'En cours\') NULL DEFAULT NULL COMMENT "Statut de réalisation" AFTER `taux_realisation`;')
));
PREPARE alterIfNotExists2 FROM @preparedStatement2;
EXECUTE alterIfNotExists2;
DEALLOCATE PREPARE alterIfNotExists2;

-- Message de confirmation
SELECT "Migration 009: Ajout des champs de suivi aux indicateurs de performance terminée avec succès." AS message;

