SET @idx_reports_report_date_id_exists := (
  SELECT COUNT(1)
  FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'production_reports'
    AND index_name = 'idx_reports_report_date_id'
);
SET @idx_reports_report_date_id_sql := IF(
  @idx_reports_report_date_id_exists = 0,
  'ALTER TABLE production_reports ADD INDEX idx_reports_report_date_id (report_date, id)',
  'SELECT ''idx_reports_report_date_id already exists'''
);
PREPARE idx_reports_report_date_id_stmt FROM @idx_reports_report_date_id_sql;
EXECUTE idx_reports_report_date_id_stmt;
DEALLOCATE PREPARE idx_reports_report_date_id_stmt;

SET @idx_reports_updated_at_id_exists := (
  SELECT COUNT(1)
  FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'production_reports'
    AND index_name = 'idx_reports_updated_at_id'
);
SET @idx_reports_updated_at_id_sql := IF(
  @idx_reports_updated_at_id_exists = 0,
  'ALTER TABLE production_reports ADD INDEX idx_reports_updated_at_id (updated_at, id)',
  'SELECT ''idx_reports_updated_at_id already exists'''
);
PREPARE idx_reports_updated_at_id_stmt FROM @idx_reports_updated_at_id_sql;
EXECUTE idx_reports_updated_at_id_stmt;
DEALLOCATE PREPARE idx_reports_updated_at_id_stmt;

SET @idx_report_items_report_id_id_exists := (
  SELECT COUNT(1)
  FROM information_schema.statistics
  WHERE table_schema = DATABASE()
    AND table_name = 'production_report_items'
    AND index_name = 'idx_report_items_report_id_id'
);
SET @idx_report_items_report_id_id_sql := IF(
  @idx_report_items_report_id_id_exists = 0,
  'ALTER TABLE production_report_items ADD INDEX idx_report_items_report_id_id (report_id, id)',
  'SELECT ''idx_report_items_report_id_id already exists'''
);
PREPARE idx_report_items_report_id_id_stmt FROM @idx_report_items_report_id_id_sql;
EXECUTE idx_report_items_report_id_id_stmt;
DEALLOCATE PREPARE idx_report_items_report_id_id_stmt;
