USE bulo;

SET @col_rating := (
  SELECT COUNT(*)
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'corridas'
    AND COLUMN_NAME = 'avaliacao_motorista'
);
SET @sql := IF(
  @col_rating = 0,
  'ALTER TABLE corridas ADD COLUMN avaliacao_motorista TINYINT NULL AFTER valor',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @col_avaliado_em := (
  SELECT COUNT(*)
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'corridas'
    AND COLUMN_NAME = 'avaliado_em'
);
SET @sql := IF(
  @col_avaliado_em = 0,
  'ALTER TABLE corridas ADD COLUMN avaliado_em DATETIME NULL AFTER avaliacao_motorista',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
