Module: DataShifter

Defined in:
lib/data_shifter/shift.rb,
lib/data_shifter.rb,
lib/data_shifter/errors.rb,
lib/data_shifter/railtie.rb,
lib/data_shifter/version.rb,
lib/data_shifter/spec_helper.rb,
lib/data_shifter/internal/env.rb,
lib/data_shifter/configuration.rb,
lib/data_shifter/internal/colors.rb,
lib/data_shifter/internal/output.rb,
lib/data_shifter/internal/progress_bar.rb,
lib/data_shifter/internal/record_utils.rb,
lib/data_shifter/internal/signal_handler.rb,
lib/data_shifter/internal/log_deduplicator.rb,
lib/data_shifter/internal/rake_setup_output.rb,
lib/data_shifter/internal/side_effect_guards.rb,
lib/data_shifter/internal/rake_exception_reporting.rb

Overview

Base class for data shifts. Dry-run by default, progress bars, transaction modes, consistent summaries.

Usage:

# lib/data_shifts/20260201120000_backfill_foo.rb
module DataShifts
  class BackfillFoo < DataShifter::Shift
    description "Backfill foo on bars"

    def collection
      Bar.where(foo: nil)
    end

    def process_record(bar)
      bar.update!(foo: computed_value(bar))
    end
  end
end

Running:

- `rake data:shift:backfill_foo` (dry run by default)
- `COMMIT=1 rake data:shift:backfill_foo` (apply changes)
- Or technically can call directly: `MyShift.call(dry_run: false)` (Axn semantics) - BUT:
  NOTES: default location not auto-loaded, and in general it is strongly recommended to use the rake task.

Transaction modes (set at class level with ‘transaction`):

- `transaction :single` (default): one transaction for the whole run (all-or-nothing).
- `transaction :per_record`: each record in its own transaction.
- `transaction false`: no automatic transaction in commit mode; in dry run we still wrap in a rollback transaction.

Dry run: DB changes are always rolled back (we wrap in a transaction and raise Rollback). Guard non-DB side effects with ‘return if dry_run?`.

Fixed list of IDs (fail fast): Use find_exactly!(Model, [id1, id2, …]) in ‘collection`. Large collections: Return an ActiveRecord::Relation and iteration uses `find_each`.

Defined Under Namespace

Modules: Internal, SpecHelper Classes: Configuration, ExternalRequestNotAllowedError, Railtie, Shift

Constant Summary collapse

VERSION =
"0.3.3"

Class Method Summary collapse

Class Method Details

.configObject

Returns the global configuration instance.



13
14
15
# File 'lib/data_shifter.rb', line 13

def config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields the configuration for block-style setup.

DataShifter.configure do |c|
  c.allow_external_requests = ["api.readonly.example.com"]
  c.suppress_repeated_logs = false
end

Yields:



23
24
25
# File 'lib/data_shifter.rb', line 23

def configure
  yield config
end