Class: PgShrink::SubTableSanitizer

Inherits:
SubTableOperator show all
Defined in:
lib/pg_shrink/sub_table_sanitizer.rb

Instance Attribute Summary

Attributes inherited from SubTableOperator

#database, #parent, #table_name

Instance Method Summary collapse

Methods inherited from SubTableOperator

#default_opts, #initialize, #name, #table

Constructor Details

This class inherits a constructor from PgShrink::SubTableOperator

Instance Method Details

#propagate!(old_parent_data, new_parent_data) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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) ⇒ Object



4
5
6
7
8
9
# 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