Transfer Gem
Transfer data from source database to ActiveRecord, SequelModel or Mongoid models.
Installation
Include Transfer in Gemfile:
gem 'transfer'
You can run bundle from command line:
bundle install
Compatibility
Source database: all, supported by Sequel.
Destination: ActiveRecord, SequelModel, Mongoid.
Configure
Set connection options of source database and global parameters:
Transfer.configure do |c|
c.host = "localhost"
c.adapter = "postgres"
c.database = "source_database"
c.user = "username"
c.password = "password"
end
Available options:
validateon/off model validations. Values:trueorfalse, default isfalse.failure_strategysets strategy if save of model is not successfully. Values::ignoreor:rollback, defult is:ignore.beforeglobal callback.successglobal callback.failureglobal callback.afterglobal callback.- another options interpreted as Sequel database connection options.
Usage
Direct transfer from source table :users to User model. All columns, including protected, existing in source table and destination model, will transferred:
transfer :users => User
Filling the field country a constant value:
transfer :users => User do
country "England"
end
Transfer :name column from source table :users into first_name of User model:
transfer :users => User do
first_name :name
end
To produce, dynamic value (e.g. dist_name field), you can pass a block and access the row of source table:
transfer :users => User do
dist_name {|row| "Mr. #{row[:first_name]}"}
end
Global callbacks
This callbacks called for each transfer.
Transfer.configure do |c|
c.before do |klass, dataset|
#...
end
c.success do |model, row|
#...
end
c.failure do |model, row, exception|
#...
end
c.after do |klass, dataset|
#...
end
end
Available global callbacks:
beforecalled before an transfer started. Parameters:klass,dataset.successcalled if save model is successfully. Parameters:model,row.failurecalled if save model is not successfully. Parameters:row,exception.aftercalled after an transfer finished. Parameters:klass,dataset.
Description of parameters:
datasetsource table dataset, instance of Sequel::Dataset.klassis destination class.modelbuilded model, instance ofklass.rowof source table. Type:Hash.exceptionif save of model is not successfull.
Local transfer callbacks
You can specify callbacks in your transfer that are separate from the model callbacks. This callbacks called in model context, therefore self keyword points to model.
transfer :users => User do
before_save do |row|
self. << Message.build(:title => "Transfer", :description => "Welcome to new site, #{row[:fname]}!")
end
end
Available callbacks:
before_savecalled before save model. Paramaters:row.after_savecalled after successfully save model. Parameters:row.
where row is row of source table, type: Hash.
Filter columns
only filter passes source columns, specified in parameters.
transfer :users => User do
only :name
end
except filter passes all source columns, except for those that are specified in the parameters:
transfer :users => User do
except :name
end
Replace global options
Global options can be replaced global options, if it passed to transfer.
transfer :users => User, :validate => false, :failure_strategy => :rollback
Available options for replace:
validatefailure_strategy
Logging
If you also want see progress of transfer in console, use e.g. progressbar gem with global callbacks.
require 'progressbar'
Transfer.configure do |c|
c.before {|klass, dataset| = ProgressBar.new(klass, dataset.count) }
c.success { .inc }
c.after { .halt }
end
transfer :users => User