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

Version Path
activerecord-cipherstash-pg-adapter-0.7.2 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.7.1 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.7.0 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.6.1 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.6.0 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.5.0 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.4.0 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.3.0 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.2.0 lib/active_record/connection_adapters/cipherstash_pg/cipherstash_tasks.rake