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

  DOWNTIME = true
  DOWNTIME_REASON = 'Subscriptions will not work as expected until this migration is complete.'

  def up
    if Gitlab::Database.mysql?
      execute <<-EOF.strip_heredoc
        UPDATE subscriptions
          INNER JOIN issues ON issues.id = subscriptions.subscribable_id AND subscriptions.subscribable_type = 'Issue'
        SET subscriptions.project_id = issues.project_id;
      EOF

      execute <<-EOF.strip_heredoc
        UPDATE subscriptions
          INNER JOIN merge_requests ON merge_requests.id = subscriptions.subscribable_id AND subscriptions.subscribable_type = 'MergeRequest'
        SET subscriptions.project_id = merge_requests.target_project_id;
      EOF

      execute <<-EOF.strip_heredoc
        UPDATE subscriptions
          INNER JOIN labels ON labels.id = subscriptions.subscribable_id AND subscriptions.subscribable_type = 'Label'
          INNER JOIN projects ON projects.id = labels.project_id
        SET subscriptions.project_id = projects.id;
      EOF
    else
      execute <<-EOF.strip_heredoc
        UPDATE subscriptions
        SET project_id = issues.project_id
        FROM issues
        WHERE issues.id = subscriptions.subscribable_id
          AND subscriptions.subscribable_type = 'Issue';
      EOF

      execute <<-EOF.strip_heredoc
        UPDATE subscriptions
        SET project_id = merge_requests.target_project_id
        FROM merge_requests
        WHERE merge_requests.id = subscriptions.subscribable_id
          AND subscriptions.subscribable_type = 'MergeRequest';
      EOF

      execute <<-EOF.strip_heredoc
        UPDATE subscriptions
        SET project_id = projects.id
        FROM labels INNER JOIN projects ON projects.id = labels.project_id
        WHERE labels.id = subscriptions.subscribable_id
          AND subscriptions.subscribable_type = 'Label';
      EOF
    end
  end

  def down
    execute <<-EOF.strip_heredoc
      UPDATE subscriptions SET project_id = NULL;
    EOF
  end
end<commit_msg>Use subqueries instead of joins to migrate subscriptions<commit_after>class MigrateSubscriptionsProjectId < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = true
  DOWNTIME_REASON = 'Subscriptions will not work as expected until this migration is complete.'

  def up
    execute <<-EOF.strip_heredoc
      UPDATE subscriptions
      SET project_id = (
        SELECT issues.project_id
        FROM issues
        WHERE issues.id = subscriptions.subscribable_id AND subscriptions.subscribable_type = 'Issue'
      )
      WHERE subscriptions.subscribable_type = 'Issue';
    EOF

    execute <<-EOF.strip_heredoc
      UPDATE subscriptions
      SET project_id = (
        SELECT merge_requests.target_project_id
        FROM merge_requests
        WHERE merge_requests.id = subscriptions.subscribable_id AND subscriptions.subscribable_type = 'MergeRequest'
      )
      WHERE subscriptions.subscribable_type = 'MergeRequest';
    EOF

    execute <<-EOF.strip_heredoc
      UPDATE subscriptions
      SET project_id = (
        SELECT projects.id
        FROM labels INNER JOIN projects ON projects.id = labels.project_id
        WHERE labels.id = subscriptions.subscribable_id AND subscriptions.subscribable_type = 'Label'
      )
      WHERE subscriptions.subscribable_type = 'Label';
    EOF
  end

  def down
    execute <<-EOF.strip_heredoc
      UPDATE subscriptions SET project_id = NULL;
    EOF
  end
end
{'fraction_non_alphanumeric': 0.05666199158485274, 'fraction_numerical': 0.0, 'mean_word_length': 2.922992299229923, 'pattern_counts': {'":': 0, '<': 27, '<?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': '4439952', 'n_tokens_mistral': 1023, 'n_tokens_neox': 959, 'n_words': 255}