Sha256: 0c3fb65d03828a4a6aa342a017c30bbd44428b4824dd841e31518a7260b4c6f9

Contents?: true

Size: 1.41 KB

Versions: 22

Compression:

Stored size: 1.41 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|
    logger = Logger.new(STDOUT)

    config = ActiveRecord::Base.connection_db_config
    adapter = config.configuration_hash[:adapter]

    unless adapter == "postgres_cipherstash"
      logger.warn "This application is using the '#{adapter}' database adapter\n\n"

      abort "To use this rake task, please update the database adapter to 'postgres_cipherstash'\n\n\n\n"
    end

    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.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

22 entries across 22 versions & 1 rubygems

Version Path
activerecord-cipherstash-pg-adapter-0.8.5 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.8.4 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.8.3 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.8.2 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.8.1 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.8.0 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.7.19 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.7.18 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.7.17 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.7.16 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.7.15 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.7.14 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.7.12 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.7.11 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.7.10 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.7.9 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.7.8 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.7.7 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.7.6 lib/cipherstash_tasks.rake
activerecord-cipherstash-pg-adapter-0.7.5 lib/cipherstash_tasks.rake