SET @schema_name := DATABASE();

SET @use_can_index_exists := (
    SELECT COUNT(1)
    FROM information_schema.statistics
    WHERE table_schema = @schema_name
      AND table_name = 'use_can'
      AND index_name = 'idx_use_can_channel_donation_filter'
);
SET @use_can_index_sql := IF(
    @use_can_index_exists = 0,
    'ALTER TABLE use_can ADD INDEX idx_use_can_channel_donation_filter (can_usage, is_refund, created_at, id)',
    'SELECT "idx_use_can_channel_donation_filter already exists"'
);
PREPARE use_can_index_stmt FROM @use_can_index_sql;
EXECUTE use_can_index_stmt;
DEALLOCATE PREPARE use_can_index_stmt;

SET @use_can_calculate_join_index_exists := (
    SELECT COUNT(1)
    FROM information_schema.statistics
    WHERE table_schema = @schema_name
      AND table_name = 'use_can_calculate'
      AND index_name = 'idx_use_can_calculate_settlement_join'
);
SET @use_can_calculate_join_index_sql := IF(
    @use_can_calculate_join_index_exists = 0,
    'ALTER TABLE use_can_calculate ADD INDEX idx_use_can_calculate_settlement_join (use_can_id, status, recipient_creator_id)',
    'SELECT "idx_use_can_calculate_settlement_join already exists"'
);
PREPARE use_can_calculate_join_index_stmt FROM @use_can_calculate_join_index_sql;
EXECUTE use_can_calculate_join_index_stmt;
DEALLOCATE PREPARE use_can_calculate_join_index_stmt;

SET @use_can_calculate_creator_index_exists := (
    SELECT COUNT(1)
    FROM information_schema.statistics
    WHERE table_schema = @schema_name
      AND table_name = 'use_can_calculate'
      AND index_name = 'idx_use_can_calculate_creator_settlement'
);
SET @use_can_calculate_creator_index_sql := IF(
    @use_can_calculate_creator_index_exists = 0,
    'ALTER TABLE use_can_calculate ADD INDEX idx_use_can_calculate_creator_settlement (recipient_creator_id, status, use_can_id)',
    'SELECT "idx_use_can_calculate_creator_settlement already exists"'
);
PREPARE use_can_calculate_creator_index_stmt FROM @use_can_calculate_creator_index_sql;
EXECUTE use_can_calculate_creator_index_stmt;
DEALLOCATE PREPARE use_can_calculate_creator_index_stmt;
