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.



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

def scope
  @scope
end

#zombiesObject (readonly)

Returns the value of attribute zombies.



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

def zombies
  @zombies
end

Instance Method Details

#delete_sqlObject



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

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



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

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

#migration(set_null: false) ⇒ Object



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

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

#set_null_migrationObject



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

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

#set_null_sqlObject



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

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



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

def sql
  scope.to_sql
end