Skip to content

Vectorface/redis-migrate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

redis-migrate

CLI tool to create and run redis key migrations.

Build Status

Overview

A migration file consists of a single exported object with two keys: up and down. The current release is designed to be backwards compatible with Redis 2.6 and 2.4. As such, it uses KEYS, which is blocking and should not be used on a production db when dealing with any significant number of keys. Future releases will allow for non-blocking operation using SCAN, HSCAN, etc.

Installation

It can be installed via npm using:

npm install -g redis-migrate

Usage

Usage: redis-migrate [up|down|create] <migrationName>

Options:

  -h, --help     output usage information
  -V, --version  output the version number

Example Migration File

exports.up = [
  {
    cmd: 'moveKeysToHashFields',
    src: {key: /(app:user:\d+):address/},
    dst: {key: '$1:properties', field: 'address'}
  },
  {
    cmd: 'renameKeys',
    src: {key: /(app:post:\d+):lastModifiedTimestamp/},
    dst: {key: '$1:lastModified'}
  }
];

exports.down = [
  {
    cmd: 'moveHashFieldsToKeys',
    src: {key: /(app:user:\d+):properties/, field: 'address'},
    dst: {key: '$1:address'}
  },
  {
    cmd: 'renameKeys',
    src: {key: /(app:post:\d+):lastModified/},
    dst: {key: '$1:lastModifiedTimestamp'}
  }
];

Example Runtime Usage

const redisMigrator = require('redis-migrate');
const redis = require('redis');

const dbClient = redis.createClient({
  url: 'redis://localhost:6379'
});

db.on('error', err => console.error(`Redis connect error: ${err}`));

db.on('connect', () => {
  // Run the "up" migration
  redisMigrator.up(dbClient, './path/to/migration/folder', 'migration-file.js', err => console.error(err));

  // Optionally, run the "down" migration to roll back the change
  redisMigrator.down(dbClient, './path/to/migration/folder', 'migration-file.js', err => console.error(err));
});

or async/await example

// Run the "up" migration
const upMigrationFunc = async (file) =>
  await new Promise((resolve, reject) => {
    redisMigrator.up(dbClient, './path/to/migration/folder', file, err => {
      if (err) return reject(err);
      resolve();
    });
  });

About

CLI tool to create and run redis key migrations

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •