-- Script incremental idempotente para melhorar a consulta do modulo de clientes
SET @tem_idx_email := (
    SELECT COUNT(*)
    FROM information_schema.statistics
    WHERE table_schema = DATABASE()
      AND table_name = 'clientes'
      AND index_name = 'idx_clientes_email'
);
SET @sql_email := IF(@tem_idx_email = 0,
    'ALTER TABLE clientes ADD KEY idx_clientes_email (email)',
    'SELECT "idx_clientes_email ja existe"'
);
PREPARE stmt FROM @sql_email;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @tem_idx_responsavel := (
    SELECT COUNT(*)
    FROM information_schema.statistics
    WHERE table_schema = DATABASE()
      AND table_name = 'clientes'
      AND index_name = 'idx_clientes_nome_responsavel'
);
SET @sql_responsavel := IF(@tem_idx_responsavel = 0,
    'ALTER TABLE clientes ADD KEY idx_clientes_nome_responsavel (nome_responsavel)',
    'SELECT "idx_clientes_nome_responsavel ja existe"'
);
PREPARE stmt FROM @sql_responsavel;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
