USE bulo;

SET @col_placa := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'veiculos' AND COLUMN_NAME = 'placa'
);
SET @sql := IF(@col_placa = 0, 'ALTER TABLE veiculos ADD COLUMN placa VARCHAR(10) NULL AFTER cor', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @idx_placa := (
  SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'veiculos' AND INDEX_NAME = 'idx_veiculos_placa'
);
SET @sql := IF(@idx_placa = 0, 'ALTER TABLE veiculos ADD INDEX idx_veiculos_placa (placa)', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
