Module: LegacyMigrations

Defined in:
lib/legacy_migrations/squirrel/squirrel.rb,
lib/legacy_migrations.rb,
lib/legacy_migrations/row_matchers.rb,
lib/legacy_migrations/status_report.rb,
lib/legacy_migrations/future_storage.rb,
lib/legacy_migrations/transformations.rb,
lib/legacy_migrations/source_iterators.rb,
lib/legacy_migrations/squirrel/paginator.rb

Overview

which is run to build the conditions and includes required to execute the query.

Defined Under Namespace

Modules: RowMatchers, SourceIterators, Squirrel, Transformations Classes: FutureStorage, Operation, StatusReport, ValidationError

Instance Method Summary collapse

Instance Method Details

#transfer_from(from_table, *args, &block) ⇒ Object

Define a source and destination table to transfer data

Options

  • :to - (Required) Class of the destination table

  • :limit - Set a limit to the number of rows to transfer. This is useful when you’re trying to find faulty data in the source table, and don’t want to run the entire dataset.

  • :validate - Default = true. Use ActiveRecord validations when saving the destination rows.

  • :source_type - Default = :active_record. Sets the source destination type. Options: :activerecord_: Assumes the From option is a class that inherits from ActiveRecord::Base, then iterates through each record of From table by using From.all.each… :other: Assumes the From option is an iterable ‘collection’ whose elements/items can respond to all source methods speficied in the given block.

  • :store_as - Stores the generated destination record as a key that is retrievable in other transformations. Note that for this to work, the source object must respond to the method id which returns a value that is unique for all rows within the table (usually this is just a primary key).

    Example <tt> transfer_from Mammal, :to => Species, :store_as => ‘new_animal’ do

    match_same_name_attributes
    

    end

    transfer_from Person, :to => Animal do

    stored 'new_animal', :to => :species
    

    end </tt>



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/legacy_migrations.rb', line 43

def transfer_from(from_table, *args, &block)

  configure_transfer(from_table, *args) { yield }
  @current_operation = StatusReport.instance.add_operation :source => @from_table,
                                      :destination => @to_table,
                                      :source_type => args.extract_options![:source_type] || :active_record,
                                      :type => 'transfer'


  source_iterator(@limit, @type).each do |from_record|
    new_destination_record(from_record)
  end
  @status_report
end

#update_from(from_table, *args, &block) ⇒ Object

Define a source and destination table with which to update data

This method accepts all of the same options as transfer_from.

In addition, you’ll need to use a series of columns that match data from the source to the destination. For example, if your source and destination data have a social security number, then you’d use the social security number to match records from the two rows. The following is how you would do that.

<tt> update_from SourceTable, :to => DestinationTable do

based_on do
  ssn == from.social_security_number
end

from :last_name, :to => :last_name

end </tt>

Note that when using the ‘based_on’ method, the left-hannd item always corresponds to a column method on the destination table.

The methods available in the based_on block correspond to the well-known squirrel plugin’s syntax. Here’s a quick review of the possible operators:

Handles comparisons in the query. This class is analagous to the columns in the database. When comparing the Condition to a value, the operators are used as follows:

  • , === : Straight-up Equals. Can also be used as the “IN” operator if the operand is an Array.

Additionally, when the oprand is nil, the comparison is correctly generates as “IS NULL”.“

  • ~ : The LIKE and REGEXP operators. If the operand is a String, it will generate a LIKE

comparison. If it is a Regexp, the REGEXP operator will be used. NOTE: MySQL regular expressions are NOT the same as Ruby regular expressions. Also NOTE: No wildcards are inserted into the LIKE comparison, so you may add them where you wish.

  • <=> : Performs a BETWEEN comparison, as long as the operand responds to both #first and #last,

which both Ranges and Arrays do.

  • > : A simple greater-than comparison.

  • >= : Greater-than or equal-to.

  • < : A simple less-than comparison.

  • <= : Less-than or equal-to.

  • contains? : Like =~, except automatically surrounds the operand in %s, which =~ does not do.

  • nil? : Works exactly like “column == nil”, but in a nicer syntax, which is what Squirrel is all about.

Options



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/legacy_migrations.rb', line 104

def update_from(from_table, *args, &block)

  configure_transfer(from_table, *args) { yield }

  @current_operation = StatusReport.instance.add_operation :source => @from_table,
                                      :destination => @to_table,
                                      :source_type => args.extract_options![:source_type] || :active_record,
                                      :type => 'update'
  source_iterator(@limit, @type).each do |from_record|
    matching_records = @conditions.call(from_record)

    unless matching_records.empty?
      matching_records.each do |to_record|
        @columns.each do |to, from|
          to_record[to]= from.call(from_record)
        end

        if @options[:validate]
          report_validation_errors(to_record, from_record, 'update')
        else
          to_record.save(false)
          @current_operation.record_update(to_record)
        end
        store_as(to_record, from_record)
      end
    else
      new_destination_record(from_record)
    end
  end
  @status_report
end