20180227182112_add_group_id_to_boards_ce.rb 926 Bytes
Newer Older
1
class AddGroupIdToBoardsCe < ActiveRecord::Migration[4.2]
2 3 4 5 6 7 8
  include Gitlab::Database::MigrationHelpers

  disable_ddl_transaction!

  DOWNTIME = false

  def up
Felipe Artur's avatar
Felipe Artur committed
9 10
    return if group_id_exists?

11
    # rubocop:disable Migration/AddConcurrentForeignKey
Felipe Artur's avatar
Felipe Artur committed
12 13 14
    add_column :boards, :group_id, :integer
    add_foreign_key :boards, :namespaces, column: :group_id, on_delete: :cascade
    add_concurrent_index :boards, :group_id
15

Felipe Artur's avatar
Felipe Artur committed
16
    change_column_null :boards, :project_id, true
17 18 19
  end

  def down
Felipe Artur's avatar
Felipe Artur committed
20 21
    return unless group_id_exists?

22
    # rubocop:disable Migration/RemoveIndex
Felipe Artur's avatar
Felipe Artur committed
23 24 25 26 27 28
    remove_foreign_key :boards, column: :group_id
    remove_index :boards, :group_id if index_exists? :boards, :group_id
    remove_column :boards, :group_id

    execute "DELETE from boards WHERE project_id IS NULL"
    change_column_null :boards, :project_id, false
29 30 31 32 33 34 35 36
  end

  private

  def group_id_exists?
    column_exists?(:boards, :group_id)
  end
end