-- DDL conservador e idempotente para preparar proformas con document-series.
-- Compatible con MariaDB/MySQL sin depender de ALTER ... IF NOT EXISTS.
-- Ejecutar solo en una BD de desarrollo segura.
-- No ejecutar automaticamente en produccion.

SET @target_schema = DATABASE();

SET
    @ddl = (
        SELECT IF(
                EXISTS (
                    SELECT 1
                    FROM information_schema.columns
                    WHERE
                        table_schema = @target_schema
                        AND table_name = 'proformas'
                        AND column_name = 'document_series_id'
                ), 'SELECT ''document_series_id already exists''', 'ALTER TABLE proformas ADD COLUMN document_series_id CHAR(36) NULL AFTER company_id'
            )
    );

PREPARE stmt FROM @ddl;

EXECUTE stmt;

DEALLOCATE PREPARE stmt;

SET
    @ddl = (
        SELECT IF(
                EXISTS (
                    SELECT 1
                    FROM information_schema.columns
                    WHERE
                        table_schema = @target_schema
                        AND table_name = 'proformas'
                        AND column_name = 'proforma_number'
                ), 'SELECT ''proforma_number already exists''', 'ALTER TABLE proformas ADD COLUMN proforma_number VARCHAR(64) NULL AFTER proforma_reference'
            )
    );

PREPARE stmt FROM @ddl;

EXECUTE stmt;

DEALLOCATE PREPARE stmt;

SET
    @ddl = (
        SELECT IF(
                EXISTS (
                    SELECT 1
                    FROM information_schema.statistics
                    WHERE
                        table_schema = @target_schema
                        AND table_name = 'proformas'
                        AND index_name = 'idx_proformas_company_document_series'
                ), 'SELECT ''idx_proformas_company_document_series already exists''', 'ALTER TABLE proformas ADD INDEX idx_proformas_company_document_series (company_id, document_series_id)'
            )
    );

PREPARE stmt FROM @ddl;

EXECUTE stmt;

DEALLOCATE PREPARE stmt;

SET
    @ddl = (
        SELECT IF(
                EXISTS (
                    SELECT 1
                    FROM information_schema.statistics
                    WHERE
                        table_schema = @target_schema
                        AND table_name = 'proformas'
                        AND index_name = 'idx_proformas_company_proforma_reference'
                ), 'SELECT ''idx_proformas_company_proforma_reference already exists''', 'ALTER TABLE proformas ADD INDEX idx_proformas_company_proforma_reference (company_id, proforma_reference)'
            )
    );

PREPARE stmt FROM @ddl;

EXECUTE stmt;

DEALLOCATE PREPARE stmt;

-- Validacion minima posterior a la DDL.
-- SELECT column_name
-- FROM information_schema.columns
-- WHERE table_schema = DATABASE()
--   AND table_name = 'proformas'
--   AND column_name IN ('document_series_id', 'proforma_number');
--
-- SELECT index_name, GROUP_CONCAT(column_name ORDER BY seq_in_index) AS indexed_columns
-- FROM information_schema.statistics
-- WHERE table_schema = DATABASE()
--   AND table_name = 'proformas'
--   AND index_name IN (
--     'idx_proformas_company_document_series',
--     'idx_proformas_company_proforma_reference'
--   )
-- GROUP BY index_name;