Sha256: 60bac97844a6f827410620916cf74a0c217a0548cb0f18ee9c3204cb4708074a
Contents?: true
Size: 1.07 KB
Versions: 9
Compression:
Stored size: 1.07 KB
Contents
require "active_record" require "logger" namespace :cipherstash do desc "Re-saves a model to migrate it when its config is set to encrypted-duplicate." task :migrate, [:model_name, :batch_size] => "db:load_config" do |_task, args| model_name = args[:model_name] batch_size = args[:batch_size] || 1000 if model_name.nil? abort "Please provide a model name, eg. rake cipherstash:migrate[User]" end model = Object.const_get(model_name) # raises NameError on failure unless model < ActiveRecord::Base abort "Not an ActiveRecord model: #{model_name}" end logger = Logger.new(STDOUT) logger.info "Migrating #{model_name} in batches of #{batch_size}." primary_keys = Array(model.primary_key) model.find_each(:batch_size => batch_size) do |record| id_info = primary_keys.map { |key| record.public_send(key) }.join(", ") logger.info "Processing record with ID #{id_info} ..." record.update_columns( record.attributes.filter { |attr| !attr.starts_with?("__") } ) end logger.info "Done." end end
Version data entries
9 entries across 9 versions & 1 rubygems