Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: balmasi/migrate-mongoose
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2d48bfa485478a59930f10d66023ebe7d93f2eae
Choose a base ref
...
head repository: balmasi/migrate-mongoose
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 76f67e31a4f4af9d067344c2245aafda5b6b6fcf
Choose a head ref
  • 9 commits
  • 13 files changed
  • 3 contributors

Commits on Mar 25, 2017

  1. Copy the full SHA
    b9098ef View commit details

Commits on Mar 28, 2017

  1. Merge pull request #22 from miangraham/master

    Fix reversed args to migrator.run() in programmatic usage README
    balmasi authored Mar 28, 2017
    Copy the full SHA
    47e5919 View commit details

Commits on Jun 22, 2017

  1. Fix migrate down without a migration name

    `migrate down` without a migration name will
    rollback only the last migration.
    
    This is due to sort option on finding `untilMigration`:
    `sort({ createdAt: -1 })` works only for the 'up' direction.
    For 'down' direction we need to `sort({ createdAt: 1 })` because
    we need the first migration instead the last one.
    shvetsovdm committed Jun 22, 2017
    Copy the full SHA
    a89d8df View commit details
  2. Fix typo in the lib run method

    shvetsovdm committed Jun 22, 2017
    Copy the full SHA
    0788f57 View commit details

Commits on Apr 9, 2019

  1. Merge pull request #27 from shvetsovdm/master

    Fix: `migrator.run('down')` without a migration name rollbacks only the last migration; Fix #26
    balmasi authored Apr 9, 2019
    Copy the full SHA
    e3707d1 View commit details

Commits on Jul 6, 2019

  1. Copy the full SHA
    3ee337c View commit details
  2. feat: remove babel support

    balmasi committed Jul 6, 2019
    Copy the full SHA
    dcdc96d View commit details

Commits on Jul 7, 2019

  1. Copy the full SHA
    d84336d View commit details
  2. 4.0.0

    balmasi committed Jul 7, 2019
    Copy the full SHA
    76f67e3 View commit details
9 changes: 0 additions & 9 deletions .babelrc

This file was deleted.

1 change: 0 additions & 1 deletion .npmignore

This file was deleted.

56 changes: 14 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# migrate-mongoose
A node based migration framework for mongoose
A node based migration framework for ES6+ for mongoose

#### Motivation
migrate-mongoose is a migration framework for projects which are already using mongoose.
@@ -63,7 +63,6 @@ Commands:
Options:
-d, --dbConnectionUri The URI of the database connection [string] [required]
--collection The mongo collection name to use for migrations [string] [default: "migrations"]
--es6 use es6 migration templates? [boolean]
--md, --migrations-dir The path to the migration files [string] [default: "./migrations"]
-t, --template-file The template file to use when creating a migration [string]
-c, --change-dir Change current working directory before running anything [string]
@@ -90,6 +89,14 @@ If you want to not provide the options such as `--dbConnectionUri` to the progra
```
export MIGRATE_dbConnectionUri=localhost/migrations
```

`.env` files are also supported. All variables will be read from the `.env` file and set by migrate-mongoose.

```bash
#.env
MIGRATE_dbConnectionUri=mongodb://localhost:27017/mydb
```

**2. Provide a config file (defaults to *migrate.json* or *migrate.js*)**
```bash
# If you have migrate.json in the directory, you don't need to do anything
@@ -109,49 +116,14 @@ Just make sure you don't have aliases of the same option with 2 different values
#### Migration Files
Here's how you can access your `mongoose` models and handle errors in your migrations

**ES5 Example**
```javascript
'use strict';

var lib = require('myLibrary');

/**
* Make any changes you need to make to the database here
*/
exports.up = function up (done) {
return lib.doSomeWork().then(function() {
// Don't forget to call done() or the migration will never finish!
done();
})
.catch(function(error){
// If you get an error in your async operations you can call done like so
done(error);
});

// Throwing errors also works
throw new Error('It should never get here!');
};

/**
* Make any changes that UNDO the up function side effects here (if possible)
*/
exports.down = function down(done) {
lib.undoAboveWork().then(function() {
done();
})
.catch(function(error){
done(error);
});
};
```

**ES6 Example**
**Example (ES6+)**
```javascript
/**
* Easy flow control
*/
// Notice no need for callback
export async function up() {
async function up() {
// Error handling is as easy as throwing an error
if (condition) {
throw new Error('This is an error. Could not complete migration');
@@ -184,9 +156,9 @@ const UserSchema = new Schema({
module.exports = mongoose.model('user', UserSchema);

// 1459287720919-my-migration.js
export async function up() {
async function up() {
// Then you can access it in the migration like so
await this('user').update({}, {
await this('user').updateMany({}, {
$rename: { firstName: 'first' }
}, { multi: true });

@@ -208,4 +180,4 @@ example: `-d mongodb://localhost:27017/development` . If you don't want to pass
### How to contribute
1. Start an issue. We will discuss the best approach
2. Make a pull request. I'll review it and comment until we are both confident about it
3. Profit
3. I'll merge your PR and bump the version of the package
2 changes: 1 addition & 1 deletion examples/config-file-usage/README.md
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ by using a config file (`migrate.json` by default), you can skip providing the o
Now instead of running this command to create a new migration

```
$ ./node_modules/.bin/migrate --es6 --migrationsDir db/migrations -d mongodb://localhost/db-dev create my_new_migration
$ ./node_modules/.bin/migrate --migrationsDir db/migrations -d mongodb://localhost/db-dev create my_new_migration
```

we can simply run
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
export async function up () {
async function up () {
await this('users').updateMany({}, { $set: { state: 'California' } });
}

export async function down () {
async function down () {
await this('users').updateMany({}, { $unset: { state: 1 } });
}

module.exports = {
down,
up
}
3 changes: 1 addition & 2 deletions examples/config-file-usage/migrate.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"dbConnectionUri": "mongodb://localhost/db-dev",
"migrationsDir": "db/migrations",
"es6": true
"migrationsDir": "db/migrations"
}
6 changes: 2 additions & 4 deletions examples/programmatic-usage/README.md
Original file line number Diff line number Diff line change
@@ -8,15 +8,13 @@ var
migrationsDir = '/path/to/migrations/',
templatePath,
dbUrl = 'mongodb://localhost/db',
useES6 = true,
collectionName = 'myMigrations',
autosync = true;

let migrator = new migrateMongoose({
migrationsPath: migrationsDir, // Path to migrations directory
templatePath: templatePath, // The template to use when creating migrations needs up and down functions exposed
dbConnectionUri: dbUrl, // mongo url
es6Templates: useES6, // Should migrations be assumed to be using ES6?
collectionName: collectionName, // collection name to use for migrations (defaults to 'migrations')
autosync: autosync // if making a CLI app, set this to false to prompt the user, otherwise true
});
@@ -30,10 +28,10 @@ migrator.create(migrationName).then(()=> {
});

// Migrate Up
promise = migrator.run(migrationName, 'up');
promise = migrator.run('up', migrationName);

// Migrate Down
promise = migrator.run(migrationName, 'down');
promise = migrator.run('down', migrationName);

// List Migrations
/*
Loading