-- ============================================
-- Migration 0009: Augmenter la taille de la colonne libelle sur lcontrole_b
-- Date: 2026-01-16
-- ============================================
-- Passer la colonne libelle de VARCHAR(100) a VARCHAR(500)
-- ============================================

-- Table principale lcontrole_b
IF OBJECT_ID('lcontrole_b', 'U') IS NOT NULL
BEGIN
    IF EXISTS (
        SELECT 1
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'lcontrole_b'
        AND COLUMN_NAME = 'libelle'
        AND CHARACTER_MAXIMUM_LENGTH < 500
    )
    BEGIN
        -- Supprimer la contrainte DEFAULT existante
        DECLARE @ConstraintName NVARCHAR(256);
        SELECT @ConstraintName = d.name
        FROM sys.default_constraints d
            INNER JOIN sys.columns c ON d.parent_object_id = c.object_id AND d.parent_column_id = c.column_id
        WHERE d.parent_object_id = OBJECT_ID('lcontrole_b')
            AND c.name = 'libelle';

        IF @ConstraintName IS NOT NULL
        BEGIN
            EXEC('ALTER TABLE lcontrole_b DROP CONSTRAINT ' + @ConstraintName);
        END

        -- Modifier la colonne
        ALTER TABLE lcontrole_b ALTER COLUMN libelle VARCHAR(500) NOT NULL;

        -- Recreer la contrainte DEFAULT
        ALTER TABLE lcontrole_b ADD DEFAULT ('') FOR libelle;

        PRINT 'Colonne libelle agrandie a 500 sur la table lcontrole_b';
    END
    ELSE
    BEGIN
        PRINT 'Colonne libelle deja a 500 ou plus sur la table lcontrole_b';
    END
END
