Sha256: bedc1dc3e987e50a3936b479cd8b286f32476adbf141bf6a2d0b3170b4e1c5a5

Contents?: true

Size: 705 Bytes

Versions: 2

Compression:

Stored size: 705 Bytes

Contents

require "forceful_foreign_key/version"
require "forceful_foreign_key/graph"

module ActiveRecord::ConnectionAdapters::SchemaStatements
  alias_method :old_add_foreign_key, :add_foreign_key

  def add_foreign_key(from_table, to_table, options = {})
    return unless supports_foreign_keys?
    if options[:force]
      graph = ForcefulForeignKey::Graph.new
      graph.build_graph
      graph.delete_orphans(from_table: from_table,
                           from_column: options[:column] || "#{to_table}_id",
                           target_table: to_table,
                           target_column: options[:primary_key] || 'id')
    end
    old_add_foreign_key from_table, to_table, options
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
forceful_foreign_key-0.1.1 lib/forceful_foreign_key.rb
forceful_foreign_key-0.1.0 lib/forceful_foreign_key.rb