Class: ForeignKeyChecker::ZombieResult

Inherits:
Result
  • Object
show all
Defined in:
lib/foreign_key_checker.rb

Instance Attribute Summary collapse

Attributes inherited from Result

#association, #model

Instance Method Summary collapse

Methods inherited from Result

#from_column, #from_table, #human_relation, #initialize, #inspect, #nullable?, #to_column, #to_table

Constructor Details

This class inherits a constructor from ForeignKeyChecker::Result

Instance Attribute Details

#scopeObject (readonly)

Returns the value of attribute scope.



71
72
73
# File 'lib/foreign_key_checker.rb', line 71

def scope
  @scope
end

#zombiesObject (readonly)

Returns the value of attribute zombies.



71
72
73
# File 'lib/foreign_key_checker.rb', line 71

def zombies
  @zombies
end

Instance Method Details

#delete_sqlObject



76
77
78
79
80
81
82
83
# File 'lib/foreign_key_checker.rb', line 76

def delete_sql
  from_t = model.connection.quote_table_name(from_table)
  from_c = model.connection.quote_column_name(from_column)
  to_t = model.connection.quote_table_name(to_table)
  to_c = model.connection.quote_column_name(to_column)
  #"DELETE #{from_t} FROM #{from_t}.#{from_c} LEFT OUTER JOIN #{to_t} ON #{to_t}.#{to_c} = #{from_t}.#{from_c} WHERE #{to_t}.#{to_c} IS NULL"
  "DELETE FROM #{from_t} WHERE #{from_c} IS NOT NULL AND #{from_c} NOT IN (SELECT #{to_c} FROM #{to_t})"
end

#messageObject



103
104
105
# File 'lib/foreign_key_checker.rb', line 103

def message
  "#{human_relation} with #{zombies} zombies; processed by statement:\n#{sql}\n"
end

#migration(set_null: false) ⇒ Object



98
99
100
101
# File 'lib/foreign_key_checker.rb', line 98

def migration(set_null: false)
  return set_null_migration if set_null
  "execute('#{delete_sql}')"
end

#set_null_migrationObject



94
95
96
# File 'lib/foreign_key_checker.rb', line 94

def set_null_migration
  "execute('#{set_null_sql}')"
end

#set_null_sqlObject



85
86
87
88
89
90
91
92
# File 'lib/foreign_key_checker.rb', line 85

def set_null_sql
  from_t = model.connection.quote_table_name(from_table)
  from_c = model.connection.quote_column_name(from_column)
  to_t = model.connection.quote_table_name(to_table)
  to_c = model.connection.quote_column_name(to_column)
  #"UPDATE #{from_t} SET #{from_t}.#{from_c} = NULL FROM #{from_t} LEFT OUTER JOIN #{to_t} ON #{to_t}.#{to_c} = #{from_t}.#{from_c} WHERE #{to_t}.#{to_c} IS NULL"
  "UPDATE #{from_t} SET #{from_c} = NULL WHERE #{from_c} IS NOT NULL AND #{from_c} NOT IN (SELECT #{to_c} FROM #{to_t})"
end

#sqlObject



72
73
74
# File 'lib/foreign_key_checker.rb', line 72

def sql
  scope.to_sql
end