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

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    add_concurrent_foreign_key :ci_stages, :projects, column: :project_id, on_delete: :cascade
    add_concurrent_foreign_key :ci_builds, :ci_stages, column: :stage_id, on_delete: :cascade
  end

  def down
    remove_foreign_key :ci_stages, column: :project_id
    remove_foreign_key :ci_builds, column: :stage_id
  end
end<commit_msg>Remove obsolete stages/build before adding foreign keys<commit_after>class CreateForeignKeysForPipelineStages < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  def up
    execute <<~SQL
      DELETE FROM ci_stages
        WHERE NOT EXISTS (
          SELECT true FROM projects
            WHERE projects.id = ci_stages.project_id
        )
    SQL

    execute <<~SQL
      DELETE FROM ci_builds
        WHERE NOT EXISTS (
          SELECT true FROM ci_stages
            WHERE ci_stages.id = ci_builds.stage_id
        )
    SQL

    add_concurrent_foreign_key :ci_stages, :projects, column: :project_id, on_delete: :cascade
    add_concurrent_foreign_key :ci_builds, :ci_stages, column: :stage_id, on_delete: :cascade
  end

  def down
    remove_foreign_key :ci_stages, column: :project_id
    remove_foreign_key :ci_builds, column: :stage_id
  end
end
{'fraction_non_alphanumeric': 0.06422651933701658, 'fraction_numerical': 0.0, 'mean_word_length': 3.83, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '5453559', 'n_tokens_mistral': 472, 'n_tokens_neox': 454, 'n_words': 111}