class PgShrink::SubTableSanitizer

Public Instance Methods

propagate!(old_parent_data, new_parent_data) click to toggle source
# File lib/pg_shrink/sub_table_sanitizer.rb, line 11
def propagate!(old_parent_data, new_parent_data)
  return if (old_parent_data.empty? && new_parent_data.empty?)
  old_batch = old_parent_data.index_by {|record| record[@opts[:primary_key]]}
  new_batch = new_parent_data.index_by {|record| record[@opts[:primary_key]]}

  foreign_key = @opts[:foreign_key]
  finder_options = {foreign_key => old_batch.keys}

  # adding additional filters from where clause.
  # TODO: support where clauses using non hash syntax (string, symbol)
  finder_options.merge!(@opts[:where])

  parent_field = @opts[:local_field].to_sym
  child_field = @opts[:foreign_field].to_sym

  old_child_records = table.get_records(finder_options)
  table.sanitize_batch(old_child_records) do |record|
    parent_record = new_batch[record[foreign_key]]
    record[child_field] = parent_record[parent_field]
    record
  end
end
validate_opts!(opts) click to toggle source
# File lib/pg_shrink/sub_table_sanitizer.rb, line 4
def validate_opts!(opts)
  unless opts[:local_field] && opts[:foreign_field]
    raise "Error: #{name} must define :local_field and :foreign_field"
  end
  super(opts)
end