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.



77
78
79
# File 'lib/foreign_key_checker.rb', line 77

def scope
  @scope
end

#zombiesObject (readonly)

Returns the value of attribute zombies.



77
78
79
# File 'lib/foreign_key_checker.rb', line 77

def zombies
  @zombies
end

Instance Method Details

#delete_sqlObject



82
83
84
85
86
87
88
89
# File 'lib/foreign_key_checker.rb', line 82

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 * FROM (SELECT #{to_c} FROM #{to_t}) AS t )"
end

#messageObject



109
110
111
# File 'lib/foreign_key_checker.rb', line 109

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

#migration(set_null: false) ⇒ Object



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

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

#set_null_migrationObject



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

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

#set_null_sqlObject



91
92
93
94
95
96
97
98
# File 'lib/foreign_key_checker.rb', line 91

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 * FROM (SELECT #{to_c} FROM #{to_t}) AS t )"
end

#sqlObject



78
79
80
# File 'lib/foreign_key_checker.rb', line 78

def sql
  scope.to_sql
end