Class: Pcrd::Transform::RowTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/pcrd/transform/row_transformer.rb

Overview

Applies a Config::Table migration spec to a single row hash.

Handles the structural changes: drops (exclude column), renames (change key), and pass-through for unchanged columns. Does NOT perform Ruby-level type conversion — PostgreSQL coerces values on INSERT/COPY, so string values from the source pass through as-is.

Added columns (from add_columns) are NOT included in the transformer output. They are omitted from the INSERT column list so the target database applies their DEFAULT expressions directly.

Usage:

transformer = RowTransformer.new(table_config, source_columns)
target_row  = transformer.transform(source_row_hash)
columns     = transformer.target_column_names

Instance Method Summary collapse

Constructor Details

#initialize(table_config, source_columns) ⇒ RowTransformer

Returns a new instance of RowTransformer.



23
24
25
26
27
28
# File 'lib/pcrd/transform/row_transformer.rb', line 23

def initialize(table_config, source_columns)
  @source_names = source_columns.map(&:name)
  @drops        = build_drop_set(table_config)
  @renames      = build_rename_map(table_config)
  @target_names = build_target_names
end

Instance Method Details

#source_column_names_keptObject

Ordered list of source column names that survive (not dropped).



45
46
47
# File 'lib/pcrd/transform/row_transformer.rb', line 45

def source_column_names_kept
  @source_names_kept
end

#target_column_namesObject

Ordered list of target column names produced by #transform. Pass this to the backfill engine when constructing INSERT/COPY statements.



40
41
42
# File 'lib/pcrd/transform/row_transformer.rb', line 40

def target_column_names
  @target_names
end

#transform(row_hash) ⇒ Object

Returns a hash of => value for all non-dropped columns. Values are whatever the pg gem returned (typically String or nil).



32
33
34
35
36
# File 'lib/pcrd/transform/row_transformer.rb', line 32

def transform(row_hash)
  @target_names.each_with_object({}).with_index do |(tgt_name, result), i|
    result[tgt_name] = row_hash[@source_names_kept[i]]
  end
end