BigW Consortium Gitlab

20170825104051_migrate_issues_to_ghost_user.rb 875 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
class MigrateIssuesToGhostUser < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers
  DOWNTIME = false

  disable_ddl_transaction!

  class User < ActiveRecord::Base
    self.table_name = 'users'
  end

  class Issue < ActiveRecord::Base
    self.table_name = 'issues'

    include ::EachBatch
  end

  def reset_column_in_migration_models
    ActiveRecord::Base.clear_cache!

    ::User.reset_column_information
21
    ::Namespace.reset_column_information
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  end

  def up
    reset_column_in_migration_models

    # we use the model method because rewriting it is too complicated and would require copying multiple methods
    ghost_id = ::User.ghost.id

    Issue.where('NOT EXISTS (?)', User.unscoped.select(1).where('issues.author_id = users.id')).each_batch do |relation|
      relation.update_all(author_id: ghost_id)
    end
  end

  def down
  end
end