Class: PruneAr::ForeignKeyHandler
- Inherits:
-
Object
- Object
- PruneAr::ForeignKeyHandler
- Defined in:
- lib/prune_ar/foreign_key_handler.rb
Overview
Core of this gem. Prunes records based on parameters given.
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#foreign_key_supported ⇒ Object
readonly
Returns the value of attribute foreign_key_supported.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#original_foreign_keys ⇒ Object
readonly
Returns the value of attribute original_foreign_keys.
Instance Method Summary collapse
- #create(foreign_keys) ⇒ Object
- #create_from_belongs_to_associations(associations) ⇒ Object
- #drop(foreign_keys) ⇒ Object
-
#initialize(models:, connection: ActiveRecord::Base.connection, logger: Logger.new(STDOUT).tap { |l| l.level = Logger::WARN }) ⇒ ForeignKeyHandler
constructor
A new instance of ForeignKeyHandler.
Constructor Details
#initialize(models:, connection: ActiveRecord::Base.connection, logger: Logger.new(STDOUT).tap { |l| l.level = Logger::WARN }) ⇒ ForeignKeyHandler
Returns a new instance of ForeignKeyHandler.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/prune_ar/foreign_key_handler.rb', line 11 def initialize( models:, connection: ActiveRecord::Base.connection, logger: Logger.new(STDOUT).tap { |l| l.level = Logger::WARN } ) @connection = connection @logger = logger @foreign_key_supported = connection.supports_foreign_keys? @original_foreign_keys = snapshot_foreign_keys(models) end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
9 10 11 |
# File 'lib/prune_ar/foreign_key_handler.rb', line 9 def connection @connection end |
#foreign_key_supported ⇒ Object (readonly)
Returns the value of attribute foreign_key_supported.
9 10 11 |
# File 'lib/prune_ar/foreign_key_handler.rb', line 9 def foreign_key_supported @foreign_key_supported end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
9 10 11 |
# File 'lib/prune_ar/foreign_key_handler.rb', line 9 def logger @logger end |
#original_foreign_keys ⇒ Object (readonly)
Returns the value of attribute original_foreign_keys.
9 10 11 |
# File 'lib/prune_ar/foreign_key_handler.rb', line 9 def original_foreign_keys @original_foreign_keys end |
Instance Method Details
#create(foreign_keys) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/prune_ar/foreign_key_handler.rb', line 31 def create(foreign_keys) return unless foreign_key_supported foreign_keys.each do |fk| logger.debug("creating #{fk.name} on #{fk.from_table} (#{fk.column})") connection.add_foreign_key(fk.from_table, fk.to_table, fk.) end end |
#create_from_belongs_to_associations(associations) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/prune_ar/foreign_key_handler.rb', line 40 def create_from_belongs_to_associations(associations) return [] unless foreign_key_supported associations.map do |assoc| constraint_name = generate_belongs_to_foreign_key_name(assoc) create_from_belongs_to_association(constraint_name, assoc) end end |
#drop(foreign_keys) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/prune_ar/foreign_key_handler.rb', line 22 def drop(foreign_keys) return unless foreign_key_supported foreign_keys.each do |fk| logger.debug("dropping #{fk.name} from #{fk.from_table} (#{fk.column})") connection.remove_foreign_key(fk.from_table, name: fk.name) end end |