Passed
Push — master ( 6bc43d...2da2ee )
by Ahmad
07:04
created

ChangeRolePriorityToUnique.change()   A

Complexity

Conditions 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 19
rs 9.45
c 1
b 0
f 0
1
# frozen_string_literal: true
2
3
class MigrationProduct < ActiveRecord::Base
4
  self.table_name = :roles
5
end
6
7
class ChangeRolePriorityToUnique < ActiveRecord::Migration[5.2]
8
  def change
9
    reversible do |dir|
10
      dir.up do
11
        MigrationProduct.where("priority < 0").where.not(name: "pending").each do |role|
12
          role.decrement!(:priority)
13
        end
14
15
        add_index MigrationProduct, [:priority, :provider], unique: true
16
      end
17
18
      dir.down do
19
        remove_index MigrationProduct, [:priority, :provider]
20
21
        MigrationProduct.where("priority < 0").where.not(name: "pending").each do |role|
22
          role.increment!(:priority)
23
        end
24
      end
25
    end
26
  end
27
end
28