Sha256: 4443c0e0c5cc327ae1d0121132573a7579ee2a25e2aa2b34257c0da79b9d6131

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

begin
  require "batch"
rescue LoadError => e
  e.message << "\nTry `gem install batch`."
end

module Ohm
  module Utils
    class Upgrade
      def redis
        Ohm.redis
      end

      attr :models
      attr :types

      def initialize(models)
        @models = models
        @types = Hash.new { |hash, model| hash[model] = {} }
      end

      def run
        models.each do |model|
          ns = Ohm::Key[model]

          puts "Upgrading #{model}..."

          Batch.each(redis.smembers(ns[:all])) do |id|
            instance = ns[id]

            attrs = []
            deletes = []

            redis.keys(instance["*"]).each do |key|
              field = key[instance.size.succ..-1]

              type = (types[model][field] ||= redis.type(key).to_sym)

              if type == :string
                attrs << field
                attrs << redis.get(key)
                deletes << key
              end
            end

            redis.hmset(instance, *attrs)
            redis.del(*deletes)
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ohm-0.1.0.rc4 lib/ohm/utils/upgrade.rb
ohm-0.1.0.rc2 lib/ohm/utils/upgrade.rb
ohm-0.1.0.rc1 lib/ohm/utils/upgrade.rb