Class: Kredis::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/kredis/migration.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = :shared) ⇒ Migration

Returns a new instance of Migration.



6
7
8
9
# File 'lib/kredis/migration.rb', line 6

def initialize(config = :shared)
  @redis = Kredis.configured_for config
  @copy_sha = @redis.script "load", "redis.call('SETNX', KEYS[2], redis.call('GET', KEYS[1])); return 1;"
end

Instance Method Details

#migrate(from:, to:) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/kredis/migration.rb', line 22

def migrate(from:, to:)
  to = Kredis.namespaced_key(to)

  if from != to
    log_migration "Migrating key #{from} to #{to}"
    @redis.evalsha @copy_sha, keys: [ from, to ]
  else
    log_migration "Skipping unaltered migration key #{from}"
  end
end

#migrate_all(key_pattern) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/kredis/migration.rb', line 11

def migrate_all(key_pattern)
  find_keys_in_batches_matching(key_pattern) do |keys|
    @redis.multi do
      keys.each do |key|
        ids = key.scan(/\d+/).map(&:to_i)
        migrate from: key, to: yield(key, *ids)
      end
    end
  end
end