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