-- Migration: Ajout du champ est_engage aux tables de contributions annuelles
-- Date: 2025-01-XX
-- Description: Ajoute est_engage pour indiquer si l'engagement est effectué pour chaque année

SET @dbname = DATABASE();

-- 1. Ajouter est_engage à convention_contribution_annuelle (conventions normales)
SET @tablename1 = 'convention_contribution_annuelle';
SET @column = 'est_engage';

SET @preparedStatement1 = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename1)
      AND (COLUMN_NAME = @column)
  ) > 0,
  'SELECT "La colonne est_engage existe déjà dans convention_contribution_annuelle." AS message;',
  CONCAT('ALTER TABLE `', @tablename1, '` ADD COLUMN `', @column, '` TINYINT(1) NULL DEFAULT 0 COMMENT "Engagement effectué pour cette année (1) ou non (0)" AFTER `est_verse`;')
));
PREPARE alterIfNotExists1 FROM @preparedStatement1;
EXECUTE alterIfNotExists1;
DEALLOCATE PREPARE alterIfNotExists1;

-- 2. Ajouter est_engage à contribution_annuelle (conventions spécifiques, avenants, projets)
SET @tablename2 = 'contribution_annuelle';

SET @preparedStatement2 = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (TABLE_SCHEMA = @dbname)
      AND (TABLE_NAME = @tablename2)
      AND (COLUMN_NAME = @column)
  ) > 0,
  'SELECT "La colonne est_engage existe déjà dans contribution_annuelle." AS message;',
  CONCAT('ALTER TABLE `', @tablename2, '` ADD COLUMN `', @column, '` TINYINT(1) NULL DEFAULT 0 COMMENT "Engagement effectué pour cette année (1) ou non (0)" AFTER `verse`;')
));
PREPARE alterIfNotExists2 FROM @preparedStatement2;
EXECUTE alterIfNotExists2;
DEALLOCATE PREPARE alterIfNotExists2;

-- Message de confirmation
SELECT "Migration 046: Ajout du champ est_engage aux tables de contributions annuelles terminée avec succès." AS message;

