Class: YeetDba::VerifyData

Inherits:
Object
  • Object
show all
Defined in:
lib/yeet_dba/verify_data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column:) ⇒ VerifyData

Returns a new instance of VerifyData.



5
6
7
# File 'lib/yeet_dba/verify_data.rb', line 5

def initialize(column:)
  @column = column
end

Instance Attribute Details

#columnObject

Returns the value of attribute column.



3
4
5
# File 'lib/yeet_dba/verify_data.rb', line 3

def column
  @column
end

Instance Method Details

#orphaned_rowsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yeet_dba/verify_data.rb', line 21

def orphaned_rows
  association = column.association

  column_name = column.db_column.name
  table_name = column.table_name
  association_table = column.association_table_name
  model = column.model

  # Check to see there could be rows with bad data
  if model
    model.joins("left join #{association_table} as association_table on association_table.id = #{table_name}.#{column_name}")
        .where.not(column_name => nil)
        .where('association_table.id is null')
  else
    []
  end
end

#orphaned_rows?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/yeet_dba/verify_data.rb', line 9

def orphaned_rows?
  orphaned_rows.first
end

#orphaned_rows_countObject



13
14
15
# File 'lib/yeet_dba/verify_data.rb', line 13

def orphaned_rows_count
  orphaned_rows.count
end

#queryObject



17
18
19
# File 'lib/yeet_dba/verify_data.rb', line 17

def query
  orphaned_rows.to_sql
end