<commit_before>class RemoveRedundantPipelineStages < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  def up
   redundant_stages_ids = <<~SQL
      SELECT id FROM ci_stages WHERE (pipeline_id, name) IN (
        SELECT pipeline_id, name FROM ci_stages
          GROUP BY pipeline_id, name HAVING COUNT(*) > 1
      )
    SQL

    execute <<~SQL
      UPDATE ci_builds SET stage_id = NULL WHERE stage_id IN (#{redundant_stages_ids})
    SQL

    if Gitlab::Database.postgresql?
      execute <<~SQL
        DELETE FROM ci_stages WHERE id IN (#{redundant_stages_ids})
      SQL
    else # We can't modify a table we are selecting from on MySQL
      execute <<~SQL
        DELETE a FROM ci_stages AS a, ci_stages AS b
          WHERE a.pipeline_id = b.pipeline_id AND a.name = b.name
      SQL
    end
  end
<commit_msg>Fix migration removing duplicate stages on MySQL again<commit_after>class RemoveRedundantPipelineStages < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  def up
   redundant_stages_ids = <<~SQL
      SELECT id FROM ci_stages WHERE (pipeline_id, name) IN (
        SELECT pipeline_id, name FROM ci_stages
          GROUP BY pipeline_id, name HAVING COUNT(*) > 1
      )
    SQL

    execute <<~SQL
      UPDATE ci_builds SET stage_id = NULL WHERE stage_id IN (#{redundant_stages_ids})
    SQL

    if Gitlab::Database.postgresql?
      execute <<~SQL
        DELETE FROM ci_stages WHERE id IN (#{redundant_stages_ids})
      SQL
    else # We can't modify a table we are selecting from on MySQL
      execute <<~SQL
        DELETE a FROM ci_stages AS a, ci_stages AS b
          WHERE a.pipeline_id = b.pipeline_id AND a.name = b.name
            AND a.id <> b.id
      SQL
    end
  end

{'fraction_non_alphanumeric': 0.06808035714285714, 'fraction_numerical': 0.0011160714285714285, 'mean_word_length': 2.719917012448133, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '937881', 'n_tokens_mistral': 580, 'n_tokens_neox': 559, 'n_words': 186}