Module: Dbchecker::DSL

Included in:
Checker
Defined in:
lib/dbchecker/dsl.rb

Instance Method Summary collapse

Instance Method Details

#check_duplicates(*fields) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dbchecker/dsl.rb', line 89

def check_duplicates(*fields)
  raise 'Empty fields' if fields.empty?
  fields_str = fields.join(', ')

  total = table.select(:id).group("#{fields_str}").having("COUNT(id) > ?", 1)
  unless total.empty?
    self.log "There are #{total.size} invalid rows in #{table} because #{fields_str} are duplicated"
    self.log "IDs: #{total}"
  end

  total.map(&:id)
end

#check_equal(*fields) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dbchecker/dsl.rb', line 75

def check_equal(*fields)
  raise 'Empty fields' if fields.empty?
  raise 'Two fields required' if fields.size != 2
  first_field, second_field = fields[0], fields[1]

  total = table.select(:id).where("#{first_field} = #{second_field}")
  if total.size > 0
    self.log "There are #{total.size} invalid rows in #{table} because #{first_field} and #{second_field} are equal"
    self.log "IDs: #{total}"
  end

  total.map(&:id)
end

#check_negatives(*fields) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dbchecker/dsl.rb', line 43

def check_negatives(*fields)
  raise 'Empty fields' if fields.empty?
  res = []

  fields.each do |field|
    total = table.select(:id).where("#{field} < 0")
    if total.size > 0
      self.log "There are #{total.size} invalid rows in #{table} because #{field} is negative"
      self.log "IDs: #{total}"
    end
    res.push total
  end

  res
end

#check_nil(*fields) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dbchecker/dsl.rb', line 4

def check_nil(*fields)
  raise 'Empty fields' if fields.empty?
  res = []

  fields.each do |field|
    bad = table.select(:id).where(field => nil)
    total = bad.map(&:id)

    unless total.empty?
      self.log "There are #{total.size} invalid rows in #{table} because #{field} is nil"
      self.log "IDs: #{total}"
    end
    res.push total
  end

  res
end

#check_references(*fields) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dbchecker/dsl.rb', line 22

def check_references(*fields)
  raise 'Empty fields' if fields.empty?
  res = []

  fields.each do |field|
    field_name = "#{field}_id"
    references = table.select(:id).all.map{|r| r.id }
    from = field.to_s.classify.constantize
    all = from.select(:id).all.map(&:id)
    total = references - all

    if total.size > 0
      self.log "There are #{total.size} invalid rows in #{table} because #{field_name} is not existant"
      self.log "IDs: #{total}"
    end
    res.push total
  end

  res
end

#check_zero(*fields) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dbchecker/dsl.rb', line 59

def check_zero(*fields)
  raise 'Empty fields' if fields.empty?
  res = []

  fields.each do |field|
    total = table.select(:id).where("#{field} = 0")
    if total.size > 0
      self.log "There are #{total.size} invalid rows in #{table} because #{field} is zero"
      self.log "IDs: #{total}"
    end
    res.push total
  end

  res
end

#log(msg) ⇒ Object



102
103
104
# File 'lib/dbchecker/dsl.rb', line 102

def log(msg)
  puts msg unless Rails.env.test?
end