1.4.0 • Published 6 years ago

rethinkdb-migrate v1.4.0

Weekly downloads
114
License
MIT
Repository
github
Last release
6 years ago

rethinkdb-migrate

Build Status Coverage Status Code Climate Standard - JavaScript Style Guide Dependencies

RethinkDB migration tool

Acknowledgement

This tool is highly inspired by, or, should I say, is a rewrite of, Johan Öbrink's rethink-migrate. Unfortunately, rethink-migrate got stale. This is an attempt to improve the code and tests, track test coverage, update syntax to ES2015 and enhance functionality.

Install

You can either install rethinkdb-migrate globally:

npm install -g rethinkdb-migrate

Or define a npm script for migration and install rethinkdb-migrate locally:

npm install rethinkdb-migrate

In package.json:

{
  "scripts": {
    "migrate": "rethinkdb-migrate"
  }
}

In this last case, rethinkdb-migrate should be run as:

npm run migrate <commands>

All examples will consider that rethinkdb-migrate is installed globally.

Usage

There are currently three operations supported by rethinkdb-migrate:

OperationCommandDescription
createrethinkdb-migrate create <migration-name>Creates a migration with the given name
uprethinkdb-migrate upRuns all un-executed migrations up
downrethinkdb-migrate downRuns all executed migrations down (unless step option specified)

Create

$ rethinkdb-migrate create <migration name>

This operation creates a migration template file, where the database changes should be made.

The template exports two functions, up and down, that receive an instance of the RethinkDB driver and a connection object. These functions must return a Promise.

Running rethinkdb-migrate create new-migration will create a file YYYYMMDDHHmmss-new-migration.js in the directory ./migrations. Do not change the filename in any way after creating it.

Migrations template:

'use strict'

exports.up = function (r, connection) {
  // must return a Promise!
}

exports.down = function (r, connection) {
  // must return a Promise!
}

Migration example:

'use strict'

exports.up = function (r, connection) {
  return Promise.all([
    r.tableCreate('companies').run(connection),
    r.tableCreate('employees').run(connection)
  ])
}

exports.down = function (r, connection) {
  return Promise.all([
    r.tableDrop('companies').run(connection), r.tableDrop('employees').run(connection)
  ])
}

Up

$ rethinkdb-migrate up --db=mydb

This command will run all pending migrations up, in order of creation. See Options section to configure this task.

Down

$ rethinkdb-migrate down --db=mydb

This command will run all down steps from migrations that have been run previously.

Caution: this refreshes the database to before the first migration (potentially deleting data added since). Be very cautious about running this command in a production environment.

To rollback just a subset of migrations, use the step option:

$ rethinkdb-migrate down --step=2 --db=mydb

See Options section to further configure this task.

Options

The following options can be passed to rethinkdb-migrate:

Option nameDefault valueDescription
driverrethinkdbRethinkDB javascript driver. Can be either rethinkdb or rethinkdbdash.
hostlocalhostThe host to connect to, if using RethinkDB official driver.
port28015The port to connect on, if using RethinkDB official driver.
dbNone, this is requiredDatabase name. Please note that the db will be created if it doesn't already exist, so there is no need to explicitly create it in the migrations.
user''RethinkDB user
username''RethinkDB username
password''RethinkDB password
authKey''RethinkDB authKey
sslfalseRethinkDB SSL/TLS Support. This option can be either true or an options object that will be passed to tls.connect
discoveryfalseWhether or not the driver should try to keep a list of updated hosts. Only available when using rethinkdbdash driver
poolfalseWhether or not to use a connection pool when using rethinkdbdash driver.
cursortrueIf true, cursors will not be automatically converted to arrays when using rethinkdbdash.
serversundefinedArray of { host, port } of servers to connect to. Only available when using rethinkdbdash
stepnoneNumber of migrations to execute or rollback. If omitted, all migrations will be executed or rolled back, potentially refreshing the db to its initial state and resulting in data loss.
migrationsDirectorymigrationsDirectory where migration files will be saved
relativeToprocess.cwd()Root path from which migration directory will be searched or created (if inexistent)'
migrationsTable_migrationsTable where meta information about migrations will be saved. This should only be changed if you need a _migrations table in your application

Options can be passed to the script in three different ways:

  • Via environment variables
  • Via configuration files
  • Via command line arguments

Command line options take precedence over all other forms of passing options. Configuration file options take precedence over environment variables.

Passing options via environment variables

$ db=mydb rethinkdb-migrate up

Passing options via configuration file

Create a file that exports the options object (can be either a javascript file exporting an object, or a JSON file)

// config.js file

module.exports = {
  db: "mydb",
  driver: "rethinkdbdash",
  pool: true,
  servers: [
    { host: "localhost", port: 28015 },
    { host: "localhost", port: 28016 }
  ],
  ssl: false
}
$ rethinkdb-migrate up -f config.js

Passing options via command line arguments

$ rethinkdb-migrate down --db=mydb --host=127.0.0.1 --port=28016

Contributing

Feel free to suggest improvements and to open PRs. Please add/modify tests to maintain high coverage. Also, code must follow standard style.

Running tests:

In order to run tests, you need three instances of RethinkDB running, two of those should be in a cluster.

To achieve this, do as the following:

  • Install rethinkdb
  • Make sure you have two instances of RethinkDB in a cluster:
$ rethinkdb

# in another terminal session:
$ rethinkdb --port-offset 1 --directory rethinkdb_data2 --join localhost:29015
  • Make sure you have a third instance of rethinkdb running in port 48015:
$ rethinkdb -d rethinkdb_data3 --cluster-port 29017 --no-http-admin --driver-port 48015
  • Clone this repo
  • Make sure you are running node version >= 6
  • npm install
  • npm test

License

MIT License

1.4.0

6 years ago

1.3.0

6 years ago

1.2.3

6 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago

0.0.3

7 years ago

0.0.1

7 years ago