diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..1017f1c --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,71 @@ +name: Ruby + +on: + push: + branches: + - main + + pull_request: + + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + name: Ruby ${{ matrix.ruby }} + strategy: + matrix: + include: + - ruby: '3.3' + bundler: latest + rubygems: latest + + - ruby: '3.2' + bundler: latest + rubygems: latest + + - ruby: '3.1' + bundler: latest + rubygems: latest + + - ruby: '3.0' + bundler: latest + rubygems: latest + + - ruby: '2.7' + bundler: '2.4.22' + rubygems: '3.2.3' + + services: + redis: + image: redis:bookworm@sha256:e422889e156ebea83856b6ff973bfe0c86bce867d80def228044eeecf925592b + # Set health checks to wait until redis has started + options: >- + --health-cmd "redis-cli ping" + --health-interval 10s + --health-timeout 3s + --health-retries 5 + ports: + # https://docs.github.com/en/actions/using-containerized-services/creating-redis-service-containers#running-jobs-in-containers + # Maps port 6379 on service container to the host + - 6379:6379 + + steps: + - uses: actions/checkout@v4 + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + rubygems: ${{ matrix.rubygems }} + bundler: ${{ matrix.bundler }} + # When the following is true, also run "bundle install", + # and cache the result automatically. Ran into an issue + # with the caching and multiple ruby versions. Needs + # further investigation. + bundler-cache: false + + - name: Re-run bundle install + run: bundle install + + - name: Run the tryouts + run: bundle exec try -v try/*_try.rb diff --git a/.gitignore b/.gitignore index 29ca08b..959ad8f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,18 @@ .DS_Store -pkg +.bundle +.byebug* +.history +.devcontainer +.vscode +*.env +*.log +*.md +*.txt +!LICENSE.txt +.ruby-version +appendonlydir +etc/config +log +tmp +vendor +*.gem diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..b7dd6a1 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,69 @@ +## +# Pre-Commit Configuration +# +# Initial setup: +# +# 0. Install the pre-commit framework (if it isn't already on your system): + +# $ pip install pre-commit +# +# 1. Install the git hook: +# +# $ pre-commit install +# +# +# Other commands: +# +# Run it on all the files in this repo: +# $ pre-commit run --all-files +# +# Updating plugin repositories: +# $ pre-commit autoupdate +# +# Automatically enable pre-commit on repositories +# $ git config --global init.templateDir ~/.git-template +# $ pre-commit init-templatedir ~/.git-template +# +# See also: +# - https://pre-commit.com for more information +# - https://pre-commit.com/hooks.html for more hooks +# + +default_install_hook_types: + - pre-commit + - prepare-commit-msg + +fail_fast: true + +repos: + - repo: meta + hooks: + - id: check-hooks-apply + - id: check-useless-excludes + - id: identity + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.6.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: detect-private-key + - id: mixed-line-ending + - id: check-added-large-files + args: ["--maxkb=1000"] + - id: no-commit-to-branch + args: ["--branch", "develop", "--branch", "rel/.*"] + - id: check-merge-conflict + - id: forbid-submodules + + - repo: https://github.com/avilaton/add-msg-issue-prefix-hook + rev: v0.0.11 + hooks: + - id: add-msg-issue-prefix + stages: [prepare-commit-msg] + name: Link commit to Github issue + args: + - "--default=[NOJIRA]" + - "--pattern=[a-zA-Z0-9]{0,10}-?[0-9]{1,5}" + - "--template=[#{}]" diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..3620847 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,13 @@ +AllCops: + TargetRubyVersion: 2.7.5 + +Style/StringLiterals: + Enabled: true + EnforcedStyle: double_quotes + +Style/StringLiteralsInInterpolation: + Enabled: true + EnforcedStyle: double_quotes + +Layout/LineLength: + Max: 120 diff --git a/CHANGES.txt b/CHANGES.txt index 6fc6f5a..9aac9a1 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,5 +1,14 @@ REDIS-DUMP, CHANGES +#### 0.6.0 (2024-06-17) ############################### + + +#### 0.5.0 (2023-01-17) ############################### + +* ADDED: Timeout support for redis connections +* CHANGED: uri-redis dependency to 1.0.0 + + #### 0.4.0 (2017-11-23) ############################### * ADDED: support for redis >= 4.0 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..26302ec --- /dev/null +++ b/Gemfile @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +gem "redis", ">= 4.0", "< 5.0" +gem "uri-redis", ">= 1.3.0" +gem "yajl-ruby", ">= 1.4.3" +gem "oj", ">= 3.16.4" +gem "drydock", ">= 0.6.9" + +gem "rake", "~> 13.0", require: false, group: :development +gem "rubocop", "~> 1.64.1", require: false, group: :development +gem "tryouts", "~> 2.2.0", require: false, group: :development +gem "pry-byebug", "~> 3.10.1", require: false, group: :development diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..a434995 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,72 @@ +GEM + remote: https://rubygems.org/ + specs: + ast (2.4.2) + bigdecimal (3.1.8) + byebug (11.1.3) + coderay (1.1.3) + drydock (0.6.9) + json (2.7.2) + language_server-protocol (3.17.0.3) + method_source (1.1.0) + oj (3.16.4) + bigdecimal (>= 3.0) + parallel (1.25.1) + parser (3.3.3.0) + ast (~> 2.4.1) + racc + pry (0.14.2) + coderay (~> 1.1) + method_source (~> 1.0) + pry-byebug (3.10.1) + byebug (~> 11.0) + pry (>= 0.13, < 0.15) + racc (1.8.0) + rainbow (3.1.1) + rake (13.2.1) + redis (4.8.1) + regexp_parser (2.9.2) + rexml (3.3.0) + strscan + rubocop (1.64.1) + json (~> 2.3) + language_server-protocol (>= 3.17.0) + parallel (~> 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.31.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.31.3) + parser (>= 3.3.1.0) + ruby-progressbar (1.13.0) + storable (0.10.0) + strscan (3.1.0) + sysinfo (0.10.0) + drydock (< 1.0) + storable (~> 0.10) + tryouts (2.2.0) + sysinfo (~> 0.10) + unicode-display_width (2.5.0) + uri-redis (1.3.0) + yajl-ruby (1.4.3) + +PLATFORMS + arm64-darwin-22 + ruby + +DEPENDENCIES + drydock (>= 0.6.9) + oj (>= 3.16.4) + pry-byebug (~> 3.10.1) + rake (~> 13.0) + redis (>= 4.0, < 5.0) + rubocop (~> 1.64.1) + tryouts (~> 2.2.0) + uri-redis (>= 1.3.0) + yajl-ruby (>= 1.4.3) + +BUNDLED WITH + 2.5.9 diff --git a/LICENSE.txt b/LICENSE.txt index bc42d13..ab6eb5d 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,4 +1,6 @@ -Copyright (c) 2010-2017 Solutious Inc, Delano Mandelbaum +MIT License + +Copyright (c) 2010-2024 Delano Mandelbaum Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -7,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..3e05678 --- /dev/null +++ b/README.md @@ -0,0 +1,81 @@ +# Redis::Dump - v0.5 PRE + +*Backup and restore your Redis data to and from JSON.* + +## Installation + +One of: +1. Gemfile: `gem 'redis-dump', '~> 0.4.0'` +2. Install manually: `gem install redis-dump` +3. Clone with git: `git clone git@github.com:delano/redis-dump.git` + + + +## Usage + +There are two executables: `redis-dump` and `redis-load`. + +```bash + $ redis-dump + $ redis-dump -u 127.0.0.1:6379 > db_full.json + $ redis-dump -u 127.0.0.1:6379 -d 15 > db_db15.json + + $ < db_full.json redis-load + $ < db_db15.json redis-load -d 15 + # OR + $ cat db_full | redis-load + $ cat db_db15.json | redis-load -d 15 + + # You can specify the redis URI via an environment variable + $ export REDIS_URI=127.0.0.1:6379 + $ redis-dump + + # If your instance uses a password (such as on RedisToGo), you + # can specify the Redis URL as such: + # :@: + # Note the leading colon is important for specifying no username. + $ redis-dump -u :234288a830f009980e08@example.redistogo.com:9055 +``` + +### Output format + +All redis datatypes are output to a simple JSON object. All objects have the following 5 fields: + +* db (Integer) +* key (String) +* ttl (Integer): The amount of time in seconds that the key will live. If no expire is set, it's -1. +* type (String), one of: string, list, set, zset, hash, none. +* value (String): A JSON-encoded string. For keys of type list, set, zset, and hash, the data is given a specific structure (see below). + +Here are examples of each datatype: + +```json +{"db":0,"key":"hashkey","ttl":-1,"type":"hash","value":{"field_a":"value_a","field_b":"value_b","field_c":"value_c"},"size":42} +{"db":0,"key":"listkey","ttl":-1,"type":"list","value":["value_0","value_1","value_2","value_0","value_1","value_2"],"size":42} +{"db":0,"key":"setkey","ttl":-1,"type":"set","value":["value_2","value_0","value_1","value_3"],"size":28} +{"db":0,"key":"zsetkey","ttl":-1,"type":"zset","value":[["value_0","100"],["value_1","100"],["value_2","200"],["value_3","300"],["value_4","400"]],"size":50} +{"db":0,"key":"stringkey","ttl":79,"type":"string","value":"stringvalue","size":11} +``` + +## Important notes + +### About TTLs + +One of the purposes of redis-dump is the ability to restore the database to a known state. When you restore a redis database from a redis-dump file, *the expires are reset to their values at the time the dump was created*. This is different from restoring from Redis' native .rdb or .aof files (expires are stored relative to the actual time they were set). + +### Output directly to an encrypted file + +For most sensitive data, you should consider encrypting the data directly without writing first to a temp file. You can do this using the power of [gpg](http://www.gnupg.org/) and file descriptors. Here are a couple examples: + +```bash + # Encrypt the data (interactive) + $ redis-dump -u 127.0.0.1:6379 -d 15 | gpg --force-mdc -v -c > path/2/backup-db1 +``` + +## Contributing + +Bug reports and pull requests are welcome on GitHub at https://github.com/delano/redis-dump. + +## License + +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). diff --git a/README.rdoc b/README.rdoc deleted file mode 100644 index 40ae335..0000000 --- a/README.rdoc +++ /dev/null @@ -1,107 +0,0 @@ -= Redis-Dump v0.4 BETA - -Backup and restore your Redis data to and from JSON. - -NOTE: This is beta software. TEST IT BEFORE RELYING ON IT. - - -== Usage - -There are two executables: redis-dump and redis-load. - - $ redis-dump - $ redis-dump -u 127.0.0.1:6371 > db_full.json - $ redis-dump -u 127.0.0.1:6371 -d 15 > db_db15.json - - $ < db_full.json redis-load - $ < db_db15.json redis-load -d 15 - # OR - $ cat db_full | redis-load - $ cat db_db15.json | redis-load -d 15 - - # You can specify the redis URI via an environment variable - $ export REDIS_URI=127.0.0.1:6371 - $ redis-dump - - # If your instance uses a password (such as on RedisToGo), you - # can specify the Redis URL as such: - # :@: - # Note the leading colon is important for specifying no username. - $ redis-dump -u :234288a830f009980e08@example.redistogo.com:9055 - -== Output format - -All redis datatypes are output to a simple JSON object. All objects have the following 5 fields: - -* db (Integer) -* key (String) -* ttl (Integer): The amount of time in seconds that the key will live. If no expire is set, it's -1. -* type (String), one of: string, list, set, zset, hash, none. -* value (String): A JSON-encoded string. For keys of type list, set, zset, and hash, the data is given a specific structure (see below). - -Here are examples of each datatype: - - {"db":0,"key":"hashkey","ttl":-1,"type":"hash","value":{"field_a":"value_a","field_b":"value_b","field_c":"value_c"},"size":42} - {"db":0,"key":"listkey","ttl":-1,"type":"list","value":["value_0","value_1","value_2","value_0","value_1","value_2"],"size":42} - {"db":0,"key":"setkey","ttl":-1,"type":"set","value":["value_2","value_0","value_1","value_3"],"size":28} - {"db":0,"key":"zsetkey","ttl":-1,"type":"zset","value":[["value_0","100"],["value_1","100"],["value_2","200"],["value_3","300"],["value_4","400"]],"size":50} - {"db":0,"key":"stringkey","ttl":79,"type":"string","value":"stringvalue","size":11} - -=== Important note about TTLs - -One of the purposes of redis-dump is the ability to restore the database to a known state. When you restore a redis database from a redis-dump file, the expires are reset to their values at the time the dump was created. This is different from restoring from Redis' native .rdb or .aof files (expires are stored relative to the actual time they were set). - - -== Output directly to an encrypted file - -For most sensitive data, you should consider encrypting the data directly without writing first to a temp file. You can do this using the power of [gpg](http://www.gnupg.org/) and file descriptors. Here are a couple examples: - - # Encrypt the data (interactive) - $ redis-dump -u 127.0.0.1:6371 -d 15 | gpg --force-mdc -v -c > path/2/backup-db15.json.gpg - - # Encrypt the data (automated) - $ redis-dump -u 127.0.0.1:6371 -d 15 | 3 path/2/backup-db15.json.gpg - - # Decrypt the file (interactive) - $ gpg path/2/backup-db15.json.gpg - Enter passphrase: ******* - - # Decrypt the file (automated) - $ 3 ["build"] -CLEAN.include [ 'pkg', 'rdoc' ] -name = "redis-dump" - -$:.unshift File.join(File.dirname(__FILE__), 'lib') -require "redis/dump" -version = Redis::Dump::VERSION.to_s - -begin - require "jeweler" - Jeweler::Tasks.new do |s| - s.version = version - s.name = name - s.summary = "Backup and restore your Redis data to and from JSON." - s.description = s.summary - s.email = "delano@solutious.com" - s.homepage = "http://github.com/delano/redis-dump" - s.authors = ["Delano Mandelbaum"] - - s.add_dependency("yajl-ruby", ">= 0.1") - s.add_dependency("redis", ">= 4.0") - s.add_dependency("uri-redis", ">= 0.4.0") - s.add_dependency("drydock", ">= 0.6.9") - - s.license = "MIT" - - s.signing_key = File.join('/mnt/gem/', 'gem-private_key.pem') - s.cert_chain = ['gem-public_cert.pem'] - end - Jeweler::GemcutterTasks.new -rescue LoadError - puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler" -end - -require 'rake/testtask' -Rake::TestTask.new do |t| - t.libs = ["lib", "test"] -end - -extra_files = %w[LICENSE.txt THANKS.txt CHANGES.txt ] -RDoc::Task.new do |rdoc| - rdoc.rdoc_dir = "rdoc" - rdoc.title = "#{name} #{version}" - rdoc.generator = 'hanna' # gem install hanna-nouveau - rdoc.main = 'README.rdoc' - rdoc.rdoc_files.include("README*") - rdoc.rdoc_files.include("bin/*.rb") - rdoc.rdoc_files.include("lib/**/*.rb") - extra_files.each { |file| - rdoc.rdoc_files.include(file) if File.exists?(file) - } -end +require "bundler/gem_tasks" +require "rubocop/rake_task" +RuboCop::RakeTask.new +task default: :rubocop diff --git a/VERSION b/VERSION deleted file mode 100644 index 1d0ba9e..0000000 --- a/VERSION +++ /dev/null @@ -1 +0,0 @@ -0.4.0 diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..e196a33 --- /dev/null +++ b/bin/console @@ -0,0 +1,15 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require "bundler/setup" +require "redis/dump" + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +# (If you use this, don't forget to add pry to your Gemfile!) +# require "pry" +# Pry.start + +require "irb" +IRB.start(__FILE__) diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000..dce67d8 --- /dev/null +++ b/bin/setup @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +bundle install + +# Do any other automated setup that you need to do here diff --git a/bin/redis-dump b/exe/redis-dump similarity index 100% rename from bin/redis-dump rename to exe/redis-dump diff --git a/bin/redis-load b/exe/redis-load similarity index 100% rename from bin/redis-load rename to exe/redis-load diff --git a/bin/redis-report b/exe/redis-report similarity index 100% rename from bin/redis-report rename to exe/redis-report diff --git a/lib/redis/dump.rb b/lib/redis/dump.rb index 0f1d111..d1c3b8f 100644 --- a/lib/redis/dump.rb +++ b/lib/redis/dump.rb @@ -1,12 +1,17 @@ + + unless defined?(RD_HOME) RD_HOME = File.expand_path(File.join(File.dirname(__FILE__), '..', '..') ) end require 'redis' -require 'uri/redis' require 'yajl' require 'base64' +require 'uri/redis' + +require_relative "dump/version" + class Redis class Dump unless defined?(Redis::Dump::VALID_TYPES) @@ -72,9 +77,10 @@ def dump filter=nil entries = [] each_database do |redis| chunk_entries = [] + Redis::Dump.ld "[db#{redis.connection[:db]}] Memory before: #{Redis::Dump.memory_usage}kb" dump_keys = redis.keys(filter) dump_keys_size = dump_keys.size - Redis::Dump.ld "Memory after loading keys: #{Redis::Dump.memory_usage}kb" + Redis::Dump.ld "[db#{redis.connection[:db]}] Dumping #{dump_keys_size} keys: #{dump_keys.join(', ')}" dump_keys.each_with_index do |key,idx| entry, idxplus = key, idx+1 if block_given? @@ -104,6 +110,8 @@ def dump filter=nil entries << self.class.encoder.encode(Redis::Dump.dump(redis, entry)) end end + + Redis::Dump.ld "[db#{redis.connection[:db]}] Memory after: #{Redis::Dump.memory_usage}kb" end entries end @@ -158,7 +166,9 @@ def load(string_or_stream, &each_record) end begin val, type = obj['value'], obj['type'] + Redis::Dump.ld " > load `#{val}`" if Redis::Dump.with_base64 && type === 'string' + Redis::Dump.ld " > load+decode64 for `#{val}`" val = Base64.decode64 val end ret = Redis::Dump.set_value this_redis, obj['key'], type, val, obj['ttl'] @@ -234,7 +244,7 @@ def set_value_list(this_redis, key, list) list.each { |value| this_redis.rpush key, value } end def set_value_set(this_redis, key, set) - set.each { |value| this_redis.sadd key, value } + set.each { |value| this_redis.sadd? key, value } end def set_value_zset(this_redis, key, zset) zset.each { |pair| this_redis.zadd key, pair[1].to_f, pair[0] } @@ -261,24 +271,6 @@ def stringify_none (this_redis, key, v=nil) (v || '') end extend Redis::Dump::ClassMethods - module VERSION - @path = File.join(RD_HOME, 'VERSION') - class << self - attr_reader :version, :path - def version - @version || read_version - end - def read_version - return if @version - @version = File.read(path).strip! - end - def prerelease?() false end - def to_a() version.split('.') end - def to_s() version end - def inspect() version end - end - end - class Problem < RuntimeError def initialize(*args) @args = args.flatten.compact diff --git a/lib/redis/dump/version.rb b/lib/redis/dump/version.rb new file mode 100644 index 0000000..fed984a --- /dev/null +++ b/lib/redis/dump/version.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class Redis + class Dump + VERSION = "0.5.0-pre" + end +end diff --git a/redis-dump.gemspec b/redis-dump.gemspec index e69de29..85cd662 100644 --- a/redis-dump.gemspec +++ b/redis-dump.gemspec @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require_relative "lib/redis/dump/version" + +Gem::Specification.new do |spec| + spec.name = "redis-dump" + spec.version = Redis::Dump::VERSION + spec.authors = ["delano"] + spec.email = "gems@solutious.com" + + spec.summary = "Backup and restore your Redis data to and from JSON." + spec.description = "Backup and restore your Redis data to and from JSON by database, key, or key pattern." + spec.homepage = "https://rubygems.org/gems/redis-dump" + spec.license = "MIT" + spec.required_ruby_version = Gem::Requirement.new(">= 2.7.8") + + spec.files = `git ls-files -z`.split("\x0").reject do |f| + f.match(%r{\A(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor}) + end + spec.bindir = "bin" + spec.executables = spec.files.grep(%r{\Abin/}) { |f| File.basename(f) } + spec.require_paths = ["lib"] +end diff --git a/sig/redis/dump.rbs b/sig/redis/dump.rbs new file mode 100644 index 0000000..7b31fa5 --- /dev/null +++ b/sig/redis/dump.rbs @@ -0,0 +1,6 @@ +module Redis + module Dump + VERSION: String + # See the writing guide of rbs: https://github.com/ruby/rbs#guides + end +end diff --git a/try/10_redis_dump_try.rb b/try/10_redis_dump_try.rb index 930ce38..b96f3be 100644 --- a/try/10_redis_dump_try.rb +++ b/try/10_redis_dump_try.rb @@ -1,9 +1,11 @@ -require 'redis/dump' +# frozen_string_literal: true + +require_relative '../lib/redis/dump' # The test instance of redis must be running: # $ redis-server try/redis.conf -@uri_base = "redis://127.0.0.1:6371" +@uri_base = "redis://127.0.0.1:6379" Redis::Dump.debug = false Redis::Dump.safe = true @@ -21,7 +23,7 @@ @rdump.redis(0).hset 'hashkey', 'field_b', 'value_b' @rdump.redis(0).hset 'hashkey', 'field_c', 'value_c' 3.times { |idx| @rdump.redis(0).rpush 'listkey', "value_#{idx}" } -4.times { |idx| @rdump.redis(0).sadd 'setkey', "value_#{idx}" } +4.times { |idx| @rdump.redis(0).sadd? 'setkey', "value_#{idx}" } 5.times { |idx| @rdump.redis(0).zadd 'zsetkey', idx.zero? ? 100 : 100*idx, "value_#{idx}" } @rdump.redis(0).keys.size #=> 5 @@ -31,12 +33,6 @@ @values.size #=> 5 -# Clear DB 0 -db0 = Redis::Dump.new 0, @uri_base -db0.redis(0).flushdb -db0.redis(0).keys.size -#=> 0 - ## Can load data @rdump.load @values.join @rdump.redis(0).keys.size @@ -56,7 +52,14 @@ @rdump.load @values.join #=> 5 -Redis::Dump.safe = true +## Clear DB 0 db0 = Redis::Dump.new 0, @uri_base db0.redis(0).flushdb +db0.redis(0).keys.size +#=> 0 +## Clear DB 0 in safe mode +Redis::Dump.safe = true +db0 = Redis::Dump.new 0, @uri_base +db0.redis(0).flushdb +#=> "OK" diff --git a/try/20_dump_specific_keys_try.rb b/try/20_dump_specific_keys_try.rb index e27d520..d57f9c0 100644 --- a/try/20_dump_specific_keys_try.rb +++ b/try/20_dump_specific_keys_try.rb @@ -1,9 +1,11 @@ -require 'redis/dump' +# frozen_string_literal: true + +require_relative '../lib/redis/dump' # The test instance of redis must be running: # $ redis-server try/redis.conf -@uri_base = "redis://127.0.0.1:6371" +@uri_base = "redis://127.0.0.1:6379" Redis::Dump.debug = false Redis::Dump.safe = true @@ -31,13 +33,6 @@ @rdump.dump('*gloria*').sort #=> ['{"db":0,"key":"gloria:0","ttl":-1,"type":"string","value":"gloria_value[0]","size":15}','{"db":0,"key":"gloria:1","ttl":-1,"type":"string","value":"gloria_value[1]","size":15}','{"db":0,"key":"gloria:2","ttl":-1,"type":"string","value":"gloria_value[2]","size":15}'] - -## Dump none existent keys (NOT SUPPORTED AS OF 0.3.0) -unknown_keys = [URI.parse("#{@uri_base}/0/unknownkey1"), URI.parse("#{@uri_base}/1/unknownkey2")] -@rdump.dump(unknown_keys).sort -##=> ['{"db":0,"key":"unknownkey1","ttl":-1,"type":"none","value":"","size":0}', '{"db":1,"key":"unknownkey2","ttl":-1,"type":"none","value":"","size":0}'] - - Redis::Dump.safe = true db0 = Redis::Dump.new 0..1, @uri_base db0.redis(0).flushdb diff --git a/try/30_dump_base64_try.rb b/try/30_dump_base64_try.rb index 79f7e72..6f84690 100644 --- a/try/30_dump_base64_try.rb +++ b/try/30_dump_base64_try.rb @@ -1,12 +1,14 @@ -require 'redis/dump' -require 'pry' +# frozen_string_literal: true + +require_relative '../lib/redis/dump' +# require 'pry-byebug' # The test instance of redis must be running: # $ redis-server try/redis.conf -@uri_base = "redis://127.0.0.1:6371" +@uri_base = "redis://127.0.0.1:6379" -Redis::Dump.debug = false +Redis::Dump.debug = true # leave enabled for CI workflow Redis::Dump.safe = true Redis::Dump.with_base64 = true @@ -21,38 +23,30 @@ @rdump.redis(0).keys.size #=> 2 -## Is base64 encoded +## Values are base64 encoded when with_base64=true @values = @rdump.dump -@values[0] -#=> "{\"db\":0,\"key\":\"stringkey1\",\"ttl\":-1,\"type\":\"string\",\"value\":\"c3RyaW5ndmFsdWUx\\n\",\"size\":12}" +# Check for values without relying on the order the keys were returned in +encoded_str = "{\"db\":0,\"key\":\"stringkey1\",\"ttl\":-1,\"type\":\"string\",\"value\":\"c3RyaW5ndmFsdWUx\\n\",\"size\":12}" +encoded_str2 = "{\"db\":0,\"key\":\"stringkey2\",\"ttl\":-1,\"type\":\"string\",\"value\":\"c3RyaW5ndmFsdWUy\\n\",\"size\":12}" +[@values.include?(encoded_str), @values.include?(encoded_str2)] +#=> [true, true] ## Can dump @values = @rdump.dump @values.size #=> 2 -# Clear DB 0 -db0 = Redis::Dump.new 0, @uri_base -db0.redis(0).flushdb -db0.redis(0).keys.size -#=> 0 - ## Can load data @rdump.load @values.join @rdump.redis(0).keys.size #=> 2 -## Is base64 decoded -stringkey = @rdump.redis(0).get('stringkey1') -#=> 'stringvalue1' - # Clear DB 0 db0 = Redis::Dump.new 0, @uri_base db0.redis(0).flushdb db0.redis(0).keys.size -#=> 0 +##=> 0 Redis::Dump.safe = true db0 = Redis::Dump.new 0, @uri_base db0.redis(0).flushdb - diff --git a/try/redis.conf b/try/redis.conf index 76fe89c..82e6836 100644 --- a/try/redis.conf +++ b/try/redis.conf @@ -9,7 +9,7 @@ dbfilename redisdump-test.rdb # requirepass @/ bind 127.0.0.1 -port 6371 +port 6379 databases 16 timeout 30