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
10
# File 'lib/kredis/migration.rb', line 6

def initialize(config = :shared)
  @redis = Kredis.configured_for config
  # TODO: Replace script loading with `copy` command once Redis 6.2+ is the minimum supported version.
  @copy_sha = @redis.script "load", "redis.call('SETNX', KEYS[2], redis.call('GET', KEYS[1])); return 1;"
end

Instance Method Details

#delete_all(key_pattern) ⇒ Object



33
34
35
36
37
# File 'lib/kredis/migration.rb', line 33

def delete_all(key_pattern)
  each_key_batch_matching(key_pattern) do |keys|
    @redis.del *keys
  end
end

#migrate(from:, to:) ⇒ Object



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

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

  if to.present? && from != namespaced_to
    log_migration "Migrating key #{from} to #{namespaced_to}" do
      @redis.evalsha @copy_sha, keys: [ from, namespaced_to ]
    end
  else
    log_migration "Skipping blank/unaltered migration key #{from}#{to}"
  end
end

#migrate_all(key_pattern) ⇒ Object



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

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