-- Converts the 'value' column from a string to a boolean.
-- If 'value' is 'true' (case insensitive), it will be set to TRUE, otherwise FALSE.

DO $$
BEGIN
    -- Check if the column is not already boolean
    IF EXISTS (
        SELECT 1 
        FROM information_schema.columns 
        WHERE table_schema = 'public' 
        AND table_name = 'flags' 
        AND column_name = 'value'
        AND data_type != 'boolean'
    ) THEN
        -- Convert the column type
        ALTER TABLE public.flags
        ALTER COLUMN value TYPE BOOLEAN USING (LOWER(value) = 'true');
    END IF;
END $$;

-- Ensure that the 'value' column is not NULL (idempotent)
ALTER TABLE public.flags ALTER COLUMN VALUE SET NOT NULL;

-- Set a default value for the 'value' column (idempotent)
ALTER TABLE public.flags ALTER COLUMN VALUE SET DEFAULT FALSE;