Class: Pcrd::Transform::Validator

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

Overview

Runs pre-migration data validation queries on the source database.

For each column in the migration spec that involves a validated cast, Validator queries the source table to confirm no rows would be rejected or silently truncated by the cast. Failures are collected and reported all at once so the operator can fix everything in one pass.

Called by the preflight phase before any replication slot is created.

Defined Under Namespace

Classes: ValidationFailure

Instance Method Summary collapse

Constructor Details

#initialize(source_pool) ⇒ Validator

Returns a new instance of Validator.



18
19
20
# File 'lib/pcrd/transform/validator.rb', line 18

def initialize(source_pool)
  @pool = source_pool
end

Instance Method Details

#validate(table_config, source_columns) ⇒ Object

Validates all columns in table_config against their source schema.

source_columns: Array<Schema::Column> from Schema::Reader Returns Array<ValidationFailure> — empty means all checks passed. Raises on unexpected database errors.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pcrd/transform/validator.rb', line 27

def validate(table_config, source_columns)
  failures = []
  col_index = source_columns.each_with_object({}) { |c, h| h[c.name] = c }

  (table_config.columns || {}).each do |src_name, col_spec|
    next if col_spec.drop || col_spec.type.nil?

    source_col = col_index[src_name.to_s]
    next unless source_col

    safety = TypeMap.cast_safety(source_col.type_name, col_spec.type)
    next if %i[no_op always_safe].include?(safety)

    if safety == :unsupported
      failures << ValidationFailure.new(
        table_name:    table_config.name,
        column_name:   src_name,
        source_type:   source_col.display_type,
        target_type:   col_spec.type,
        failing_count: nil,
        description:   "pcrd does not support this type transition — " \
                       "use a custom transform or perform it separately",
        warn_only:     false
      )
      next
    end

    # :validated — run the data check
    result = run_check(table_config.name, src_name, source_col, col_spec.type)
    failures << result if result
  end

  failures
end