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

ChangeRolePriorityToUnique   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 21
rs 10
c 1
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A change() 0 19 1
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